Jul-25-2020, 09:32 AM
import random def add(): # defines sub program num1 = random.randint(5,20) # creates random number between 5 and 20 num2 = random.randint(5,20) # creates random number between 5 and 20 correct = num1 + num2 # defines correct answer print('What is ', num1, ' + ', num2, '?') # ask user to input their answer answer = int(input('Please enter your answer')) answers = (correct, answer) return answers # return the answers def sub(): num3 = random.randint(25,50) # creates random number between 25 and 50 num4 = random.randint(5,20) # creates random number between 5 and 20 correct = num3 - num4 print('What is ', num3, ' - ', num4, '?') # asks user to input their answer answer = int(input('Please enter your answer')) answers = (correct, answer) return answers # return the answers print('1) Addition') # Prints menu then asks user to choose option 1 or 2 print('2) Subtraction') num = int(input('Enter 1 or 2:')) if num == 1: # runs add() program if 1 is selected add() elif num == 2: # runs sub() program if 2 is selected sub() else: print('Error! You did not enter a 1 or a 2') # or prints this error message def check(correct,answer): # defines subprogram called check using previously returned variables if answer == correct: # gives this message if the correct answer was given print('Correct') else: # gives this message if an incorrect answer was given print("Incorrect, the correct answer is ", correct) check(correct,answer) # runs the check subprogramHi the above code gives me the following error message:
Error: check(correct,answer) # runs the check subprogram
NameError: name 'correct' is not defined
However I think that I have defined the variable correct in my code can anyone help? Thanks