簡體   English   中英

如何使用打印語句創建菜單? 我希望能夠選擇一個數字,並根據我選擇的數字運行該功能

[英]How do i create a menu using print statements? I want to be able to choose a number and based on what number i choose run that function

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時,它只打印“ end” ..? 救命。 主要()

您的行option=displaymenu()會將option設置為None因為您在函數末尾沒有return語句。 將以下行添加到您的函數中,它應該可以工作:

return int(option1)

您不會從displaymeny返回option1。 相反,您嘗試在main函數中訪問它:

 while option1 != 4:

這也意味着main函數中的選項沒有任何作用。

將程序更改為此:

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。 如果您不在外面,則只會獲得一次菜單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM