zoukankan      html  css  js  c++  java
  • 练习流程控制

    1.猜年龄游戏

    给定年龄,用户可以猜三次年龄
    
    年龄猜对,让用户选择两次奖励
    
    用户选择两次奖励后可以退出
    
    age = 18
    count = 0
    prize_dict = {0:'布娃娃',1:'变形金刚',2:'高达',3:'零食大礼包',4:'<墨菲定律>'}
    while count <3:
        inp_age = input('请输入您的名字: ')
        if not inp_age.isdigit():  #判断输入是否为数字.isdigit---字符串的内置函数
            print('傻逼,你的年龄输入错误!!')
            continue
        inp_age_int = int(inp_age)
        if inp_age_int == age :
            print('你太聪明啦!!猜对啦!')
            print(prize_dict)
    
            for i in range(2):
                prize_choice = input('请输入你想要的奖品,如果不想要,则输入"n"退出!!!')
    
                if prize_dict != 'n':
                    print(f'恭喜你获得奖品: {prize_dict[int(prize_choice)]}')
                else:
                    break
            break
        elif inp_age_int < age:
            print('抱歉!你离我们的奖品还有一点距离!猜大一点,奖品就是你的啦!!')
        else:
            print('太可惜啦!!猜小一点,奖品在等待你的召唤!!')
        count+=1
        if count !=3:
            continue
        again_choice = input('是否继续游戏,继续请输入"Y",否则按任意键退出')
    
        if again_choice == "Y":
            count =  0
    
    

    2.三级菜单

    打印省、市、县三级菜单
    
    可返回上一级
    
    可随时退出程序
    
    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车站': {
                    '携程': {}
                }
            },
            '浦东': {
                '外滩' : {
                    '和平饭店' : {}
                }
            },
        },
        '山东': {},
    }
    layers ={
        menu,
    }
    while True:
        current_layer = layers[-1]
        for key in current_layer:
            print(key)
        choice = input('>>: ').strip()
        if choice == 'q':
            break
        if choice not in current_layer:
            continue
        layers.append(current_layer[choice])
    
    
    #-------改进版(加入退出机制) 
    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车站': {
                    '携程': {}
                }
            },
            '浦东': {
                '外滩' : {
                    '和平饭店' : {}
                }
            },
        },
        '山东': {},
    }
    layers = {
        menus,
    }
    while True:
        if len(layers) == 0:
            break
        current_layer = layers[-1]
        for key in current_layer:
            print(key)
        choice = input('>>: ').strip()
        if choice == ' b':
            layers.pop(-1)
            continue
        if choice == 'q':
            break
        if choice not in current_layer:
            continue
        layers.append(current_layer[choice]
    
    
    
  • 相关阅读:
    linux中mysql表名默认区分大小写导致表找不到的问题
    将hive的hql执行结果保存到变量中
    excel导出功能优化
    shell中执行hive命令错误:delimited by end-of-file (wanted `EOF')
    javascript中全局变量的定义
    datagrid中reoload提交时如何批量提交表单中的查询条件
    Linux下查看文件和文件夹大小
    Linux 时间修改--date -s命令
    Unicode字符集,各个语言的区间
    js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11514726.html
Copyright © 2011-2022 走看看