-1

While running the code below, I'm getting this output--1) Addition 2) Subtraction Enter 1 or 21 First number: 17 Second number: 13 Add two random numbers10

Traceback (most recent call last):
  File "C:/Users/rafis/PycharmProjects/pythonProject1/Problem120.py", line 51, in <module>
    main()
  File "C:/Users/rafis/PycharmProjects/pythonProject1/Problem120.py", line 43, in main
    verify(r_ans, user_adds)
NameError: name 'r_ans' is not defined

Process finished with exit code 1 ............................... But I can't seem to find any error in the code. How do I "define" the variable? TIA

import random

menu1 = input("1) Addition")
menu2 = input("2) Subtraction")
choose = int(input("Enter 1 or 2"))



def random_generator():
    r_1 = random.randint(4, 19)
    r_2 = random.randint(4, 19)
    print("First number: " + str(r_1))
    print("Second number: " + str(r_2))
    user_adds = int(input("Add two random numbers"))
    r_ans = r_1 + r_2
    return r_ans, user_adds




def random_generator3():
    r_1 = random.randint(24, 49)
    r_2 = random.randint(2, 24)
    print("First number: " + str(rando_1))
    print("Second number: " + str(rando_2))
    r_ans = rando_1 - rando_2
    user_adds = int(input("Work out random number 1- random number 2: "))
    return r_ans, user_adds




def verify(r_ans, user_adds):
    if r_ans == user_adds:
        print("Correct")
    else:
        print("Incorrect. The answer is: " + str(r_ans))


def main():
    if choose == 1:
        random_generator()
        verify(r_ans, user_adds)
    elif choose == 2:
        random_generator3()
        verify(r_ans, user_adds)
    else:
        print("Invalid input")


main()
Justin Ezequiel
  • 4,686
  • 2
  • 10
  • 13
  • Welcome to Stack Overflow. You have to assign the function return values to a local variable, exactly as shown [in this answer](https://stackoverflow.com/a/354929). – bad_coder Feb 07 '21 at 07:09

1 Answers1

1

In your main function, you have to assign the returns of each function:

r_ans, user_adds = random_generator()
r_ans, user_adds = random_generator3()
goalie1998
  • 1,283
  • 1
  • 8
  • 14