猜年龄游戏
'''
1. 给定年龄,用户可以猜三次年龄
2. 年龄猜对,让用户选择两次奖励
3. 用户选择两次奖励后可以退出
'''
import random
age = random.randint(18, 40) # 随机数字,范围18-40
count = 0
reward_dict = {
'0': '奥特曼',
'1': '钢铁侠',
'2': '《笨方法学python》',
'3': '泰国一日游',
'4': 'iphoneX',
'5': '娃娃',
'6': '阿拉丁',
'7': '特斯拉',
'8': '谢谢惠顾'
}
reward_cart = {} # 用户获得奖品放在这里
while count < 3:
count += 1
age_inp = input('请输入你的年龄:').strip()
if not age_inp.isdigit(): # 判断是否是数字
print('非法输入字符,请重新输入')
age_inp = int(age_inp)
# 删选年龄范围
if age_inp > 40 or age_inp < 18:
print('超出范围,请重新输入')
# 核心逻辑
if age_inp == age:
print('猜对了')
# 打印奖品
for i, j in reward_dict.items():
print(f'奖品编号:{i} {j}')
# 用户抽奖两次
for k in range(2):
reward_choice = input('请输入你想要的奖品').strip()
if reward_choice not in reward_dict: # 删选范围
print('没这么多奖品')
else:
reward_get = reward_dict[reward_choice]
print(f'恭喜获得奖品:{reward_get}')
if reward_get not in reward_cart:
reward_cart[reward_get] = 1
else:
reward_cart[reward_get] += 1
print(f'恭喜你获得以下奖品:{reward_cart}')
break
elif age_inp < age:
print('猜小了')
else:
print('猜大了')
continue
三级菜单
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
# 偷懒方法
menu1 = [menu]
while True:
if len(menu1) == 0:
break
current_menu = menu1[-1]
for key in current_menu:
print(key)
choice = input('请输入>>').strip()
if choice == 'q':
break
if choice == 'b':
menu1.pop(-1)
continue
menu1.append(current_menu[choice])