+ 2

Why my code does not function as intendet?

x = input() for y in x : total = 0 if y == "Cheeseburger" : total += 10 if y == "Pizza" : total += 6 if y == "Nachos" : total += 6 if y == "Water" : total += 4 if y == "Coke" : total += 5 else: total += 5 print(total) The input is always something else.

9th Nov 2022, 5:37 PM
Engineer X
Engineer X - avatar
5 Answers
+ 13
Engineer X , there are several issues in the code: (1) the input string (separated by space) has to be split, otherwise we can not calculate the total price properly. currently the code iterates over each character in the string. (2) the calculation of the tax is missing, see the task description. (3) a rounding for 2 decimal places has to be done finally (3) we should use if... elif... else... instead of individual if conditionals
9th Nov 2022, 5:51 PM
Lothar
Lothar - avatar
+ 7
Engineer X , here is an explanation why we sometimes need to split strings. see the (basic) sample: https://code.sololearn.com/cdG7wUXiD8QU/?ref=app
10th Nov 2022, 11:54 AM
Lothar
Lothar - avatar
+ 6
Put total=0 before the for loop.
9th Nov 2022, 5:41 PM
Oma Falk
Oma Falk - avatar
+ 2
Hi Lothar , Thanks for the info, but I also have a question: I only know that the code split() splits the string values but how can I use it?
9th Nov 2022, 8:03 PM
Engineer X
Engineer X - avatar
+ 2
Hi Lothar , I can only say Thank you for the explanation, I really needed to know this.
10th Nov 2022, 2:48 PM
Engineer X
Engineer X - avatar