+ 1
Ballpark Orders Challenge
I don't know what I'm missing. I've printed subtotal to the console and it looks like my for loop is stopping at the first if statement and appending 6 to subtotals 36 times when I run it. Code below. subtotal = [ ] menu = ["Pizza", "Nachos", "Cheeseburger", "Water", "Coke"] orders = str(input().split()) for i in orders: if i == "Pizza" or "Nachos": subtotal.append(6) elif i == "Cheeseburger": subtotal.append(10) elif i == "Water": subtotal.append(4) elif i == "Coke": subtotal.append(5) elif i not in menu: subtotal.append(5) total = round((sum(subtotal) * 1.07), 2) print(total)
3 Antworten
+ 3
AH. Thank you. (:
+ 1
The dictionary type instead of list optimises your code a lot better—because you can retrieve the value of the menu items in your loop using the get function.
Also, since ordering a coke is the same as ordering something not in the menu, you can eliminate coke from your loop and menu.
And you can have your subtotal as a float rather than a list, so you can have your loop add to that float rather than having to sum your list afterwards, making your code more efficient.
menu = {"Pizza": 6.0, "Nachos": 6.0, "Cheeseburger": 10.0, "Water": 4.0}
orders = input().split()
cost = 0.0
for order in orders:
cost += menu.get(order, 5.0)
print(round(cost * 1.07, 2))
0
Keep pizza and nachos separately