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
Elgene Ee
3,554 PointsWhat 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
andren
28,558 PointsThere are a couple of issues in your code:
- The line of code which can rise a
ValueErrorisvar2 = int(input("How old are you?")but you have placed this line outside thetrystatement. When catching exceptions the exception causing code has to be within thetryblock. - 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. - Within your
expectblock you use thebreakkeyword. 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.
Elgene Ee
3,554 PointsElgene Ee
3,554 PointsThank you andren for your help, is return better or print? which one do you use often?
andren
28,558 Pointsandren
28,558 PointsWell 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
returnkeyword 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).printon 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
returnactually 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
returnrather than aprint, but for the function you are writing it makes more sense to useprintstatements 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 thatprintandreturnare of course not exclusive, you can (and often will) use both of them in a function.