猜年龄游戏
条件
给定年龄,用户可以猜三次年龄
年龄猜对,让用户选择两次奖励
用户选择两次奖励后可以退出
age_count = 0 # 计算次数
age = 18 # 给定年龄
# 奖品信息
prize_dict = {
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
}
prize_msg = '''
0 芭比娃娃
1 变形金刚
2 psp游戏机
3 奥特曼
4 遥控飞机
5 chongqiwawa
'''
get_prize_dict = {} # 获取的奖品信息
while age_count < 3:
age_inp = input('请输入你的年龄:') # 与用户交互
# robust(健壮性/撸棒性)
if not age_inp.isdigit():
print(f'你活了{age_inp}岁吗?') # f格式化可以拼接数字和字符串
continue
age_inp_int = int(age_inp)
# 核心逻辑
if age_inp_int > age:
print('猜大了')
elif age_inp_int < age:
print('猜小了')
else:
print('猜对了')
prize_count = 0
while prize_count < 2:
print(f'奖品如下:{prize_msg}')
# 与用户交互
prize_choice = input('请选择你需要的奖品:')
prize = prize_dict[prize_choice]
# 奖品信息放入购物车
if prize in get_prize_dict:
get_prize_dict[prize] += 1
else:
get_prize_dict[prize] = 1
print(f'恭喜你获得奖品 {prize}')
prize_count += 1
print(f'总共获得奖品为:{get_prize_dict}')
break
age_count += 1 # 失败计数加1