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

Can someone help explain this script? I am confused about the logic here.

How do you interpret this script?

Which script?

2 Answers

rydavim
rydavim
18,814 Points

Think about what would happen if you ran the provided code. What letters get printed? Walk through each line step by step and try to determine what it's doing. Which blocks get triggered? Are any errors raised? If so, what gets printed? If no errors are raised, what line do you run?

# Question 1
print("A") # does this print?
try: # walk through running this block
    result = 5 + 5
    print("B")
except ValueError: # were any value errors raised above?
    print("C")
except TypeError: # were any type errors raised above?
    print("D")
else: # were any errors raise above?
    print("E")
print("F") # does this print?

You should end up with a sequence of letters that matches one of the multiple choice answers. If you're still stuck, let me know and we can go over any questions you still have. Good luck, and happy coding!

Thank you! This script I understood for the most part. It was the second script I had trouble interpreting. Can you see the second script (i.e., question 2)? If not, I can find it and paste it in here.

Edit: The script is below. The question is "What would the output be?" print("A") try: result = "test" + 5 print("B") except ValueError: print("C") except TypeError: print("D") else: print("E") print("F")

rydavim
rydavim
18,814 Points

vand 123

Hint - it has to do with what errors you might be raising. The code has a deliberate "wrong" thing in it that causes something unexpected to happen. This changes what gets printed when compared to Question 1.

print("A")
try:
    result = "test" + 5 # What happens when you run this line?
    print("B")
except ValueError: # Would you trigger this error?
    print("C")
except TypeError: # What about this one?
    print("D")
else: # This would get run if you have no errors. Are you still error-free?
    print("E")
print("F")

Remember, if you get really stuck you can always try running the code yourself to see what happens. But let me know if it still doesn't make sense to you and we can talk through it. :)

Makes sense! Thank you for your help!