python作業5:多級菜單

來源:互聯網
上載者:User

標籤:list   列表   last   int   while迴圈   for   多個   返回   choice   

# 作業5:多級菜單
# ● 三級菜單,省、市、縣、公司,要求程式啟動後,允許使用者依次選擇進入各子功能表
# ● 可以在任意一級菜單返回上級菜單,也可以退出程式
# ● 所需新知識點:列表、字典
#
# 思路:
# 先創字典,字典三層嵌套
#逐層提取key欄位,
#先不管各種可能,把主程式寫好,即各種選擇都是好好的情況下能實現的功能,再補充各種可能
#這個地方其實是重點考察while迴圈裡面的continue和break
#出於程式簡化,沒有考慮輸入數字超範圍、輸入的不是數字、q、b三者以外的情況
dict1 ={        "省1":{            "市11":{                "縣111":{                    "公司1111:{}",                    "公司1112:{}"                },                "縣112":{                    "公司1121:{}",                    "公司1122:{}"                }            },            "市12":{                "縣121":{                    "公司1211:{}",                    "公司1212:{}"                },                "縣122":{                    "公司1221:{}",                    "公司1222:{}"                }            },            "市13":{                "縣131":{},"縣132":{}            }        },        "省2":{            "市21":{                "縣211":{},"縣212":{}            },            "市22":{                "縣221":{},"縣222":{}            },            "市23":{                "縣231":{},"縣232":{}            }        },        "省3":{            "市31":{                "縣311":{},"縣312":{}            },            "市32":{                "縣321":{},"縣222":{}            },            "市33":{                "縣331":{},"縣332":{}            }        }}judge = Truewhile judge:    list1 = list(dict1.keys())    list1.sort()    print("The level_1 menu:")    for index,item in enumerate(list1):        print(index,item)    choice1 = input("Please choose level_1:")    if choice1.isdigit():        choice1 = int(choice1)        choice_key1 = list1[choice1]        list2 = list(dict1.get(choice_key1))        list2.sort()        while judge:            print("The level_2 menu:")            for index, item in enumerate(list2):                print(index, item)            choice2 = input("Please choose level_2:")            if choice2.isdigit():                choice2 = int(choice2)                choice_key2 = list2[choice2]                list3 = list(dict1.get(choice_key1).get(choice_key2))                list3.sort()                while judge:                    print("The level_3 menu:")                    for index, item in enumerate(list3):                        print(index, item)                    choice3 = input("Please choose level_3:")                    if choice3.isdigit():                        choice3 = int(choice3)                        choice_key3 = list3[choice3]                        list4 = list(dict1.get(choice_key1).get(choice_key2).get(choice_key3))                        list4.sort()                        print("The level_4 menu:")                        for index, item in enumerate(list4):                            print(index, item)                        choice4 = input("It‘s the last level,Please come back:")                        if choice4 == "q":                            # 判斷位置空後,可以連續跳出多個迴圈                            judge = False                            # 這裡是跳出了本while迴圈的所有迴圈                            break                        elif choice4 == "b":                            # 這裡只是跳出了本while迴圈中的一次迴圈                            continue                    elif choice3 == "q":                        judge = False                        break                    elif choice3 == "b":                        break            elif choice2 == "q":                judge = False                break            elif choice2 == "b":                break    elif choice1 == "q" :#是q退出        judge = False    else:#不是q也不是數字,重新選        print("Please choose the number.")

 

python作業5:多級菜單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.