python第七天

时间:2021-09-06
本文章向大家介绍python第七天,主要包括python第七天使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

三级登陆菜单

city = {
    "湖北省": {
        "武汉市":{
            "江岸区":{
                "360":{...}},
            "江汉区":{
                "小米":{...}},
            "硚口区":{
                "百度":{...}},
             },
        "宜昌市":{
            "西陵区":{
                "京东":{...}},
            "伍家岗区":{
                "淘宝":{...}},
         }
    },
    "湖南省": {
        "长沙市": {
            "芙蓉区":{
                "中关村":{...},
                 "谷歌":{...}}
        },
            "荷塘区":{
                "微软":{...},
               "阿里巴巴":{...}},
            }
    }
back_flag=0
while back_flag == False:
    
    choice=input("请输入:").strip()
    if choice  in  city: #判断输入的字符在是否在city的首层键里面
          while back_flag == False:
                for key2  in city[choice]:
                   print(key2)              #循环取出第二层--市
                choice2=input("请输入:").strip()
                if choice2 in  city[choice]:
                    while back_flag == False:
                       for key3 in city[choice][choice2]:
                            print(key3)          #循环取出第三层--区县
                       choice3 = input("请输入:").strip()
                       if choice3 in city[choice][choice2]:
                               while back_flag == False:
                                 for key4 in city[choice][choice2][choice3]:
                                   print(key4)       #循环取出第四层--企业
                                 choice4 = input("请输入:").strip()
                                 if  choice4=="b":
                                     back_flag = True
                                 elif  choice4 in city[choice][choice2][choice3]:
                                    while back_flag == False:
                                       for key5 in city[choice][choice2][choice3][choice4]:
                                          print(key5,"----这是最后一层")   #循环取出第五层--企业内容为空
                                       choice5 = input("请输入:").strip()
                                       if choice5=="b":
                                           back_flag = True
                                    else:
                                        back_flag = False
                               else:
                                   back_flag = False
                       elif choice3 == "b":
                                back_flag = True
                       else:
                                   print("你输入选项的未在菜单中,请重新输入")
                    else:
                         back_flag = False
                elif choice2 == "b":
                   back_flag = True
                else:
                       print("你输入选项的未在菜单中,请重新输入")
          else:
                back_flag = False
    elif choice=="b":
        back_flag =True
    else:
            print("你输入选项的未在菜单中,请重新输入")
View Code

三级登陆菜单  进阶版 

city = {
    "湖北省": {
        "武汉市":{
            "江岸区":{
                "360":{...}},
            "江汉区":{
                "小米":{...}},
            "硚口区":{
                "百度":{...}},
             },
        "宜昌市":{
            "西陵区":{
                "京东":{...}},
            "伍家岗区":{
                "淘宝":{...}},
         }
    },
    "湖南省": {
        "长沙市": {
            "芙蓉区":{
                "中关村":{...},
                 "谷歌":{...}}
        },
            "荷塘区":{
                "微软":{...},
               "阿里巴巴":{...}},
            }
}
current_list=city      #定义一个变量 用于存放字典
p_list=[]              #定义一个列表,用于存放上父节点
while True:
    for  key  in  current_list:
        print(key)                     #循环取出current_list中的键
    choice=input("请输入:").strip()    #输入数据.strip() 去掉换行和回车符号
    if len(choice)==0:                 #判断输入的数据是否为空,
        continue                       #为空则结束本次循环,进入下一次循环
    if  choice  in current_list:       #如果输入的数据在字典中
        p_list.append(current_list)    #将当前一层的数据存到父列表中  list.append(x)x为要添加的值
        current_list=current_list[choice]   #current_list[choice]进入下一层子列表 ,
        # 将下一层子列表赋值给current_list,循环结束进入下一次循环,则在第一个for中循环遍历字列表中的数值
    elif choice =="q":                 #如果输入的数据为q
        break                          #则直接跳出循环
    elif  choice =="b":                #如果输入的数据为b
        if p_list:                     #如果p_list不为空,(p_list为空返回False,不为空返回True)
            current_list = p_list.pop()  #list.pop()删除列表最后一个数据,并返回(将保存的父列表赋值给current_list)
            #返回初始循环,打印父列表
        else:                          #为空
             break                     #结束循环
    else:                              #输入数据不在范围内
            print("无此项")             #打印无此项

  

原文地址:https://www.cnblogs.com/lxz151239/p/15235923.html