I'm working on a Python program that allows users to place an order by inputting items, one per line, until they press Ctrl+D to end their input. After each input, I want to display the total cost of all items inputted once the user presses Ctrl+D, with a dollar sign ($) prefix and formatted to two decimal places. The user's input should be case-insensitive, and I want to ignore any input that is not a valid item from a predefined menu.Here's a simplified version of my code:
food_menu = {
"Baja Taco": 4.00,
"Burrito": 7.50,
"Bowl": 8.50,
"Nachos": 11.00,
"Quesadilla": 8.50,
"Super Burrito": 8.50,
"Super Quesadilla": 9.50,
"Taco": 3.00,
"Tortilla Salad": 8.00
}
cost = []
while True:
try:
item = input("Item: ").title()
if item in food_menu:
cost.append(food_menu\[item\])
else:
continue
except EOFError:
print()
break
total = sum(cost)
print("Total: $", f"{total:.2f}", sep="")
The issue I'm facing is that if I want to input multiple instances of the same item, such as "taco", "taco" and "quesadilla," I have to press Enter after each item and then when I'm prompted for the fourth time I'm able to press Ctrl+D and get the total amount. Ideally, I would like to be able to input "taco", "taco" and "quesadilla" with just two enters and one Ctrl+D, i.e. I'm only prompted thrice and use Ctrl-D instead of thrid 'enter'. How can I modify my code to achieve this behavior?
Here is my attempt for the script:
When entered
taco taco quesadillathe result is: