+ 2
Does my code don't work or is it a bug?
Order = input() x = Order.split() count = 0 The code bellow supposed to take user input and give the user back the price which is a number. This code was in "coach tip" and all tests were correct but only "test 4" which was always shown incorrect. Is it a bug or my code doesn't work? for y in x : if y == "Cheeseburger" : count += 10 elif y == ("Pizza" or "Nachos") : count += 6.00 elif y == "Water" : count += 4.00 elif y == "Coke" : count += 5.00 else : count += 5.00 z = (count*7)/100 print(z + count) Any answer will be appreciated.
4 Answers
+ 3
This line is the problem:
elif y ==("Pizza" or "Nacho"):
you should split them into two different conditions, even if they have the same price
elif y == "Pizza":
count...
elif y == "Nachos":
count...
I hope this helps :)
+ 4
Thank a lot Apollo-Roboto ,
By the way how did you find out that was the problem?
+ 4
the or operator won't behave like you would expect on strings.
("a" or "b") will give "a", why is that?
turns out it's because the or operator will return the first string that is not None or empty
so ("" or "b") will give "b"
if you wanted both on the same line, you need to compare with y again
elif (y == "Pizza") or (y == "Nachos"):
+ 3
Also, you can use the "in" operator:
elif y in ("Pizza", "Nachos"):