简体   繁体   中英

“ during the handling of above exception, another exception occurred ” for the first input in the for loop

when I wrote "int" in front of input in for loop the try/except block I wrote for non-integer inputs traceback error " during the handling of the above exception, another exception occurred " is displayed. However, on all of the other inputs in the range, the try/except block is recognized and prints the error message I wrote.

CODE

QLEN = 11
MAX_NUM = 12

for i in range(1,QLEN):
  int2 = (random.randint(1,MAX_NUM))
  int1 = (random.randint(1,MAX_NUM))
  print()
  print("Question {}".format(i))
  try:
    ans = int(input("{} + {}: ".format(int1,int2)))
    add(int1,int2,ans)
  except ValueError:
    add(int1,int2,ans)

"ADD" FUNCTION

def add(a,b,c):
  if a + b != c:
    print("Incorrect")
    print("Correct Answer Was {}".format(a + b))
    return (a + b)
    print()
  else:
    print("Correct")

ERROR

Traceback (most recent call last):
  File "main.py", line 125, in <module>
    ans = int(input("{} + {}: ".format(int1,int2)))
ValueError: invalid literal for int() with base 10: 'e'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 128, in <module>
    add(int1,int2,ans)
NameError: name 'ans' is not defined

You tried to execute the same fraudulent code that caused an exception in the try block in the except block. Also the python indentation convention is 4 spaces , not two. Your original code will produce an error message similar to this:

Traceback (most recent call last):
  File "/Users/eric/Desktop/ Python_Files/stackoverflow/0008.py", line 21, in <module>
    ans = int(input("{} + {}: ".format(int1,int2)))
ValueError: invalid literal for int() with base 10: 'ans'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eric/Desktop/ Python_Files/stackoverflow/0008.py", line 24, in <module>
    add(int1,int2,ans)
NameError: name 'ans' is not defined

Try using the code below (the except block is different)

import random

QLEN = 11
MAX_NUM = 12

def add(a,b,c):
  if a + b != c:
    print("Incorrect")
    print("Correct Answer Was {}".format(a + b))
    return (a + b)
    print()
  else:
    print("Correct")

for i in range(1,QLEN):
  int2 = (random.randint(1,MAX_NUM))
  int1 = (random.randint(1,MAX_NUM))
  print()
  print("Question {}".format(i))
  try:
    ans = int(input("{} + {}: ".format(int1,int2)))
    add(int1,int2,ans)
  except ValueError:
    print("You answered with something that wasn't an integer!")

Replacing the except block with anything else, like, in this case a notification that you didn't answer in the correct format, should work.

Here is a sample output:

Question 1
9 + 10: 21
Incorrect
Correct Answer Was 19

Question 2
2 + 1: 3
Correct

Question 3
8 + 7: 15
Correct

Question 4
8 + 12: LOL
You answered with something that wasn't an integer!

int failed with ValueError because it could not parse something which was not an integer. That caused it to trigger the except part, which called add using ans . However, ans is an unknown variable because it was never assigned a value ( int failed), causing another exception during the handling of the previous one .

Unlike some languages, in Python if you do not assign a value there will not be any "default" value.

Here is the corrected snippet:

print("Question {}".format(i))
try:
    ans = int(input("{} + {}: ".format(int1,int2)))
except ValueError:
    print("Bad input!")
    continue

add(int1,int2,ans)

What you expect to fail is int , so you surround it with try/except . In case it failed, you continue and thus skip the add call which is later in the loop. If there were no exceptions, then you have a good ans and you can call add with all integer arguments.

Also, print() after return (a+b) can never be executed because return will leave the function. Moreover, you do not need outer parenthesis in expressions like int2 = (random.randint(1,MAX_NUM)) as they only add to the clutter and might be confused for a tuple with one element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM