Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Martina Frisk
Martina Frisk
378 Points

After I did the "except ValueError as err", I can't get the right error message when i enter "blue".

I wrote the "except ValueError as err"-code, and I get the right error message when I enter that I want to buy more tickets than are available. But now, It does not work when I write an invalid input such as "blue", then I get this output:

What is your name? Gallahad
How many tickets would you like, Gallahad? blue
Oh no, we ran into an issue. invalid literal for int() with base 10: 'blue'. P lease try again.
There are 100 tickets remaining.
What is your name?

This is how my code looks like:

    try:  
      amount_tickets = int(amount_tickets)
      if amount_tickets > tickets_remaining:
        raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
      print("Oh no, we ran into an issue. {}. Please try again.".format(err))

Thankful for some help! /Martina

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Not sure what behavior you are expecting, but it is operating correctly. When a int('blue') is attempted it raises a ValueError and the code immediate moves to the except block. The message is correct invalid literal for int() with base 10: 'blue'. That is, the invalid literal "blue" can not be converted by int().

In the test example below, you can see that the print statement is not executed.

>>> try:
...     int('blue')
...     print("got here")
...     if True:
...         raise ValueError("There are only x tickets left")
... except ValueError as err:
...     print("oh, no", err)
... 
oh, no invalid literal for int() with base 10: 'blue'

Post back if you need more help. Good luck!

Martina Frisk
Martina Frisk
378 Points

Thanks! Now I understand!