+ 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.
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
+ 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
+ 6
Put total=0 before the for loop.
+ 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?
+ 2
Hi Lothar ,
I can only say Thank you for the explanation, I really needed to know this.