最初の質問
# 给定年龄,用户可以猜三次年龄
#
# 年龄猜对,让用户选择两次奖励
#
# 用户选择两次奖励后可以退出
age = 18
count = 0
prize_lis = ['海绵宝宝玩偶', '变形金刚', '小霸王游戏机', '三国杀珍藏版卡牌', '奥特曼']
tag = True
while tag:
# if count == 3:
# break
# 判断年龄
gus_age = int(input('请输入你猜的年龄:'))
if gus_age < 18:
print('你猜小了。。')
elif gus_age > 18:
print('你猜大了。。')
else:
print('恭喜,你猜对了!!')
# 打印奖品
for ind, prize in enumerate(prize_lis):
print(f'{ind} 奖品:{prize}')
# 用户选择奖品
for i in range(2):
choice_prize = input('请输入你选择的奖品的编号(不需要奖品请输入 q 退出):')
if choice_prize == 'q':
print('退出成功!')
break
else:
choice_prize = int(choice_prize)
print(f'恭喜你获得奖品:{prize_lis[choice_prize]}')
break
count += 1
if count == 3:
print('gameover...')
# 判断是否继续游戏
while True:
choice_jixu = input('请问是否继续游戏(继续请输入 Y/y,退出请输入 N/n ):')
if choice_jixu == 'Y' or choice_jixu == 'y':
count = 0
break
elif choice_jixu == 'N' or choice_jixu == 'n':
print('退出游戏成功!')
tag = False
break
else:
print('输入错误,请重新输入')
2番目の質問
打印省、市、县三级菜单
可返回上一级
可随时退出程序
dic1 = {'北京': {'大兴': {'大兴1': {'d': {}}, '大兴2': {'a': {}}, '大兴3': {'c': {}}}, '海淀': {'海淀1', '海淀2'}}, '上海': {'青浦区'}}
lis = [dic1]
print(1, lis)
while True:
if len(lis) == 0: # 在首次显示的第一级菜单,就输入 r
break
current_list = lis[-1]
for i in current_list:
print(i)
choice_name = input('请输入上面的菜单名字(q 退出程序, r 返回上一级):')
if choice_name == 'q':
break
if choice_name == 'r':
lis.pop()
continue
if choice_name not in current_list:
continue
lis.append(current_list[choice_name])