第一题|有奖猜年龄游戏
'''
1、随机给出姓名,用户可以猜三次该人物的年龄
2、年龄猜对,让用户选择两次奖励
3、用户选择两次奖励后可以退出
'''
prize_dic = {'1':'iphone','2':'mac','3':'rabbit','4':None,'5':'pen','6':None,'7':'son'} # 奖品编号:奖品名
# 猜年龄
def guess_game():
answer_info = {'alex': '18', 'robot': '19', 'nick': '500', 'bzr': '3'} # 姓名:年龄
# 随机选取一个name
import random
name = random.choice(list(answer_info.keys()))
# 猜年龄
count = 3
while count:
print(f'you have {count} chances to gusee !')
age_inp = input(f'age of {name}:>>')
if age_inp == answer_info[name]:
print('guess rightly!')
select_prize(prize_dic)
break
else:
print('guess wrong!')
count -= 1
# 打印奖品编号
def print_prize(prize_dic):
print('奖品编号:')
for item in prize_dic:
print(item,end=',')
# 选择奖品
def select_prize(prize_dic):
count = 2
print(f'你有{count}次选择奖品的机会')
prize_get_list=[]
while count:
print_prize(prize_dic)
choice = input('选择奖品编号:>>')
# 判断所选商品是否存在
if choice in prize_dic and prize_dic[choice] == None:
print('很遗憾,没有奖品')
elif choice in prize_dic and prize_dic[choice] != None:
print(f'恭喜,选中了奖品{prize_dic[choice]}')
prize_get_list.append(prize_dic[choice])
else:
print('想多了,没这个奖品编号')
count -= 1
print(f'你获得了奖品{prize_get_list}')
guess_game()
第二题|三级菜单
menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {'dasd':{'dad'},'baba':{'dad':{}}},
'google': {}
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
layers = [menu]
while True:
if len(layers) == 0:
print('没有数据')
break
else:
for key in layers[-1]:
print(key)
choice = input("选择地区打印下级菜单;b:返回;q:退出:>>").strip()
if choice in layers[-1]:
layers.append(layers[-1][choice])
continue
elif choice == 'q':
break
elif choice == 'b':
layers.pop(-1)
continue