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 Python Basics All Together Now Cleaner Code Through Refactoring

How do you account for all exceptions?

After finishing this part of the course, an error was still present when we fed in a string instead of an integer. (for example "blue").

My code is exactly as it is in the video but Craig didn't check the final code with a string. My console would output:

Oh no, we ran into an issue. invalid literal for int() with base 10: 'blue'. Please try again

Any help is greatly appreciated.

Cheers

2 Answers

Claudia Xie
Claudia Xie
2,125 Points

What I did was remove the int() from the num_of_tickets input prompt first. I then checked if the response to the input contained all numbers. If it didn't, I raised a ValueError. Otherwise, I converted the response to int().

num_of_tickets = input('Hello, {}! How many tickets would you like?\n'.format(username))
        if all(char.isdigit() for char in num_of_tickets) == False:
            raise ValueError('Input is not a valid number.')
        else:
            num_of_tickets = int(num_of_tickets)
Bryan Land
Bryan Land
10,306 Points

I ran into this issue too, and this solution is really good, but its beyond the scope of this beginner course. I decided to break it down for everyone...

# First, they are prompting the user for input, asking how many tickets they would like
num_of_tickets = input('Hello, {}! How many tickets would you like?\n'.format(username))
    # The entire if statement below checks if any characters in num_of_tickets is a string
    # all() is a built in function checking if all values in an iterable return true.
    # every 'char'acter in num_of_tickets is checked to make sure it ".isdigit()" with a for loop
    if all(char.isdigit() for char in num_of_tickets) == False:
        # and if any are a string value, it would make the expression False, 
        # which will raise the ValueError
        raise ValueError('Input is not a valid number.')
    # if all of the characters in num_of_tickets are integer values, the expression it does not 
    # raise the ValueError for later use in "except ValueError as err:"
    else:
        num_of_tickets = int(num_of_tickets)

Hope this helps! It sure helped me to understand the all function!

Steven Parker
Steven Parker
229,708 Points

You see that message because your program is handling the exception caused by the string input. Otherwise, the program would end instead of giving you the chance to "try again".