I’m trying to practise Try/Except. In my code below, my initial EOFError prints the message and breaks correctly. However, in the second try block if I enter an item that is not in the dictionary the message does not print, it just re-prompts for input.
Can anyone explain why this is?
<code>taquitos = {
"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
}
order = 0
while True:
try:
item = input("Place your order: ").title()
except EOFError:
print("End of order")
break
else:
if item in taquitos:
try:
order += taquitos[item]
except KeyError:
print("Item not found")
else:
tot_order = format(order, ".2f")
print(f"Total: ${tot_order}")
</code>
<code>taquitos = {
"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
}
order = 0
while True:
try:
item = input("Place your order: ").title()
except EOFError:
print("End of order")
break
else:
if item in taquitos:
try:
order += taquitos[item]
except KeyError:
print("Item not found")
else:
tot_order = format(order, ".2f")
print(f"Total: ${tot_order}")
</code>
taquitos = {
"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
}
order = 0
while True:
try:
item = input("Place your order: ").title()
except EOFError:
print("End of order")
break
else:
if item in taquitos:
try:
order += taquitos[item]
except KeyError:
print("Item not found")
else:
tot_order = format(order, ".2f")
print(f"Total: ${tot_order}")
I’ve tried nesting the blocks differently, but to no avail.
1