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 (2015) Letter Game App Review: Python Basics

Robert Baucus
Robert Baucus
3,400 Points

Python Basics Review Quiz

Review Quiz for Python Basics, the 3rd part says:

"Finish this so 'return' happens if there is no exception

try:
    count = int(input("Give me a number: "))
except ValueError:
    count = 0
_________________:
    return count * 15

They want me to fill in the blank and I am completely drawing a blank!

2 Answers

andren
andren
28,558 Points

The missing word is else.

A Try statement has three parts:

The try block which contains code that might raise an exception. The except blocks which will be ran if an exception of the specified type occurs. And the else block which runs if no exception occurs after the code inside the try block runs.

Great answer!

I just wanted to point out that out of the scope of this course, try actually has 4 parts; try, except, else and finally. The first three are exactly as you describe, but finally is the optional last part of the block that has some interesting properties. Whereas else will run in the event that there was no exception ( we can think of it like an if/else block - if exception, else) finally will run regardless of anything that happens in the try block - once you have entered the try block, finally will run no matter what.

On the face of it that can seem kind of redundant - couldn't we just put anything we wanted to run regardless of there being an error AFTER the try block? And most of the time you can, finally is more of a "you'll know it when you need it" kind of tool.

For example, even if you try to get out of a try block with continue, break or even the return statement, finally will still run, actually even if an error is raised that you didn't catch with an except block, it will still run. Here's an example of the return case:

def add_nums(a, b):
    try:
        a + b
    except TypeError:
        return "ops {} & {} aren't compatible types". format(
                                                            type(a).__name__, 
                                                            type(b).__name__
                                                            )
    else:
        return a + b

    finally:
        print("exited function")

print(add_nums(2, "2"))
print(add_nums(2, 2))

Will print:

exited function
ops int & str aren't compatible types
exited function
4

Because the finally clause happened before the result was returned to print. One quick gotcha - if you put a return in finally, it will override any return statements inside the try block that have already been run. So in this eacmple, if the finally block returned the string instead of printing it the output would look like this:

exited function
exited function
andren
andren
28,558 Points

Thank you Jon Mirow for adding that extra info :smile:. I don't actually code in Python that often myself so I honestly forgot about the final part while writing my answer. But it is indeed a useful and important part of the try statement and you have done a great job explaining it.

Robert Baucus
Robert Baucus
3,400 Points

I did not know about the "finally" . Thanks both Andren and Jon Mirow for your answers.