1-2 三级菜单

 

  

1.需求

 
作业三:多级菜单
  • 三级菜单
  • 可依次选择进入各子菜单
  • 所需新知识点:列表、字典

2.个人思路

定义 database = {}

打印主菜单

用户输入选择  
   
    打印二级菜单
    
    用户输入
        
        打印三级菜单
        

每层输入q 退出程序

每层输入b返回上一层 (没有实现 )

  

2.1个人代码

data_base = {
    'shaanxi':{
        "xi'an":{
            'yantaqu':['dongyilu','dianzierlu','taibailu'],
            'weiyangqu':['ziwulu','dahualu'],
            'changanqu':['changanlu','jinhualu']
        },
        'xianyang':{},
        'weinan':{}
    },
    'jiangsu':{},
    'nanjing':{}
}

#循环打印字典,第一层
for i in data_base:
    print(i)

choice_1 = input("enter your choice >").strip()

if choice_1 in data_base:
    for i in data_base[choice_1]:
        print(' >',i)
    choice_2 = input("enter your choice >>").strip()

    if choice_2 in data_base[choice_1]:
        for i in data_base[choice_1][choice_2]:
            print('     >>',i)

        choice_3 = input("enter your choice >>>").strip()

        if choice_3 in data_base[choice_1][choice_2]:
            print('         >>>',data_base[choice_1][choice_2][choice_3])

        elif choice_3 == 'q':
            print("-------bye-bye------")
            exit()
        else:
            print("33[1mEntererror!!!33[0m")

    elif choice_2 == 'q':
            print("---------bye-bye------")
            exit()
    else:
        print("33[1mEntererror!!!33[0m")
elif choice_1 == 'q':
            print("------bye-bye--------")
            exit()
else:
    print("33[1mEntererror!!!33[0m")

2.2 小方法心得

1)打印字典

#打印完整字典
#print(data_base)

#print(data_base[choice_1])  #打印第一层
#print(data_base[choice_1][choice_2])  #打印第2层
# print(data_base[choice_1][choice_2][choice_3]  #打印第3层

  

2)循环打印字典

#循环打印字典,第一层
# for i in data_base:
#     print(i)

  

3)字体加粗

字体加粗

print("33[1mEntererror!!!33[0m")

4)break问题

a.错误实例

b.正确实例

.5)  elif 问题

如果需要检查多个条件,就可以使用 elif

Choice_3 》yanta     if
    》b        elif
    》q       elif
    》other     else

只能用一个 if 其余的全是 elif 就可以解决问题

 

  

3.完整代码

data_base = {
    'shaanxi':{
        "xi'an":{
            'yantaqu':['dongyilu','dianzierlu','taibailu'],
            'weiyangqu':['ziwulu','dahualu'],
            'changanqu':['changanlu','jinhualu']
        },
        'xianyang':{},
        'weinan':{}
    },
    'jiangsu':{},
    'nanjing':{}
}

while True:
    #循环打印字典,第一层
    for i in data_base:
        print(i)

    choice_1 = input("enter your choice >").strip()

    if choice_1 in data_base:
        while True:
            for i in data_base[choice_1]:
                print(' >',i)
            choice_2 = input("enter your choice >>").strip()

            if choice_2 in data_base[choice_1]:
                while True:
                    for i in data_base[choice_1][choice_2]:
                        print('     >>',i)

                    choice_3 = input("enter your choice >>>").strip()

                    if choice_3 in data_base[choice_1][choice_2]:
                        print('         >>>',data_base[choice_1][choice_2][choice_3])
                        choice_4 = input ('this is the bottom ,按b返回 >')
                        if choice_4 == 'b':
                            pass
                        elif choice_4 == 'q':
                            print("-------bye-bye------")
                            exit()
                        else:
                            print("33[1mEntererror!!!33[0m")
                    elif choice_3 == 'b':
                        break
                    elif choice_3 == 'q':
                        print("-------bye-bye------")
                        exit()
                    else:
                        print("33[1mEntererror!!!33[0m")
            elif choice_2 == 'b':
                break
            elif choice_2 == 'q':
                print("-------bye-bye------")
                exit()
            else:
                print("33[1mEntererror!!!33[0m")
    elif choice_1 == 'b':
            break
    elif choice_1 == 'q':
            print("-------bye-bye------")
            exit()
    else:
        print("33[1mEntererror!!!33[0m")

  

 

原文地址:https://www.cnblogs.com/venicid/p/7241358.html