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('输入有误,请重新输入')

 

Python之双色球选购和三级菜单问题的更多相关文章

  1. 老男孩python学习之作业二---三级菜单

    因为之前花力气完成了购物小程序的作业 现在做这个三级菜单简直是so easy!! 1.显示省级菜单 2.交互,提示用户输入要查看的省份(退出e) 2.1.用户正确输入列表中的省份 3.显示市级菜单 3 ...

  2. day17 python递归案例(二分查找,三级菜单)

    递归函数与三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {} ...

  3. (Python基础)最Low三级菜单

    #-*-coding:utf-8-*- #_author_: Keep #三级菜单 menu = { '中国':{ '广东省':{ '广州市':{ '海珠区':{}, '荔湾区':{}, '越秀区': ...

  4. Python初学者第十三天 三级菜单程序小作业

    13day 作业题目: 三级菜单 作业需求: 数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村' ...

  5. Python学习第二天-编写三级菜单

    编写三级菜单:1. 运行程序输出第一级菜单2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单3. 菜单数据保存在文件中4. 让用户选择是否要退出5. 有返回上一级菜单的功能 # Author: z ...

  6. python小程序--Three(三级菜单)

    #!/usr/bin/env python # _*_ coding:utf8 _*_ data = { "山东省":{ "滨州市":{"惠民县&qu ...

  7. Python小代码_4_省市区三级菜单

    menu = { "北京": { "朝阳区": { "三环到四环之间": {}, "四环到五环之间": {}, &quo ...

  8. python练习1 登录和三级菜单

    ,: username1 = input("请输入您的用户名:")# password1 = getpass.getpass("请输入您的密码:") passw ...

  9. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

随机推荐

  1. 踩坑录-mysql不允许远程连接(错误码:1130) Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“

    每次搭建mysql环境都会遇见同样的问题,在此分享一下踩坑笔录. 一.问题描述 安装成功后,本地直接链接远程mysql,默认为不允许远程访问,则客户端提示1130 - Host'xxx.xxx.xxx ...

  2. <a href="javascript:;"></a>

    有时会在网页a标签中看到这样的代码,比如: <a href="javascript:;">反选</a> 这是啥意思呢? 我们知道标签的 href属性用于指定 ...

  3. [Sqlite3] Sqlite Introduction

    Check whether you have sqlite3 installed: sqlite3 -version To create a new db: sqlite3 <filename. ...

  4. Gulp安装及使用

    測试环境 Mac:10.10.4 Gulp:3.9.0 时间:2015年08月15日18:07:08 安装Gulp sudo npm install --global gulp npm install ...

  5. HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)

    HDU - 3584 Cube Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  6. 用block变量来对字符数组对象进行排序

    <span style="font-size:18px;">降序排序</span> <span style="font-size:18px; ...

  7. iOS 多线程,ARC

    iOS自己创建的线程需要自己定时的创建autorelease pools,否则对象不能及时自动释放. 方法1是不对的,while中的对象会无法及时释放. 1:-(void)Thread{ @autor ...

  8. java 和 Android Base64加密

    Java8 Base64 Java 8 新特性 在Java 8中,Base64编码已经成为Java类库的标准. Java 8 内置了 Base64 编码的编码器和解码器. Base64工具类提供了一套 ...

  9. Semaphore and SemaphoreSlim

    https://msdn.microsoft.com/en-us/library/z6zx288a(v=vs.110).aspx The System.Threading.Semaphore clas ...

  10. iOS中打包.a静态库

    1.新建.a静态库工程 需要选择Static Library静态库工程模板新建工程,如下图: 新建静态库工程 实现需要打包的类,如下图: 实现需要打包的类 2.设置需要暴露的头文件 添加Headers ...