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 Functions and Looping Exception Flow

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Understading the exception questions

Two questions are provided but i do understand the answers print("A") try: result = 5 + 5 print("B") except ValueError: print("C") except TypeError: print("D") else: print("E") print("F")

Answer ABEF

print("A") try: result = "test" + 5 print("B") except ValueError: print("C") except TypeError: print("D") else: print("E") print("F")

Answer answer ADF

Could someone please explain the results are what there are Thanks for advance

4 Answers

Cooper Runstein
Cooper Runstein
11,850 Points

Result is what is being tried, if result isn't valid, it throws the exception. For example, if result is a string that is being cast to an integer, which won't work ('hello' can never be a number), it will cause one of the except cases to fire.

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Hi Cooper Thanks for the quick response Ok i understand it 30% better. i got the point of result is what being tried but what is the input for us to get result = 5 + 5. I am not sure if i worded that correctly if not could you run through the question step by step and explain what is happening at each stage

Cooper Runstein
Cooper Runstein
11,850 Points

For me, it made more sense when learning this to not use something constant, result = 5 + 5 will always be a valid expression, so it seems kinda silly to put it in a try/except block. Imagine result was an input instead:

try:
    result = input('Give me a number')
    my_new_number = int(result) + 5
except ValueError:
    print('Whoops! That didn't work')

In this case, if result is a number, everything will work. If not, the exception will fire. In the case of their example, that result in a constant number, but if that constant isn't valid, the exception will still fire.

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

The above makes clear sense and i can see the point of the valid expression. However if we are using your example for a second a valid input will be any number not a string. but back to the example print("A") try: result = 5 + 5 print("B") This part confuses me everything underneath this is fine. In others words in what scenario would it print B

Cooper Runstein
Cooper Runstein
11,850 Points

In that case, the exceptions will never fire, because result will always equal 10 and the program will always print B if it's in the try block. That's the problem with the challenge problem, you'd never need a try/except block for that scenario in real world code. If result is constant, every single time through the code the try block will pass. Typically try/except is used for situations where you're relying on external input that may or may not be valid.