如何使用print语句创建菜单?我希望能够选择一个数字,并根据我选择的数字运行该功能

时间:2017-03-26 21:47:12

标签: python function menu

evicted

我希望这是一个输入语句,当我输入1时,它将打印1234,因为我在主程序中编码...帮助?为什么当我输入1或2或3时它除了打印“结束”之外什么都没做..?救命。     main()的

2 个答案:

答案 0 :(得分:1)

您的行option=displaymenu()会将option设置为None,因为您在函数末尾没有返回语句。 将以下行添加到您的函数中,它应该可以工作:

return int(option1)

答案 1 :(得分:0)

您没有从displaymeny返回选项1。相反,您试图在主函数中访问它:

 while option1 != 4:

这也意味着main函数中的选项没有任何vaule。

将您的计划更改为:

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循环内的displaymeny。如果你在外面,你只能获得一次菜单。