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

ok, I am not understanding the logic by which the output values "A" "D" "F" are being returned

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

2 Answers

boi
boi
14,241 Points

Hey Jason! how are you? Alright let's check the logic, and go through each step very slowly. First the complete code below,

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

Now breaking it down; First step;

print("A")

The first step is "print("A")", pretty simple no need to overthink, so let's run it,

print("A")

#Output is "A". YES WE GOT "A"

Now step 2 is the try block, things can get complicated but let's focus.

try:
    result = "test" + 5
    print("B")

OH, this seems to be wrong because the "test" is a string type and the (5) is an int type, notice that the "test" word has the quotation marks("") at the beginning and end of it, so it makes it a string type and since the number (5) have no quotation marks and is a number, so it's an integer or int type, WE CANNOT add two different types, so this cannot be valid, and print("B") cannot execute BECAUSE the statement;

result = "test" + 5

will give out a TYPE ERROR. so print("B") is not valid, hence no output. But if we look at the next step there is an exception, meaning there is some error do this;

try:
    result = "test" + 5         # This gives us a Type error because "test" is a string type and 5 is an int type
    print("B")
except ValueError:   # print("C") will execute if there is a Value Error, and in this case we have a type error not ValueError                                  
    print("C")
except TypeError:     # Now there is the error that is our type of error the TYPE ERROR, so this line of code will execute.
    print("D")

Since we have a type error so print("D") will execute.

NOW, we have two outputs "A" and "D"

moving on to the next step;

else:
    print("E")  # This line of code will only execute IF nothing is executed in the try block and no errors were there.

Since we had a Type error, the Else line of code won't execute, So print("E") won't execute.

Now for our last step;

print("F") # This code will execute directly because it has no try block, no conditions or anything.
# output gives us "F"

print("F") will give us "F".

So now we have all the answers

"A" "D" "F"

I hope you got some idea.

Thanks so much, boi. This breakdown that you provided really helped clear it up for me. I fully understand now :)

boi
boi
14,241 Points

Glad it helped, You could mark the answers as "Best Answer" to help others be aware that the question was answered.