Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code help?
#11
(May-21-2020, 01:57 PM)Beau Wrote: Ok well this has really blown my mind now im a beginner don't forget lol...
so I watched and read you cant have strings and numbers on the same print line??? that's first

secondly I am sure your version is heaps better although I have no idea what sep=' ' is or what the f' is after print(f' haha sorry not that far in yet....

In your simple code, you do need to convert numeric values to strings in order to use the '+' operator to concatenate (join them together to be printed by your print statement). If you don't convert the number to a string first, you would be trying to add a string value and a numeric value, which would result in an error.

As dean's post illustrates, there are a LOT of different ways of doing the same thing when you are writing code. When learning (and I say this as someone just starting out myself), step one is figuring out how to do something and get the result you want. Steps two through one million are figuring out how to do it more efficiently. Big Grin
Reply
#12
(May-21-2020, 02:04 PM)GOTO10 Wrote:
(May-21-2020, 01:57 PM)Beau Wrote: Ok well this has really blown my mind now im a beginner don't forget lol...
so I watched and read you cant have strings and numbers on the same print line??? that's first

secondly I am sure your version is heaps better although I have no idea what sep=' ' is or what the f' is after print(f' haha sorry not that far in yet....

In your simple code, you do need to convert numeric values to strings in order to use the '+' operator to concatenate (join them together to be printed by your print statement). If you don't convert the number to a string first, you would be trying to add a string value and a numeric value, which would result in an error.

As dean's post illustrates, there are a LOT of different ways of doing the same thing when you are writing code. When learning (and I say this as someone just starting out myself), step one is figuring out how to do something and get the result you want. Steps two through one million are figuring out how to do it more efficiently. Big Grin


AMEN !! lol thank you :)
Reply
#13
Most languages allow doing math with floats and ints. The computer doesn't allow doing this, so the language "promotes" the more restrictive type to the less restrictive type and returns a result of the less restrictive type.

You can have strings and floats and ints and all kinds of things in a print statement. The print statement calls the str() automatically when needed. Or maybe it calls it for all arguments.

"sep=" lets you tell print what you want to use to separate items in the print output (default is ' '). There is also an "end=" for what you want to do at the end of the line (default is '\n'), You can also print to a file. print is a tool. To use the tool you should understand how it works. It is really easy to copy an example and say "Done!" once it works, but that leads to an incomplete understanding and leaves you unprepared when you make a mistake. And then you have to post a question to the forum and listen to me pontificate.

f' is the newest incarnation of formatted strings for python. The curly brackets contain code to evaluate and convert to a string. Formatting eliminates having to concatenate strings or specifying the separator. You can also set things like field width, justification, precision so your output is arranged in nice columns. I used formatting in my code because it is so easy to read (once you know what the f' and the {} mean).
Reply
#14
(May-21-2020, 02:36 PM)deanhystad Wrote: Most languages allow doing math with floats and ints. The computer doesn't allow doing this, so the language "promotes" the more restrictive type to the less restrictive type and returns a result of the less restrictive type.

You can have strings and floats and ints and all kinds of things in a print statement. The print statement calls the str() automatically when needed. Or maybe it calls it for all arguments.

"sep=" lets you tell print what you want to use to separate items in the print output (default is ' '). There is also an "end=" for what you want to do at the end of the line (default is '\n'), You can also print to a file. print is a tool. To use the tool you should understand how it works. It is really easy to copy an example and say "Done!" once it works, but that leads to an incomplete understanding and leaves you unprepared when you make a mistake. And then you have to post a question to the forum and listen to me pontificate.

f' is the newest incarnation of formatted strings for python. The curly brackets contain code to evaluate and convert to a string. Formatting eliminates having to concatenate strings or specifying the separator. You can also set things like field width, justification, precision so your output is arranged in nice columns. I used formatting in my code because it is so easy to read (once you know what the f' and the {} mean).

Thanks I have been practising with this and think im getting it lol.. I totally agree that understanding it completely is key hence even tho I got it working I wanted to understand why so thanks again and any other tips or pointers would be fully appreciated ! the f' function I can see will save a lot in the long run.. do you by any chance have a tutorial for better understanding you could point me in the direction of??
Reply
#15
(May-21-2020, 01:50 PM)GOTO10 Wrote:
(May-21-2020, 01:34 PM)pyzyx3qwerty Wrote: In your code, it should be
str(float(num_burgers*cost_burgers)))

I don't think this works, because num_burgers is obtained by input() and is therefore a string until converted.

Well I was about to post that too and he could change it by placing float() before the inout(), but @deanhystad explained the whole thing

(May-21-2020, 07:56 PM)Beau Wrote: the f' function I can see will save a lot in the long run.. do you by any chance have a tutorial for better understanding you could point me in the direction of??
Dunno if you are talking about formatted strings but if by any chance you are, well here you go:-
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020