9.12 已经算长篇的两个程序

一、猜年龄抽奖游戏:

  1. 给定年龄,用户可以猜三次年龄
  2. 年龄猜对,让用户选择两次奖励
  3. 用户选择两次奖励后可以退出
age = 18
count = 0
prize_dict = {0: '多拉爱梦', 1: '百宝箱', 3: 'python学习宝典', 4: '境外游资助费'}
while count < 3:
    age_inp = input('请输入你猜的年龄:')
    if not age_inp.isdigit():   #如果输入的不是纯数字
        print('只能输入数字,请重新输入')
        continue
    age_inp_int = int(age_inp)
    if age_inp_int == age:
        print('猜中了,可以抽两次奖')
        print(prize_dict)
        # 获取两次奖品:
        for i in range(2): #猜中后的内循环
            prize_choice = input('请输入你想要的奖品序号,如果不需要输入n退出!!')
            if prize_choice != 'n':
                print(f'恭喜你获得奖品:{prize_dict[int(prize_choice)]}')  #字典按key取值
            else:
                break
        break
    elif age_inp_int > age:
        print('猜大了')
    else:
        print('猜小了')
    count += 1  # 玩一次游戏次数加1
    if count != 3:
        continue
    again_choice = input('你还想继续玩吗?继续请回复y,否则按其他任意键退出!')
    if again_choice == 'y':
        count = 0

二、菜单:

  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
menu={
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
tag=True
while tag:
    menu1=menu
    for key in menu1:  #打印第一层
        print(key)
    choice1=input('第一层:').strip()   #选择第一层
    if choice1=='b':
        break
    if choice1=='q':
        tag=False
        continue
    if choice1 not in menu1:
        continue
    while tag:
        menu2=menu1[choice1]
        for key in menu2:
            print(key)
        choice2 = input('第二层:').strip()  # 选择第二层
        if choice2 == 'b':
            break
        if choice2== 'q':
            tag = False
            continue
        if choice2 not in menu2:
            continue
        while tag:
            menu3 = menu2[choice2]
            for key in menu3:
                print(key)
            choice3 = input('第三层:').strip()  # 选择第三层
            if choice3 == 'b':
                break
            if choice3 == 'q':
                tag = False
                continue
            if choice3 not in menu3:
                continue
            while tag:
                menu4 = menu3[choice3]
                for key in menu4:
                    print(key)
                choice4 = input('第四层:').strip()  # 选择第四层
                if choice4 == 'b':
                    break
                if choice4 == 'q':
                    tag = False
                    continue
                if choice4 not in menu4:
                    continue

这个程序主要应用了while的嵌套循环,实现了菜单的层层包含关系,每一层循环的内容一样,都是把上一层的值当做键,开始遍历,if条件判断中,如果输入'b'就返回上一层,跳出本层循环,如果输入'q'就退出全部循环,因为tag=false相当于一个条件,在每层的while判断中不通过。如果输入其他内容就一直输入,直到输入菜单里有的子菜单,方进入下一层。

猜你喜欢

转载自www.cnblogs.com/lidandanaa/p/11515133.html
今日推荐