Back

Exceptions challenges

Try the following challenges:

Q1. Predict before running the code exactly what will be printed out when the following code is run:

a = 5
b = 10
c = 20
print("A")
print(a)
try:
    print("B")
    a = b + c
    print("b + c")
except:
    print("D")
print("X")
print(c)

Q2. Now run the code in Python to see if you were correct.
Q3. Predict before running the code exactly what will be printed out when the following code is run:

temp = 5
value = 10
print("temp")
print(value)
try:
    print("value")
    newTemp = temp + value
    print("temp + value")
except:
    print("temp")
print("value")
print(temp+value)

Q4. Now run the code in Python to see if you were correct.
Q5. Predict before running the code exactly what will be printed out when the following code is run:

stock = 5
reviews = 0
print(stock)
print(stock + reviews)
try:
    print("value")
    newValue = stock / reviews
    print(newValue)
except:
    print("ReviewAgain")
print("reviews")
print(reviews)

Q6. Now run the code in Python to see if you were correct.
Q7. Predict before running the code exactly what will be printed out when the following code is run:

alpha = ten
beta = twenty
print(alpha)
print("twenty + ten")
try:
    print("beta")
    gamma = "ten" + "twenty"
    print(gamma)
    delta = alpha + alph
    print(alpha)
except:
    print("theta")
    print("zeta")
print(beta)
print(alpha)

Q8. Now run the code in Python to see if you were correct.
Q9. Predict before running the code exactly what will be printed out when the following code is run:

alpha = "five"
beta = "six"
print(alpha)
print("five + five")
try:
    print("beta")
    gamma = "five" + "six"
    print(gamma)
    delta = alpha + alph
    print(alpha)
except:
    print("theta")
    print("beta")
print(beta)
print(alpha)

Q10. Now run the code in Python to see if you were correct.

Back