Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,227 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!