def main():
option=displaymenu()
print(option)
while option1 != 4:
if option1 ==1:
print("1234")
elif option1==2:
print("1")
elif option1==3:
print("joe")
else:
print("end")
def displaymenu():
print("choose one of the following options")
print("1.Calculate x to the power of N") << this is supposed to be
print("2.Calculate factorial of N")
print("3.Calculate EXP")
print("4.Exit")
option1=input("Please enter your choice here:")<<< i want these print statements to act as a menu? how do I make it so when I input a number 1-4 It does this operation I input ?
我希望这是一个输入语句,当我输入 1 时,它将打印 1234,因为我在我的主程序中编码......帮助?为什么当我输入 1 或 2 或 3 时,它除了打印"结束"之外什么都不做..?帮助。 主((
您的行option=displaymenu()
会将option
设置为 None
,因为您在函数末尾没有 return 语句。将以下行添加到您的函数中,它应该可以工作:
return int(option1)
您不会从显示中返回选项 1。相反,您正在尝试在主函数中访问它:
while option1 != 4:
这也意味着主函数中的选项没有任何价值。
将您的程序更改为以下内容:
def main():
option = -1 # Value to get the while loop started
while option != 4:
option=displaymenu()
if option ==1:
print("1234")
elif option==2:
print("1")
elif option==3:
print("joe")
else:
print("end")
def displaymenu():
print("choose one of the following options")
print("1.Calculate x to the power of N") << this is supposed to be
print("2.Calculate factorial of N")
print("3.Calculate EXP")
print("4.Exit")
return input("Please enter your choice here:")<<< i want these print statements to act as a menu? how do I make it so when I input a number 1-4 It does this operation I input ?
另请注意,我将调用移动到 while 循环中的显示。如果你在外面,你只会得到一次菜单。