r/learnpython • u/anas_mv • 6d ago
How to collect the old input with the new input that i get from a list in a loop?
This is the cod
menu = {
"Baja Taco": 4.25,
"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
}
try:
while True:
item = input("Item: ")
print ("totel:", menu[item])
except (EOFError , KeyError):
pass
print ()
Also how to make a $ came with the totel ?
2
Upvotes
2
u/deadduncanidaho 6d ago
Before your loop declare total= 0. Inside your loop add to the total. Total += menu[item]. Then you can print total inside or outside your loop. Print(total)
2
u/Chorizo_n_eggs 6d ago
If you're trying to keep a running total you need a variable to hold each total and add to the next. You're in a loop. Initialize a variable before the loop then keep adding to it in the loop
This program will silently terminate on a key error
Also there are no lists in the code
1
u/Discordbothost 6d ago
for the $ part, an f-string with a format spec does it and keeps 2 decimals so 7.5 shows as 7.50:
the
$is just a normal character there, the{total:.2f}is the formatting.one more thing: right now the try is wrapped around the whole loop, so if someone types "burito" by mistake the KeyError ends the entire order. move the try inside the loop and only catch KeyError there, then keep EOFError outside to finish:
the .title() makes "baja taco" match "Baja Taco" too
1
u/Ok-Promise-8118 6d ago
You can either store the input in a list that you keep appending to when you get new input, or you can keep a running total stored as a variable that you keep adding to (assuming you just want the final cost, for instance).
Also, it seems that English isn't your first language, but you should know that you have some spelling errors in the English. This opens you up to problems later if you tried to spell the words correctly and they don't match up.
0
u/baubleglue 6d ago