Python之双色球选购和三级菜单问题

发布时间:2019-06-01 20:40:55编辑:auto阅读(2038)

    1:双色球选购
    # 1 双色球(假设一共八个球,6个红球,球号1-32、2个蓝球,球号1-16)
    # 2 确保用户不能重复选择,不能超出范围
    # 3 用户输入有误时有相应的错误提示
    # 4 最后展示用户选择的双色球的号码

    select_red_ball = []
    while True:
        n = int(input('请输入你要选择的红色球(1-32):'))
        if 0 < n < 33:
            if n not in select_red_ball:
                select_red_ball.append(n)
            else:
                print('number %d is already exist in red ball list' % n)
        else:
            print('only can select n between 1-32')
        if len(select_red_ball) == 6:
            break
    select_red_ball.sort()
    select_blue_ball = []
    while True:
        n = int(input('请输入你要选择的蓝色球(1-32):'))
        if 0 < n < 17:
            if n not in select_blue_ball:
                select_blue_ball.append(n)
            else:
                print('number %d is already exist in blue ball list' % n)
        else:
            print('only can select n between 1-16')
        if len(select_blue_ball) == 2:
            break
    select_blue_ball.sort()
    print('red ball %d' % select_red_ball)
    print('blue ball %d' % select_blue_ball)
     
     

    2 :三级菜单

    • 数据结构:
      menu = {
          '北京':{
              '海淀':{
                  '五道口':{
                      'soho':{},
                      '网易':{},
                      'google':{}
                  },
                  '中关村':{
                      '爱奇艺':{},
                      '汽车之家':{},
                      'youku':{},
                  },
                  '上地':{
                      '百度':{},
                  },
              },
              '昌平':{
                  '沙河':{
                      '老男孩':{},
                      '北航':{},
                  },
                  '天通苑':{},
                  '回龙观':{},
              },
              '朝阳':{},
              '东城':{},
          },
          '上海':{
              '闵行':{
                  "人民广场":{
                      '炸鸡店':{}
                  }
              },
              '闸北':{
                  '火车站':{
                      '携程':{}
                  }
              },
              '浦东':{},
          },
          '山东':{},
      }
      
      需求:
      可依次选择进入各子菜单
      可从任意一层往回退到上一层
      可从任意一层退出程序
      所需新知识点:列表、字典

    代码一:

     exit_flag = False
    while not exit_flag:
        for i in menu:
            print(i)
        choice1 = input("选择进入1>>:")
        if choice1 in menu:
            while not exit_flag:
                for i2 in menu[choice1]:
                    print("\t", i2)
                choice2 = input("选择进入2>>:")
                if choice2 in menu[choice1]:
                    while not exit_flag:
                        for i3 in menu[choice1][choice2]:
                            print("\t\t", i3)
                        choice3 = input("选择进入3>>:")
                        if choice3 in menu[choice1][choice2]:
                            for i4 in menu[choice1][choice2][choice3]:
                                print("\t\t", i4)
                            choice4 = input("最后一层,按b返回>>:")
                            if choice4 == "b":
                                pass
                            elif choice4 == "q":
                                exit_flag = True
                        if choice3 == "b":
                            break
                        elif choice3 == "q":
                            exit_flag = True
                if choice2 == "b":
                    break
                elif choice2 == "q":
                    exit_flag = True

    代码二:(优化版本,代码缩减为15行)

    current_layer = menu
    parent_layer = []  # 将父级key值放入到列表中
    flags = False  # 设置标志位
    while not flags:
        for key in current_layer:
            print(key)
        choose = input("请选择,输入b返回上一级菜单,输入q退出菜单:").strip()  # 去除空格
        if choose in current_layer:
            parent_layer.append(current_layer)  # 将当前的状态放入列表中
            current_layer = current_layer[choose]
        elif choose == 'b':
            if parent_layer:
                current_layer = parent_layer.pop()
        elif choose == 'q':
            flags = True
        else:
            print('输入有误,请重新输入')

     

     

关键字