Python Python Basics Functions and Looping Exception Flow

Andres Deidan
Andres Deidan
279 Points

Hello! I am very confused on how this code would run to output the letters. Can someone guide me step by step? Thanks!

Please guide me step by step as I am confused and stuck on this currently. Thanks!

Zimri Leijen
Zimri Leijen
6,955 Points

It would be a little easier to help if you'd link the challenge you're stuck with.

It's also good practice to at least make an attempt at solving it, and then showing your work and show where you got stuck.

1 Answer

Zimri Leijen
Zimri Leijen
6,955 Points

Assuming you mean this:

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")

It would go line by line.

First, it does `Print("A")

then it will

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

since there are no errors in this code, this code will run successfully it will therefore execute print("B")

it will skip the except statements because neither of those errors have been encountered, so it will resume at the else: block

inside the else block there's print("E")

finally there's a print("F") command, that isn't in any of the blocks so that will always run.

hope that helps.

Andres Deidan
Andres Deidan
279 Points

Thank you very much! Understand it thoroughly now.