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 Python Basics Functions and Looping Expecting Exceptions

check_please - Completely Overwhelming!

I have been following most of this and to be honest, the check_please script makes me feel very sad. I am trying so, so hard to be able to code, so hard and I'm so upset because it's so much information!

I am new to all this and just about been following along but this whole script, it's overwhelming and I can't write this on my own, I feel like an absolute failure :(

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, peterrrrrrr! First, don't give up! Smart money says that you're doing better than you think you are and you'll get it eventually. I'm going to try my best to give you a clear explanation. This took a while for me too when I was first starting out so don't worry.

Let's talk a bit first about getting input from the user. No matter what the user types in at the prompt it will always be a string. Even if it looks like a number, it's still a string. The terminal/command prompt may look like 23 but when it gets read in, it will be a "23".

Now let's talk about what happens when we try and convert a string to an integer. Well, if the string is "23", there's no problem at all! Python can do that. But what happens when the user types "treehouse" and we try int("treehouse")? Woops! The app would crash with a stack trace about a ValueError.

This brings us to our next point: what is a ValueError? Python has built-in functions like int(). That function can take either a number like 3.999 or a string. Either are valid types. Since it can't convert the string "treehouse" to an integer, Python raises a ValueError. The type is correct but the value is such that it still can't perform the action.

So to mitigate this we have the concept of try, except and else. The try is what you're trying to do. It is the thing that could cause an error. We can't guarantee that we can convert the user's input to an integer because users are wildly unpredictable and could have a cat running across their keyboard at the moment.

The except is what happens if there was an error. And the else is what happens if there was not an error.

I've included some example code with comments so you can follow along:

user_input = input("Give me a number between 1 and 10:  ")

try: 
    user_input = int(user_input) # Try to convert their input to an integer
except ValueError:
    print("Woops! That doesn't look like a number")  # print something if it failed
else:  # this happens if it could be converted to an integer without problem
    if user_input < 1 or user_input > 10:  # if it was less than one or greater than 10 it is out of range
        print("Woops! That's out of range.")
    else:   # otherwise it's fine
        print(f"You chose the number {user_input}")

Hope this helps! :sparkles:

Thank you so much that is so kind of you! I understand it a lot better :D