gpt4 book ai didi

python - 在我的 Python 应用程序中找不到名称

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:19 26 4
gpt4 key购买 nike

keepProgramRunning = True

while keepProgramRunning:
print "Welcome to the Calculator!"
print "Please choose what you'd like to do:"

print "0: Addition"
print "1: Subtraction"
print "2: Multiplication"
print "3: Division"


#Capture the menu choice.
choice = raw_input()

#Capture the numbers you want to work with.
numberA = raw_input("Enter your first number: ")
numberB = raw_input("Enter your second number: ")

if choice == "0":
print "Your result is:"
print Addition(numberA, numberB)
elif choice == "1":
print "Your result is:"
print Subtraction(numberA, numberB)
elif choice == "2":
print "Your result is:"
print Multiplication(numberA, numberB)
elif choice == "3":
print "Your result is:"
print Division(numberA, numberB)
else:
print "Please choose a valid option."



def Addition(a, b):
return a + b

def Subtraction(a, b):
return a - b

def Multiplication(a, b):
return a * b

def Division(a, b):
return a / b

这是错误:

Traceback (most recent call last):
File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\Tutorials\src\tutorials.py", line 23, in <module>
print Addition(numberA, numberB)
NameError: name 'Addition' is not defined

感谢您的帮助!

附言。我意识到循环永远不会结束,我还没有添加菜单选项。 :P

最佳答案

您需要在调用函数之前定义它们。

当解释器读取调用 Addition() 的行时,它还没有到达定义 Addition() 的行。因此它会抛出异常。

关于python - 在我的 Python 应用程序中找不到名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com