Does my code don't work or is it a bug? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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.

10th Nov 2022, 4:15 PM
Engineer X
Engineer X - avatar
4 Antworten
+ 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 :)
10th Nov 2022, 4:40 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 4
Thank a lot Apollo-Roboto , By the way how did you find out that was the problem?
10th Nov 2022, 4:42 PM
Engineer X
Engineer X - avatar
+ 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"):
10th Nov 2022, 5:11 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 3
Also, you can use the "in" operator: elif y in ("Pizza", "Nachos"):
11th Nov 2022, 12:16 AM
Emerson Prado
Emerson Prado - avatar