猜年龄
# 给定年龄,用户可以猜三次年龄
#
# 年龄猜对,让用户选择两次奖励
#
# 用户选择两次奖励后可以退出
age = 18 # 答案
count = 0 # 游戏次数控制
prize_dict = {0: '布娃娃', 1: '变形金刚', 2: '奥特曼', 3: '<Python从入门到放弃>'}
prize_get = []
while count < 3:
inp_age = int(input('请输入年龄:'))
if inp_age > age:
print('猜大了')
elif inp_age < age:
print('猜小了')
else:
print('猜对了')
print(prize_dict)
for i in range(2):
prize_choice = int(input('请选择你的奖品:'))
if prize_choice in prize_dict:
prize_get.append(prize_dict[prize_choice])
else:
break
print('恭喜你获得奖品:', end='')
for n in prize_get:
print(n, end=' ')
count += 1
if count == 3:
again_choice = input('是否继续游戏(Y/N):')
if again_choice == 'Y':
count = 0
else:
break
三级菜单
# 打印省、市、县三级菜单
#
# 可返回上一级
#
# 可随时退出程序
menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
tag = True
count = 0
while tag:
if count == 0:
for key in menu:
print(key)
choice = input('第一层:')
if choice not in menu and choice not in ['q']:
continue
elif choice == 'q':
tag = False
break
else:
count += 1
elif count == 1:
menu1 = menu[choice]
for key in menu1:
print(key)
choice1 = input('第二层:')
if choice1 not in menu1 and choice1 not in ['b', 'q']:
continue
elif choice1 == 'b':
count -= 1
elif choice1 == 'q':
tag = False
break
else:
count += 1
elif count == 2:
menu2 = menu[choice][choice1]
for key in menu2:
print(key)
choice2 = input('第三层:')
if choice2 not in menu2 and choice2 not in ['b', 'q']:
continue
elif choice2 == 'b':
count -= 1
elif choice2 == 'q':
tag = False
break
else:
count += 1
elif count == 3:
menu3 = menu[choice][choice1][choice2]
for key in menu3:
print(key)
choice3 = input('第四层:')
if choice3 not in menu3 and choice3 not in ['b', 'q']:
continue
elif choice3 == 'b':
count -= 1
elif choice3 == 'q':
tag = False
break
else:
count = 3