zoukankan      html  css  js  c++  java
  • python 实战之猜年纪游戏 练习。

    '''
    python 实战之猜年纪游戏。
    1.给定年龄,用户可以猜三次年龄

    2.年龄猜对,让用户选择两次奖励

    3.用户选择两次奖励后可以退出

    '''
    age = 20
    count = 0
    prize_dict = {0: '钢铁侠', 1: '变形金刚', 2: '美国队长', 3: '蜘蛛侠',4:'绿巨人'}
    while count < 3:
    inp_age = input('请输入你的年龄:')
    if not inp_age.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('请输入你想要的奖品,如果不想要,则输入"J"退出!!!')
            if prize_choice != 'J':
                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('是否继续游戏,继续请输入"Q",否则任意键直接退出.')
    if again_choice == 'Q':
        count =0
    

    2. 三级菜单
    """
    打印省、市、县三级菜单

    可返回上一级

    可随时退出程序
    """
    menu = {
    '北京': {
    '海淀': {
    '五道口': {
    'soho': {},
    '网易': {},
    'google': {}
    },
    '中关村': {
    '爱奇艺': {},
    '汽车之家': {},
    'youku': {},
    },
    '上地': {
    '百度': {},
    },
    },
    '昌平': {
    '沙河': {
    '老男孩': {},
    '北航': {},
    },
    '天通苑': {},
    '回龙观': {},
    },
    '朝阳': {},
    '东城': {},
    },
    '上海': {
    '闵行': {
    "人民广场": {
    '炸鸡店': {}
    }
    },
    '闸北': {
    '火车战': {
    '携程': {}
    }
    },
    '浦东': {},
    },
    '山东': {},
    }

    tag = True
    while tag:
    menu1 = menu
    for key in menu1: # 打印第一层
    print(key)

    choice1 = input('第一层>>: ').strip()  # 选择第一层
    
    if choice1 == 'b':  # 输入b,则返回上一级
        break
    if choice1 == 'q':  # 输入q,则退出整体
        tag = False
        continue
    if choice1 not in menu1:  # 输入内容不在menu1内,则继续输入
        continue
    
    while tag:
        menu_2 = menu1[choice1]  # 拿到choice1对应的一层字典
        for key in menu_2:
            print(key)
    
        choice2 = input('第二层>>: ').strip()
    
        if choice2 == 'b':
            break
        if choice2 == 'q':
            tag = False
            continue
        if choice2 not in menu_2:
            continue
    
        while tag:
            menu_3 = menu_2[choice2]
            for key in menu_3:
                print(key)
    
            choice3 = input('第三层>>: ').strip()
            if choice3 == 'b':
                break
            if choice3 == 'q':
                tag = False
                continue
            if choice3 not in menu_3:
                continue
    
            while tag:
                menu_4 = menu_3[choice3]
                for key in menu_4:
                    print(key)
    
                choice4 = input('第四层>>: ').strip()
                if choice4 == 'b':
                    break
                if choice4 == 'q':
                    tag = False
                    continue
                if choice4 not in menu_4:
                    continue
  • 相关阅读:
    利用Spring MVC 上传图片文件
    HdU 4046 Panda 段树
    unity3D的FingerGestures小工具
    深入了解java同步、锁紧机构
    _00021 尼娜抹微笑伊拉克_谁的的最离奇的异常第二阶段 Jedis pool.returnResource(jedis)
    【从翻译mos文章】正在实施的获取job的 session id
    找呀志_通过开源框架引AsyncHttpClient上传文件
    [LeetCode]Count and Say
    使用Intent启动组件
    cpe移植framework后,。解决问题的现有数据库
  • 原文地址:https://www.cnblogs.com/WQ577098649/p/11523456.html
Copyright © 2011-2022 走看看