numA = [4, 1, 6, 10, 2, 3, 7, 9, 11, 12, 5, 8]
numB = [2, 12, 10, 11, 1, 3, 7, 9, 4, 8, 5, 6]
operation = input("Are you adding or multiplying? (a/m)")
i = 0
if operation == "a":
while i < len(numA):
try:
answer = int(input("{} + {} = ".format(numA[i], numB[i])))
if answer == numA[i] + numB[i]:
print("You're correct!")
else:
print("You're wrong! The correct answer is {}".format(numA[i] + numB[i]))
i+=1
except ValueError:
print("Please enter a number!")
if operation == "m":
while i < len(numB):
try:
answer = int(input("{} * {} = ".format(numA[i], numB[i])))
if answer == numA[i]*numB[i]:
print("You're correct!")
else:
print("You're wrong! The correct answer is {}".format(numA[i]*numB[i]))
i+=1
except ValueError:
print("Please enter a number!")
The type of error handling we perform is ValueError handling. Instead of getting an exception when the user enters a letter when they try to answer a math problem, we tell the user to enter a number.
I hope this helps!