三级菜单
打印省、市、县三级菜单
可返回上一级
可随时退出程序
menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': { '百度': {}, }, }, '昌平': { '沙河': { '老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': { '闵行': { "人民广场": { '炸鸡店': {} } }, '闸北': { '火车战': { '携程': {} } }, '浦东': {}, }, '山东': {}, } tag = True while tag: menu1 = menu # print(menu1) for key in menu1: print(key) # 打印第一层字典的key chose1 = input('第一层>>:').strip() if chose1 == 'b': # 输入b返回上一层 break if chose1 == 'q': # 输入p结束程序 tag = False continue if chose1 not in menu1: # 输入内容不在menu1内,则继续输入 continue while tag: menu2 = menu1[chose1] # print(menu2) for key in menu2: print(key) chose2 = input('第二层>>:').strip() if chose2 == 'b': break if chose2 == 'q': tag = False if chose2 not in menu2: continue while tag: menu3 = menu2[chose2] # print(menu3) for key in menu3: print(key) chose3 = input('第三层>>:').strip() if chose3 == 'b': break if chose3 == 'p': tag = False if chose3 not in menu3: continue while tag: menu4 = menu3[chose3] # print(menu4) for key in menu4: print(key) chose4 = input('第四层>>:') if chose4 == 'b': break if chose4 == 'p': tag = False continue if chose4 not in menu4: continue if chose4 in menu4: # 如果输入正确结束程序 tag = False
猜年龄
- 给定年龄,用户可以猜三次年龄
- 年龄猜对,让用户选择两次奖励
- 用户选择两次奖励后可以退出
age = 18 # 答案 count = 0 # 游戏次数控制 prize_dict = {0: '布娃娃', 1: '变形金刚', 2: '奥特曼', 3: '<Python从入门到放弃>'} # 核心代码 while count < 3: inp_age = input('请输入你的年龄>>>') # 与用户交互 # 判断用户是否骚扰(超纲:判断用户输入的是否为数字) if not inp_age.isdigit(): print('傻逼,你的年龄输错了') continue inp_age_int = int(inp_age) # 核心逻辑,判断年龄 if inp_age_int == age: print('猜对了') print(prize_dict) # 打印奖品 # 获取两次奖品 for i in range(2): prize_choice = input( '请输入你想要的奖品,如果不想要,则输入"n"退出!!!') # 与用户交互获取奖品 # 判断是否需要奖品 if prize_choice != 'n': print(f'恭喜你获得奖品: {prize_dict[int(prize_choice)]}') else: break break elif inp_age_int < age: print('猜小了') else: print('猜大了') count += 1 # 成功玩一次游戏 if count != 3: continue again_choice = input('是否继续游戏,继续请输入"Y",否则任意键直接退出.') # 交互是否再一次 # 判断是否继续 if again_choice == 'Y': count = 0