用新的用户输入覆盖以前的用户输入的变量

时间:2019-06-21 05:22:15

标签: python python-3.x

我正在尝试构建一个计算器,但是对于如何使用相同的变量用新值覆盖用户输入有些困惑。我要的是在菜单上打招呼,当用户输入“ 5”时,它提示输入变量“ num1”和“ num2”的新输入。我感觉这很容易做到,但是由于某种原因我被卡住了。

我尝试了正常的-num1 = int(input("Enter new first number: )),依此类推,但在正确的省略号内,但得到了:

  

UnboundLocalError:分配前已引用本地变量“ num1”

这是我的代码:

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 个答案:

答案 0 :(得分:1)

您的代码存在的问题是在函数num1中未定义num2calculate变量。因此,当您尝试访问函数内部的num1时,它将引发错误。

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

您还可以尝试使变量num1num2成为全局变量。

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

您还可以将变量作为参数传递给函数calculate

num1 = int(input(“输入第一个数字:”)) num2 = int(input(“输入第二个数字:”))

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)