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 trialIvana Bojcic
Python Development Techdegree Student 892 PointsException Flow Quiz - frustrated
This is the first quiz where I just cannot get things right. Someone care to explain it to me? Is this question text maybe missing something? How can I "trace the print output" if I cannot see what is being typed and which except error is shown? :( I suppose I should read out something from result = "test" + 5 but I cannot figure out what. This is the point where I'm beginning to understand I'm not fit for programming. :((
Trace the print output of the following code:
print("A") try: result = "test" + 5 print("B") except ValueError: print("C") except TypeError: print("D") else: print("E") print("F")
2 Answers
Steven Parker
231,248 PointsLearning programming is more a matter of persistence than innate ability.
This exercise is about learning to look at the code and predict what it will do:
print("A") # you know this will print the letter "A"
try: # this will cause the program flow to change based on errors
result = "test" + 5 # adding a number to a string will cause a TypeError
print("B") # so this line will get skipped
except ValueError: # this code is for a different kind of error, also skipped
print("C") # so this line will get skipped as well
except TypeError: # here's where instructions will resume
print("D") # now the letter "D" will be printed
else: # this code is only for when there is NO error
print("E") # so this line will get skipped
print("F") # now the letter "F" will be printed
So this code will print "A",'D", and "F", each on a separate line.
Steven Parker
231,248 PointsYou're right that if the code is changed so there is no error then "B" will be printed. And "E" will also be printed.
And you are also right that lines that are not indented are not related to the "try" block. This guarantees that "A" will be printed first and "F" will be printed last regardless of what happens in the middle. I would assume this is the intentional design of the program and not that either line is "standing there randomly".
Ivana Bojcic
Python Development Techdegree Student 892 PointsThanks, Steven. Hypothetically, let's say there's no error.
If the result is, for example:
result = "test" + "test2"
then why doesn't it print("B")? Because, you say for else: "this code is only for when there is NO error"
If there is no error, wouldn't the solution be print("B")?
Extra question: I see that print("F") is not indented. It seems like it has nothing to do with try, except and else. Just standing there randomly, am I right?
Thanks
Steven Parker
231,248 PointsSteven Parker
231,248 PointsIt's not clear what course and lesson you are referring to. You might want to take a look at this video about Posting a Question
Also make sure posted code is properly formatted. Take a look at this videos about using Markdown formatting to preserve the code's appearance. And for when your question refers to something you are doing in a workspace, you may also want to view this one about sharing a snapshot of your workspace.