calculate()
函数的第24行或第10行上的问题。它说选择没有定义。
我尝试过更改与每个运算符相关的定义变量和整数变量。
def start():
print("Hey, I'm your calculator... \n")
num1 = float(input("Enter your first number: "))
num2 = float(input("Enter your second number: "))
print("Operator list: \n 1. Addition + \n 2. Subtraction - \n 3. Multiply * \n 4. Division / \n")
choice = int(input("Type the letter corresponding to your selection: "))
add = 1
subtract = 2
multiply = 3
divide = 4
def calculate():
if choice == add or 1:
print(num1 + num2)
elif choice == subtract or 2:
print(num1 - num2)
elif choice == multiply or 3:
print(num1 * num2)
elif choice == divide or 4:
print(num1 / num2)
else:
print("Oops! Please enter a valid operator: '+', '-', '*', or '/'")
start()
start()
calculate()
回溯(最近通话最近一次):
文件“ C:/Users/hkben/OneDrive/Desktop/PythonFiles/calculator.py”,第29行,在 计算() 计算中的文件“ C:/Users/hkben/OneDrive/Desktop/PythonFiles/calculator.py”,第16行 如果选择==添加或1: NameError:名称“ choice”未定义