首页 > 解决方案 > 如何在 python 3 中将菜单连接在一起并在它们之间导航?

问题描述

我对python比较陌生。

波纹管是我一直在处理的代码的一部分目前对我来说发生的主要问题是我无法在主菜单和我创建的其他菜单之间导航。当我尝试它时,它只会进入下一个菜单,直到它完成程序。

def main():
    menuSelect = ""
    print("\tPersonal Date, Gadget Inventory, Exit")

#main menu
print ("\n\t\tWelcome Admin!")
print("\n\t\tMain Menu")
print("\t1. Personal Data")
print("\t2. Gadget Inventory")
print("\t3. Quit")

menuSelect = int(input("\nPlease select one of the Three options "))

while menuSelect < 1 or menuSelect > 3:
    print("The selection provided is invalid.")
    menuSelect = int(input("\nPlease select one of the three options "))

    if menuSelect == 1:
        personalData()
    elif menuSelect == 2:
        gadgetInventory()
    elif menuSelect == 3:
        quit()


#personal data Menu
def personalData():
    menuSelect == ""
    print("\tCreate New, Current Users, Salary Calculator, Main Menu")
    
#personal data menu
print ("\n\t\tHello Admin!")
print("\n\t\tPersonal Data")
print("\t1. Create New")
print("\t2. Current Users")
print("\t3. Salary Calculator")
print("\t4. Main Menu")
menuSelect = int(input("\nPlease select one of the four options "))

while menuSelect < 1 or menuSelect > 4:
    print("The selection provided is invalid.")
    menuSelect = int(input("\nPlease select one of the four options "))

    if menuSelect == 1:
        createNew()
    elif menuSelect == 2:
        currentUsers()
    elif menuSelect == 3:
        salaryCalculator()
    elif menuSelect == 4:
        main()

#quit menu
def quit():
    again = ""

    if again=="yes" or again=="y":
        input('Press ENTER to exit')    
    else:
        main()
    
    
main()

标签: python-3.xmenu

解决方案


我快速查看了您的代码,您的思路是正确的,但我认为结构不同步,例如第一次选择 3 不会退出程序。您是否尝试过在像 Thonny 这样的 IDE 中单步执行程序,它会向您展示您的程序是如何分支的。我没有尝试为您解决问题,因为您可以做到,您只需要执行一些伪代码来确定您希望程序如何通过其操作进行流程。


推荐阅读