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()