三级菜单
# 三级菜单自定义
menu = {1:{
11:{
111:{},
112:{},
113:{}
},
12:{
121:{},
122:{},
123:{}
}
},
2:{
21:{
211:{},
212:{},
213:{}
},
22:{
221:{},
222:{}
}
},
3:{
31:{},
32:{}
}
}
flag = True
menu1 = menu
list = []
while flag:
for key in menu1:
print(key)
choise = int(input("input your choice: ").strip())
if choise == 4:
choise1 = list.pop()
print('break')
for i in menu:
if choise1 == i:
menu1 = menu
break
menu_1 = menu[i]
for j in menu_1:
if choise1 == j:
menu1 = menu_1
break
menu_2 = menu_1[j]
for k in menu_2:
if choise1 == k:
menu1 = menu_2
break
continue
elif choise == 5:
flag = False
continue
else:
menu1 = menu1[choise]
list.append(choise)
#改进版
tag = True
level = []
level.append(menu)
while tag:
if len(level) > 0:
print(level[-1])
choice = input('请输入您的选择: ')
if choice.isdigit():
if int(choice) in level[-1]:
level.append(level[-1][int(choice)])
else:
print('对不起没有这个选择!')
elif choice == 'q':
break
elif choice == 'b':
level.pop(-1)
猜年龄游戏
age = 18
tag = True
prize_dict = {0: '布娃娃', 1: '变形金刚', 2: '奥特曼', 3: '<Python从入门到放弃>'}
while tag:
count = 0
while count < 3:
guess = int(input('请输入猜测的年龄: '))
if guess == age:
print('恭喜你猜对了!')
for i in range(2):
prize_choice = input('请输入您想要的奖品编号: ')
if prize_choice != 'n' and prize_choice != 'N':
print(f'恭喜您获得奖品{prize_dict[int(prize_choice)]} 1个!')
else:
break
print('奖品选择完成,退出游戏!')
tag = False
break
elif guess > age:
print('对不起,猜测偏大!')
else:
print('对不起,猜测偏小!')
count += 1
else:
while 1:
choice = input('是否还想继续游戏?')
if choice == 'Y' or choice == 'y':
break
elif choice == 'N' or choice == 'n':
tag = False
break
else:
print('对不起输入有误!')