三级菜单 ,求1

1.猜年龄游戏:

要求:

    允许用户最多尝试3次
    每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
    如果猜对了,有三次选择奖励的机会,不要奖励可以随时退出,并打印奖品清单给用户看;
count = 0
real_age = 19
goods_list = [
    '芭比娃娃',
    '变形金刚',
    '小猪佩奇',
    '蜘蛛侠',
    '模型飞机',
    'inflatable doll'
]
customer_goods_list = []

while True:
    age = input('请猜一下真实年龄(q退出)>>>:').strip().lower()
    if age == 'q': break
    if not age.isdigit():
        print('请输入纯数字!')
        continue
    age = int(age)
    if age == real_age:
        print('恭喜你!猜对了!你有3次选择奖励的机会!','
')
        choice_count = 0
        while True:
            for index, good in enumerate(goods_list, 1):
                print(index, good)
            choice = input('请选择奖品(或者直接按q退出)>>>:').strip().lower()
            if choice == 'q': break
            if not choice.isdigit():
                print('请输入数字!')
                continue
            choice = int(choice) - 1
            if choice not in range(len(goods_list)):
                print('选择的奖品不在范围内,请重新输入!')
                continue
            else:
                choice_count += 1
                print(f'你选择的奖品是:{goods_list[choice]},还有{3-choice_count}次机会!')
                customer_goods_list.append(goods_list[choice])

            if choice_count == 3: break
        print(f'你的奖品清单:{customer_goods_list}')
        break

    elif age > real_age:
        print('猜大了!')
        continue
    else:
        print('猜小了!')
    count += 1
    if count == 3:
        while True:
            again_choice = input('还要再玩三次吗?输y再玩3次,或者输n直接退出>>>:').strip().lower()
            if again_choice == 'y':
                count = 0
                continue
            elif again_choice == 'n':
                exit()
                #quit()
            else:
                print('输入不正确,请重试!')
 
View Code

2.三级菜单:

  1. 打印省、市、县三级菜单

  2. 可返回上一级

  3. 可随时退出程序

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
layers = [menu, ]
while True:
    if len(layers) == 0: break
    current_layers = layers[-1]
    # print(current_layers,type(current_layers),'=================')
    for key in current_layers:
        print(key)
    choice = input('请选择(b返回上一层,q直接退出)>>>:').strip()
    if choice == 'b':
        layers.pop(-1)
        # print(layers,'+++++++++++++++++')
        continue
    if choice == 'q': break
    if choice not in current_layers:
        continue
    else:
        layers.append(current_layers[choice])
        # for i in layers:
        #     print(i,'@@@@@@@@@@@@@@@@@@@@')
        # print(layers,'-------------------------',len(layers))
        # print(current_layers[choice],type(current_layers[choice]),'**********************')
View Code
3.求1 - 2 + 3 - 4 + 5...99的所有数的和
# 求1 - 2 + 3 - 4 + 5...99的所有数的和

# 方法1
count = 0
get_sum = 0
while True:
    if count == 100:
        break
    get_sum += count * ((-1) ** (count + 1))
    count += 1
print(get_sum)

# 方法2
count = 0
get_sum = 0
get_sum1 = 0
get_sum2 = 0
while True:
    if count == 100:
        break
    if count % 2 == 0:
        get_sum1 -= count
    else:
        get_sum2 += count
    get_sum = get_sum1 + get_sum2
    count += 1
print(get_sum)
View Code

 

 

原文地址:https://www.cnblogs.com/zhangchaocoming/p/11520208.html