OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

print exception message in try block

  • Thread starter Thread starter Gavin Reid
  • Start date Start date
G

Gavin Reid

Guest
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}")

I've tried nesting the blocks differently, but to no avail.
<p>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.</p>
<p>Can anyone explain why this is?</p>
<pre><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></pre>
<p>I've tried nesting the blocks differently, but to no avail.</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top