课后作业 day06

Python 实战练习 day06

一 、猜年龄

# 猜年龄游戏
# 1.给定年龄,用户可以猜三次年龄
# 2.年龄猜对,让用户选择两次奖励
# 3.用户选择两次奖励后可以退出
age = 18
count = 0
gift_list = []
i = 0
a,b,c = '手机','电脑','ipad'
while i < 3:
    get_age = int(input('请输入年龄:'))
    if get_age > age:
        print('猜大了')
    if get_age < age:
        print('猜小了')
    else:
        print('恭喜你,猜对了!')
        while count < 2:
            gift = input('''请礼物对应的数字,挑选你的礼物,一共可以挑选两次
    'a':手机,'b':电脑,'c':ipad
    退出请按'N'或'n': ''')
            if gift == 'a' :
                count += 1
                gift_list.append(f'{a}')
            elif gift == 'b':
                count += 1
                gift_list.append(f'{a}')
            elif gift == 'c':
                count += 1
                gift_list.append(f'{c}')
            elif gift == ('N' or 'n'):
                break
            else:
                print('输入的为无效字符,请重新输入')
    if count != 0:
        break
    i += 1
if count != 0:
    print('恭喜你获得:',gift_list)

二 、进阶三级菜单

# 三级菜单
# 1.打印省、市、县三级菜单
# 2.可返回上一级
# 3.可随时退出程序
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
t = 1
while t > 0:
    for key in menu:
        print(key)
    choice1 = input('请输入你要查的省名(退出查询输入"N"or"n"):')
    if choice1 == 'N' or choice1 =='n':
        t =0
    elif choice1 not in menu:
        print('请输入正确的城市名!')
        continue
    if choice1 in menu:
        if bool(menu[choice1]) == False:
            print('无资料,查询结束!')
            t = 0
        else:
            city_dic = menu[choice1]
            for city in city_dic:
                print(city)
            choice2 = input('请输入你要查的城市名(退出查询输入"N"or"n",返回上级输入"B"or"b"):')
            if choice2 == 'N' or choice2 == 'n':
                t = 0
            elif choice2 == 'B' or choice2 == 'b':
                break
            elif choice2 not in city_dic:
                print('请输入正确的城市名!')
                continue
            elif choice2 in city_dic:
                if bool(city_dic[choice2]) == False:
                    print('无资料,查询结束!')
                    t = 0
                else:
                    place_dic = city_dic[choice2]
                    for place in place_dic:
                        print(place)
                    choice3 = input('请输入你要查的地名(退出查询输入"N"or"n",返回上级输入"B"or"b"):')
                    if choice3 == 'N' or choice3 == 'n':
                        t = 0
                    elif choice3 == 'B' or choice3 == 'b':
                        break
                    elif choice3 not in place_dic:
                        print('请输入正确的地名!')
                        continue
                    elif choice3 in place_dic:
                        if bool(place_dic[choice3]) == False:
                            print('无资料,查询结束!')
                            t = 0
                        else:
                            for info in place_dic[choice3]:
                                print(info)
                                print('查询结束!')
                                t = 0
posted @ 2019-09-16 21:35  simple123  阅读(187)  评论(0编辑  收藏  举报