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

What is wrong with my try except code?

Hey guys, I wrote a simple try except block just to explore python...Do forgive me on my code here, I ran it on repl but it doesn't work. Does any python user have idea on this one?

var = str(input("Hi what's your name?"))
print("Hi {}!".format(var))

var2 = int(input("How old are you?")
def test():
    try:
        if var2 > 30:
            return("you should get a job")
        else:
            return("you should finish your study")
    except ValueError:
        print("This is not a number")
        break
    else:
        return("Hi {}, so your age is {}. Confirm?".format(var, var2))

test()

1 Answer

There are a couple of issues in your code:

  1. The line of code which can rise a ValueError is var2 = int(input("How old are you?") but you have placed this line outside the try statement. When catching exceptions the exception causing code has to be within the try block.
  2. You are calling two function in this line var2 = int(input("How old are you?") but only closing one of them with a parenthesis, which means you are missing a closing parenthesis.
  3. Within your expect block you use the break keyword. That keyword is used to forcibly end a loop, but there is no loop in your code, so using that keyword is invalid.

Those are the only direct issues, so if you fix those like this:

var = str(input("Hi what's your name?"))
print("Hi {}!".format(var))


def test():
    try:
        # Move exception casting code within try and add closing parenthesis
        var2 = int(input("How old are you?"))
        if var2 > 30:
            return("you should get a job")
        else:
            return("you should finish your study")
    except ValueError:
        print("This is not a number")
        # Remove break keyword
    else:
        return("Hi {}, so your age is {}. Confirm?".format(var, var2))

test()

Then your code will work.

I do have some further nitpicks though, all of your code paths ends with return statements other than the except case, where you use print instead. In the REPL print and return might seem to behave similarly, but in reality they do very different things. So you should try to not get into the habit of using them interchangeably.

Also ideally you should place all code that logically fits together into one function, not place some inside and some outside like you have currently.

Thank you andren for your help, is return better or print? which one do you use often?

Well as mentioned they are actually not interchangeable, since they do completely different things you can't say one is better than the other, it's not a logical comparison.

Put somewhat simply the return keyword takes a value from a function and passes it back to the code that called it, and also ends the function (No code after a return is hit is executed). print on the other hand simply prints a value to the console. In the REPL they seem similar because the REPL is setup to automatically print anything that gets returned, outside the REPL they are not connected at all.

Once you get farther into the course and start to see what return actually does in practice, and how it's used you'll probably start to get a better understanding of what I mean.

Generally speaking it's more common for a function to end with a return rather than a print, but for the function you are writing it makes more sense to use print statements as the strings are messages for the user and they don't seem like something that would serve much use outside the function. Though it's worth mentioning that print and return are of course not exclusive, you can (and often will) use both of them in a function.