Python Forum
Code help? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Code help? (/thread-26988.html)

Pages: 1 2


Code help? - Beau - May-21-2020

Hi if someone could help me id be really grateful..
im new to this and just started studying it.. I been asked to write a simple code asking for a number of burgers a pre determined price and to give the total cost of it after.

Ive managed to do this with some messing about and several youtube videos, however I don't really understand something. if I put the code below at first I was * (multiplying) num_burger bhy cost_burger and was getting an error message, however when I reversed it to cost_burger by num_burger it was ok? could someone explain this to me so I don't run into the same problem especially when doing bigger coding.. Many Thanks

This one works...

num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55

print("\nok each burgers cost 1.55 each")
print("\nso the price of your burger will be £" + str(float((cost_burgers) * (float(num_burgers)))))
However this doesn't????

num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55

print("\nok each burgers cost 1.55 each")
print("\nso the price of your burger will be £" + str(float((num_burgers) * (float(cost_burgers)))))
Many thanks


RE: Code help? - GOTO10 - May-21-2020

You should be using BBCode when posting your code. I'm sure a moderator will be along shortly to clean it up for you. Smile

In the first version of your code, you are explicitly converting num_burgers to a float: float(num_burgers)

In the second version, you are not converting num_burgers at all, just attempting to multiply it and then present the output as a float. Only use as many parentheses as you actually need, and it will be easier to see. Technically, since you've already defined cost_burgers with a float value, there is no need to use float() on it.

print("\nso the price of your burger will be £" + str(float(num_burgers) * cost_burgers))



RE: Code help? - Beau - May-21-2020

Oh shoot sorry I literally signed up and posted this quick, I don't think I can edit it now?

Ok cool so many thanks for your reply I think I kind of get it so I don't need to define the cost of the burgers as ive but in a float value of 1.55..

However im still a little confused as I did put float((num_burgers) and define it as a float on both versions the only difference being an extra open bracket on the first one which tbh was just a matter of playing about until it worked (Doh) but I don't get why it would work one way and not the other? I just took the extra bracket out and it works both ways now?? so why would that bracket make a difference?
 str(float((num_burgers)))
vs

 str(float(num_burgers))
I would of thought the first one would of been the correct way
str(
float(
(the variable)
apologies for my newb dumbness , im sure I will look back at this


RE: Code help? - GOTO10 - May-21-2020

To clarify, it's not the extra parentheses that are the problem, but what's included in the parentheses. In your first example, the extra parentheses are irrelevant because num_burgers is the only thing inside the parentheses, so float() is still being applied to the variable num_burgers:
str(float((num_burgers)))
But in your original code, the float() is being applied to the expression "(num_burgers) * (float(cost_burgers))" rather than the num_burgers variable itself:
float((num_burgers) * (float(cost_burgers)))



RE: Code help? - pyzyx3qwerty - May-21-2020

In your code, it should be
str(float(num_burgers*cost_burgers)))



RE: Code help? - Beau - May-21-2020

Ok think I get this now.. if I closed the bracket for the first float after float((num_burgers))) it would work as im defining that one ….even tho this is not the way to do it it would of worked.. so sorry I think I was getting confussed as it was working the other way round but as you said it was already defined in the variable.. ok cool thanks so much for your help much appreciated

(May-21-2020, 01:34 PM)pyzyx3qwerty Wrote: In your code, it should be
str(float(num_burgers*cost_burgers)))

Actually I tried this and it don't work ??

num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55


print("\nok each burgers cost 1.55 each")
print("\nso the price of your burger will be £" + str(float(num_burgers*cost_burgers)))



RE: Code help? - deanhystad - May-21-2020

There is no need to convert values to strings for printing. Print does that automatically. That gets rid of a layer of function calls and parenthesis and makes the error stand out a little more:
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", float((num_burgers) * float(cost_burgers)), sep='')
The problem becomes much easer to see if the unnecessary conversion from float to float is also removed.
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", float(num_burgers * cost_burgers), sep='')
And it is really easy to see the problem when the conversion and math is removed from the print statement.
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
total = float(num_burgers * cost_burgers)
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", total, sep='')
By now the solution is obvious and you feel compelled to do the required conversion where it belongs.
cost = 1.55
count = int(input("How many burgers do you want ? "))
total = count * cost
print(f'{count} burgers at £{cost} each is £{total} please')
When compared to this:
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
print("\nok each burgers cost 1.55 each")
print("\nso the price of your burger will be £" + str(float((cost_burgers) * (float(num_burgers)))))
Which is easier to read?


RE: Code help? - Beau - May-21-2020

Output:
how many burgers do you want ? 12 ok each burgers cost 1.55 each
I get this error
Error:
Traceback (most recent call last): File "main.py", line 6, in <module> print("\nso the price of your burger will be £" + str(float(num_burgers * cost_burgers))) TypeError: can't multiply sequence by non-int of type 'float' 



RE: Code help? - GOTO10 - May-21-2020

(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.


RE: Code help? - Beau - May-21-2020

(May-21-2020, 01:48 PM)deanhystad Wrote: There is no need to convert values to strings for printing. Print does that automatically. That gets rid of a layer of function calls and parenthesis and makes the error stand out a little more:
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", float((num_burgers) * float(cost_burgers)), sep='')
The problem becomes much easer to see if the unnecessary conversion from float to float is also removed.
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", float(num_burgers * cost_burgers), sep='')
And it is really easy to see the problem when the conversion and math is removed from the print statement.
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
total = float(num_burgers * cost_burgers)
print("ok each burgers cost 1.55 each")
print("so the price of your burger will be £", total, sep='')
By now the solution is obvious and you feel compelled to do the required conversion where it belongs.
cost = 1.55
count = int(input("How many burgers do you want ? "))
total = count * cost
print(f'{count} burgers at £{cost} each is £{total} please')
When compared to this:
num_burgers = input("how many burgers do you want ? ")
cost_burgers = 1.55
print("\nok each burgers cost 1.55 each")
print("\nso the price of your burger will be £" + str(float((cost_burgers) * (float(num_burgers)))))
Which is easier to read?



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.... oh and I also thought int does not work with decimals?? aaaaahhhh head blown emoji