练习流程控制

匿名 (未验证) 提交于 2019-12-03 00:03:02

Ŀ¼

给定年龄,用户可以猜三次年龄  年龄猜对,让用户选择两次奖励  用户选择两次奖励后可以退出
age = 18 count = 0 prize_dict = {0:'布娃娃',1:'变形金刚',2:'高达',3:'零食大礼包',4:'<墨菲定律>'} while count <3:     inp_age = input('请输入您的名字: ')     if not inp_age.isdigit():  #判断输入是否为数字.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_dict != '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
打印省、市、县三级菜单  可返回上一级  可随时退出程序
menu = {     '北京': {         '海淀': {             '五道口': {                 'soho': {},                 '网易': {},                 'google': {}             },             '中关村': {                 '爱奇艺': {},                 '汽车之家': {},                 'youku': {},             },             '上地': {                 '百度': {},             },         },         '昌平': {             '沙河': {                 '老男孩': {},                 '北航': {},             },             '天通苑': {},             '回龙观': {},         },         '朝阳': {},         '东城': {},     },     '上海': {         '闵行': {             "人民广场": {                 '炸鸡店': {}             }         },         '闸北': {             '火车站': {                 '携程': {}             }         },         '浦东': {             '外滩' : {                 '和平饭店' : {}             }         },     },     '山东': {}, } layers ={     menu, } while True:     current_layer = layers[-1]     for key in current_layer:         print(key)     choice = input('>>: ').strip()     if choice == 'q':         break     if choice not in current_layer:         continue     layers.append(current_layer[choice])
#-------改进版(加入退出机制)  menu = {     '北京': {         '海淀': {             '五道口': {                 'soho': {},                 '网易': {},                 'google': {}             },             '中关村': {                 '爱奇艺': {},                 '汽车之家': {},                 'youku': {},             },             '上地': {                 '百度': {},             },         },         '昌平': {             '沙河': {                 '老男孩': {},                 '北航': {},             },             '天通苑': {},             '回龙观': {},         },         '朝阳': {},         '东城': {},     },     '上海': {         '闵行': {             "人民广场": {                 '炸鸡店': {}             }         },         '闸北': {             '火车站': {                 '携程': {}             }         },         '浦东': {             '外滩' : {                 '和平饭店' : {}             }         },     },     '山东': {}, } layers = {     menus, } while True:     if len(layers) == 0:         break     current_layer = layers[-1]     for key in current_layer:         print(key)     choice = input('>>: ').strip()     if choice == ' b':         layers.pop(-1)         continue     if choice == 'q':         break     if choice not in current_layer:         continue     layers.append(current_layer[choice] 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!