第一(关于list)
Posted ckl893
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一(关于list)相关的知识,希望对你有一定的参考价值。
list的方法很多,这里就不写了,参考官方或其他朋友的。
1.toyprogram关于省市列表显示的小脚本.
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 #导入相关类,为退出程序做准备 4 import sys 5 import os 6 7 #定义省、市、县字典 8 ProCityCountyDict = { 9 ‘ShanXi‘:{ 10 ‘XiAn‘:{ 11 ‘GaoXin‘, 12 ‘BeiLin‘, 13 ‘YanTa‘ 14 }, 15 ‘WeiNan‘:{ 16 ‘ChengCheng‘, 17 ‘Pucheng‘, 18 ‘Dali‘ 19 } 20 }, 21 ‘HeNan‘:{ 22 ‘ZhengZhou‘:{ 23 ‘JinShui‘, 24 ‘HuiJi‘ 25 }, 26 ‘LuoYang‘:{ 27 ‘XiGong‘, 28 ‘JianXi‘ 29 } 30 }, 31 ‘ShangHai‘:{ 32 ‘PuTuo‘:{ 33 ‘ZhangJiang‘, 34 ‘ChuanSha‘ 35 }, 36 ‘PuDong‘:{ 37 ‘Zhangjiang‘, 38 ‘ChuanSha‘ 39 } 40 } 41 } 42 43 Pbool = True 44 45 while True: 46 #开始循环省相关的字典列表的key 47 print("Province Name List:") 48 print("=" * 30) 49 #定义空的省份列表 50 ProvinceList = [] 51 for Province in ProCityCountyDict.keys(): 52 print(Province) 53 ProvinceList.append(Province) 54 #打印并收集省份到列表里 55 56 #输入你要选择省 57 InputProvince = input("Please choose a province: ") 58 try: 59 if InputProvince in ProCityCountyDict.keys(): 60 ProvinceNumber = ProvinceList.index(InputProvince) 61 62 #定义输入的内容在列表的索引位置 63 print(‘‘‘ 64 b: back to the before level 65 c: continue to next 66 q: quit the program 67 ‘‘‘) 68 FirstC = True 69 #循环选择条件,如果匹配则执行,继续、返回、退出操作 70 while FirstC: 71 ProvinceChoose = input("please enter b or c or q : ") 72 if ProvinceChoose == ‘b‘: 73 break 74 elif ProvinceChoose == ‘c‘: 75 break 76 elif ProvinceChoose == ‘q‘: 77 FirstC = False 78 Pbool = False 79 sys.exit(0) 80 else: 81 print("You must enter a key in list !") 82 continue 83 84 #如果选择的是返回,则返回重新选择 85 if ProvinceChoose == ‘b‘: 86 continue 87 88 Cbool = True 89 while Cbool: 90 #开始循环市响应的字典列表 91 CityList = [] 92 # 定义市空列表 93 print("City Name list is :") 94 print("*" * 30) 95 #循环打印省下的所有市 96 for City in ProCityCountyDict[InputProvince].keys(): 97 print(City) 98 CityList.append(City) 99 #循环并添加城市到列表里 100 InputCity = input("Please choose a City: ") 101 try: 102 if InputCity in ProCityCountyDict[InputProvince].keys(): 103 #如果输入的城市在响应的key则操作 104 CityNumber = CityList.index(InputCity) 105 print(‘‘‘ 106 b: back to the before level 107 c: continue to next 108 q: quit the program 109 ‘‘‘) 110 SecondC = True 111 while SecondC: 112 CityChoose = input("please enter b or c or q : ") 113 if CityChoose == ‘b‘: 114 break 115 elif CityChoose == ‘c‘: 116 break 117 elif CityChoose == ‘q‘: 118 Cbool = False 119 sys.exit(1) 120 else: 121 print("You must enter a key in list !") 122 continue 123 124 if CityChoose == ‘b‘: 125 continue 126 127 Xbool = True 128 while Xbool: 129 #开始循环县的字典相关 130 CountyList = [] 131 #定义空的县列表 132 print("County Name list is :") 133 print("*" * 30) 134 #循环打印选择所在市下的所有县 135 for County in ProCityCountyDict[InputProvince][InputCity]: 136 print(County) 137 CountyList.append(County) 138 InputCounty = input("Please Choose a County") 139 try: 140 if InputCounty in ProCityCountyDict[InputProvince][InputCity]: 141 CountyNumber = CountyList.index(InputCounty) 142 print(‘‘‘ 143 b: back to the before level 144 c: continue to next 145 q: quit the program 146 ‘‘‘) 147 Tchoose = True 148 while Tchoose: 149 CountyChoose = input("please enter b or c or q : ") 150 if CountyChoose == ‘b‘: 151 break 152 elif CountyChoose == ‘c‘: 153 break 154 elif CountyChoose == ‘q‘: 155 print("nimei") 156 os._exit(1) 157 else: 158 print("You must enter a key in list !") 159 continue 160 161 if CountyChoose == ‘b‘: 162 continue 163 164 #打印省、市、县,通过对应列表的序列号来打印 165 print(‘‘‘ 166 Your choose Province: %s 167 Your choose City: %s 168 Your choose County: %s 169 ‘‘‘ %(ProvinceList[ProvinceNumber],CityList[CityNumber],CountyList[CountyNumber])) 170 171 print(‘‘‘ 172 b: back to the before level 173 q: quit the program 174 ‘‘‘) 175 176 LastChoose = input("please enter b or c or q : ") 177 if LastChoose == ‘b‘: 178 continue 179 elif LastChoose == ‘q‘: 180 os._exit(1) 181 except: 182 pass 183 else: 184 print("You must choose a City !") 185 except KeyError: 186 pass 187 else: 188 print("You must choose a province") 189 except KeyError: 190 pass
执行结果:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/WorkAbout/新建文件夹/s12/day01/hw01/HierarchyList.py Province Name List: ============================== ShanXi HeNan ShangHai Please choose a province: ShanXi b: back to the before level c: continue to next q: quit the program please enter b or c or q : c City Name list is : ****************************** XiAn WeiNan Please choose a City: XiAn b: back to the before level c: continue to next q: quit the program please enter b or c or q : c Please Choose a Countyq County Name list is : ****************************** GaoXin BeiLin YanTa Please Choose a CountyGaoXin b: back to the before level c: continue to next q: quit the program please enter b or c or q : q nimei Process finished with exit code 1
2.toyprogram2,关于用户登录
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import pickle 4 import sys 5 6 ‘‘‘pickle 将文件持久化到文件中,初始化函数‘‘‘ 7 def initPass(): 8 passDict = {‘ckl‘: ‘629888‘, ‘zld‘: ‘880403‘, ‘bab‘: ‘0307‘, ‘mama‘: ‘1129‘} 9 pickleFile = open(‘D:\s12\day01\hw01\pass_file‘, ‘wb‘) 10 pickle.dump(pass_dict, pick_file) 11 pickleFile.close() 12 13 ‘‘‘pickle 加载文件出来‘‘‘ 14 passFile = open(‘D:\s12\day01\hw01\pass_file‘,‘rb‘) 15 userPassDict = pickle.load(passFile) 16 userNameList = userPassDict.keys() 17 passFile.close() 18 19 userName = input("Please input your username: ") 20 for i in range(3): 21 ‘‘‘判断用户是否存在‘‘‘ 22 if userName in userNameList: 23 passWord = input("please input your password: ") 24 ‘‘‘判断用户密码是否包含#,如果包含则是被锁定‘‘‘ 25 if ‘#‘ not in userPassDict[userName]: 26 if passWord == userPassDict[userName]: 27 print("welcome to user login system!") 28 break 29 else: 30 ‘‘‘计算剩余机会‘‘‘ 31 leaveTimes = 2 - i 32 if leaveTimes > 0: 33 print("sorry , you have %s chance!" % leaveTimes) 34 elif leaveTimes == 0: 35 print("hollyshit, you must be reject and be locked!") 36 ‘‘‘如果三次机会用完,则在密码后加#,锁定用户‘‘‘ 37 userPassDict[userName] += ‘#‘ 38 passFile = open(‘D:\s12\day01\hw01\pass_file‘, ‘wb‘) 39 pickle.dump(userPassDict, passFile) 40 passFile.close() 41 else: 42 print("sorry, you have been locked!") 43 sys.exit(0) 44 else: 45 print("who are you? i dont‘t know you!") 46 break
运行结果:
以上是关于第一(关于list)的主要内容,如果未能解决你的问题,请参考以下文章
关于js----------------分享前端开发常用代码片段
TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect(代码片段