Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code help?
#7
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?
Reply


Messages In This Thread
Code help? - by Beau - May-21-2020, 12:17 PM
RE: Code help? - by GOTO10 - May-21-2020, 12:30 PM
RE: Code help? - by Beau - May-21-2020, 12:56 PM
RE: Code help? - by GOTO10 - May-21-2020, 01:20 PM
RE: Code help? - by pyzyx3qwerty - May-21-2020, 01:34 PM
RE: Code help? - by GOTO10 - May-21-2020, 01:50 PM
RE: Code help? - by pyzyx3qwerty - May-22-2020, 10:06 AM
RE: Code help? - by Beau - May-21-2020, 01:38 PM
RE: Code help? - by deanhystad - May-21-2020, 01:48 PM
RE: Code help? - by Beau - May-21-2020, 01:48 PM
RE: Code help? - by Beau - May-21-2020, 02:00 PM
RE: Code help? - by GOTO10 - May-21-2020, 02:04 PM
RE: Code help? - by Beau - May-21-2020, 02:10 PM
RE: Code help? - by deanhystad - May-21-2020, 02:36 PM
RE: Code help? - by Beau - May-21-2020, 07:56 PM

Forum Jump:

User Panel Messages

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