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

Ladislav Szep
Ladislav Szep
514 Points

What would the output be of the following code?

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

I don't understand why A D F is printed out and also i don't understand what reason is there for result = 5 + 5, please somebody clarify it for me.

Ladislav Szep
Ladislav Szep
514 Points

never mind it prints out A B E F i know why because C and D are exceptions but what is actually the meaning of: result = 5 + 5 please?

3 Answers

Dear ladislav, Its great that you understood the concept

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

Its print ABEF only because there was no error and 2 integers. So that exceptions lines never ran. result = 5 + 5 its trying to add, like below: result = 5 + "5" you will get TypeError and You will get different output. Hope this help

Wait, you said you'll get a TypeError so why would B print if that was the case?

Ladislav Szep
Ladislav Szep
514 Points

yes it helped thanks, i played around with it but still kinda don't get what is that result = 5 + 5, I tried to put it there delete it from there put it in different line but still ran the code. is that result = 5 + 5 something like users input?

edit: I just realized I played with it and tried result = 5 + "5" it gave me an TypeError and printed out A D F also tried with result = 'text' + 5 or vise versa and it printed out TypeError so A D F. so kinda starting to understand probably now its just a matter of practice and getting it into content

:) sure. It really nice to listen that, you are playing with code and this is the only way to learn.

Brendan Berg-Jacobson
Brendan Berg-Jacobson
1,067 Points

Hello, I was wondering where you practiced your python coding. Did you just put it into your Workspace? I am still having trouble with the concepts being presented and would like to practice.