0

I'm trying to build a calculator, and I'm a bit stuck on how to overwrite the user input with a new value using the same variable. What I am wanting is when greeted at the menu, when the user enters "5" it prompts for a new input for the variables "num1" and "num2". I have a feeling this is extremely easy to do but for some reason i'm stuck.

I have tried the normal - num1 = int(input("Enter new first number: )) and so on within the correct elif, but I get:

UnboundLocalError: local variable 'num1' referenced before assignment

Here is my code:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))


def calculate():

    print('''The numbers you have selected to calculate are: 
    {}, and {} \n'''.format(num1, num2))

    menu = int(input(''' Main Menu:
    1. Addition
    2. Subtraction 
    3. Multiplication
    4. Division
    5. Enter new numbers
    6. Exit\n '''))

    if menu == 1:
        add = addition(num1, num2)
        print("{} + {} = {}".format(num1, num2, add))
    elif menu == 2:
        sub = subtract(num1, num2)
        print("{} - {} = {}".format(num1, num2, sub))
    elif menu == 3:
        multi = multiply(num1, num2)
        print("{} x {} = {}".format(num1, num2, multi))
    elif menu == 4:
        div = divide(num1, num2)
        print("{} / {} = {}".format(num1, num2, div))
    elif menu == 5:
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))
    elif menu == 6:
        print("Exiting...")
    else:
        print("You have not entered a valid input.")

    rerun()
1
  • i have a feeling there is no way to do this unless you do num1 and num2 as lists but just wondering if theres a way to do it without having to use lists Commented Jun 21, 2019 at 5:26

1 Answer 1

1

The problem with your code is that the num1 and num2 variables are not defined inside the function calculate. So, when you try to access num1 inside the function, it will throw an error.

def calculate():
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    print('''The numbers you have selected to calculate are: 
    {}, and {} \n'''.format(num1, num2))

    menu = int(input(''' Main Menu:
    1. Addition
    2. Subtraction 
    3. Multiplication
    4. Division
    5. Enter new numbers
    6. Exit\n '''))

    if menu == 1:
        add = addition(num1, num2)
        print("{} + {} = {}".format(num1, num2, add))
    elif menu == 2:
        sub = subtract(num1, num2)
        print("{} - {} = {}".format(num1, num2, sub))
    elif menu == 3:
        multi = multiply(num1, num2)
        print("{} x {} = {}".format(num1, num2, multi))
    elif menu == 4:
        div = divide(num1, num2)
        print("{} / {} = {}".format(num1, num2, div))
    elif menu == 5:
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))
    elif menu == 6:
        print("Exiting...")
    else:
        print("You have not entered a valid input.")

    rerun()

calculate()

You can also try and make the variables num1 and num2 global.

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))


def calculate():
    global num1, num2
    print('''The numbers you have selected to calculate are: 
    {}, and {} \n'''.format(num1, num2))

    menu = int(input(''' Main Menu:
    1. Addition
    2. Subtraction 
    3. Multiplication
    4. Division
    5. Enter new numbers
    6. Exit\n '''))

    if menu == 1:
        add = addition(num1, num2)
        print("{} + {} = {}".format(num1, num2, add))
    elif menu == 2:
        sub = subtract(num1, num2)
        print("{} - {} = {}".format(num1, num2, sub))
    elif menu == 3:
        multi = multiply(num1, num2)
        print("{} x {} = {}".format(num1, num2, multi))
    elif menu == 4:
        div = divide(num1, num2)
        print("{} / {} = {}".format(num1, num2, div))
    elif menu == 5:
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))
    elif menu == 6:
        print("Exiting...")
    else:
        print("You have not entered a valid input.")

    rerun()

calculate()

You could also pass the variables as parameters to the function calculate

num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: "))

def calculate(num1, num2):
    print('''The numbers you have selected to calculate are: 
    {}, and {} \n'''.format(num1, num2))

    menu = int(input(''' Main Menu:
    1. Addition
    2. Subtraction 
    3. Multiplication
    4. Division
    5. Enter new numbers
    6. Exit\n '''))

    if menu == 1:
        add = addition(num1, num2)
        print("{} + {} = {}".format(num1, num2, add))
    elif menu == 2:
        sub = subtract(num1, num2)
        print("{} - {} = {}".format(num1, num2, sub))
    elif menu == 3:
        multi = multiply(num1, num2)
        print("{} x {} = {}".format(num1, num2, multi))
    elif menu == 4:
        div = divide(num1, num2)
        print("{} / {} = {}".format(num1, num2, div))
    elif menu == 5:
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))
    elif menu == 6:
        print("Exiting...")
    else:
        print("You have not entered a valid input.")

    rerun()

calculate(num1, num2)
Sign up to request clarification or add additional context in comments.

1 Comment

Had no clue something like "global" existed - it worked perfectly! thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.