新手小白 python之路 Day1 (三級菜單功能實現)

來源:互聯網
上載者:User

標籤:ict   實現   多級   珠海   cond   函數   exce   擷取   三級菜單   

直接上需求:

        實現一個多級菜單

    三級菜單的實現
    可以依次選擇進入各個子功能表
    也可以返回上級菜單
    所需知識 列表 字典

需求也比較簡單,但實際上做起來還是遇到許多的問題,我這邊主要用到的知識點大概就是  字典 等

大致的思路 我是分別寫了四個函數

Abnormal() 通過拋出異常來判斷輸入的編號是否合法
Province() 擷取省級菜單
City() 擷取市級菜單
Area() 擷取區級菜單
來實現整個功能的,話不多說直接上代碼了
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: linghanchujian"""多級菜單  三級菜單的實現  可以依次選擇進入各個子功能表  也可以返回上級菜單  所需知識 列表 字典"""""" 三級菜單字典"""DictionaryMenu = {    ‘山東‘ : {         ‘青島‘ : [‘四方‘,‘黃島‘,‘嶗山‘,‘李滄‘,‘城陽‘],         ‘濟南‘ : [‘曆城‘,‘槐蔭‘,‘高新‘,‘長青‘,‘章丘‘],         ‘煙台‘ : [‘龍口‘,‘萊山‘,‘牟平‘,‘蓬萊‘,‘招遠‘]     },    ‘安徽‘ : {        ‘合肥‘ : [‘蜀山‘,‘廬陽‘,‘包河‘,‘經開‘,‘新站‘],         ‘蕪湖‘ : [‘鏡湖‘,‘鳩江‘,‘無為‘,‘三山‘,‘南陵‘],        ‘蚌埠‘ : [‘蚌山‘,‘龍子湖‘,‘淮上‘,‘懷遠‘,‘固鎮‘]     },    ‘廣東‘ : {         ‘深圳‘ : [‘羅湖‘,‘福田‘,‘南山‘,‘寶安‘,‘布吉‘],         ‘廣州‘ : [‘天河‘,‘珠海‘,‘越秀‘,‘白雲‘,‘黃埔‘],         ‘東莞‘ : [‘莞城‘,‘長安‘,‘虎門‘,‘萬江‘,‘大朗‘]     }}"""通過拋出異常來判斷輸入的編號是否合法"""def Abnormal(Num):    try:        int(Num)        return True    except ValueError:        return False"""擷取省級"""def Province():    for i,j in enumerate (DictionaryMenu):        print(str(i+1)+"、"+j)    LevelOneOptions = input("請選擇上面一級菜單列表的序號(n 退出): ")    if LevelOneOptions == ‘n‘:        exit()    else:        if Abnormal(LevelOneOptions):            OneOptions = int(LevelOneOptions)            # print(LevelOneCount+1)            if OneOptions>0 and OneOptions<(i+2):                City(OneOptions-1)            else:                print("請輸入0~~"+str(i+2)+"之間的數")                print("---------------------------------------")                Province()        else:            print("編號不合法!!")            print("---------------------------------------")            Province()    pass"""擷取市級"""def City(Options):    for i,j in enumerate(DictionaryMenu):        if i == Options:            for n,k in enumerate(DictionaryMenu[j]):                print(str(n+1)+"、"+k)            SecondLevelOptions = input("請選擇上面二級菜單列表的序號(b/n 返回上級/退出): ")            if SecondLevelOptions == "n":                exit()            elif SecondLevelOptions == "b":                Province()                return ;            else:                if Abnormal(SecondLevelOptions):                    TwoOptions = int(SecondLevelOptions)                    # print(LevelOneCount+1)                    if TwoOptions > 0 and TwoOptions < (n + 2):                        Area(Options,TwoOptions - 1)                    else:                        print("請輸入0~~" + str(n + 2) + "之間的數")                        print("---------------------------------------")                        City(Options)                else:                    print("編號不合法!!")                    print("---------------------------------------")                    City(Options)    pass"""擷取區級"""def Area(Options,TwoOptions):    for i,j in enumerate(DictionaryMenu):        if i == Options:            for n,k in enumerate(DictionaryMenu[j]):                if n == TwoOptions:                    for c,v in enumerate(DictionaryMenu[j][k]):                        print(str(c+1)+"、"+v)                    ThreeOptions = input("上面就是三級菜單全部內容(b/n 返回上級/退出): ")                    if ThreeOptions == "b":                        City(Options)                        return ;                    else:                        exit()    passprint("歡迎來到三級菜單!!")Province()

 

新手小白 python之路 Day1 (三級菜單功能實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.