1、你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号。
产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成,结果如下图:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #需要你写一个函数,批量生成一些注册使用的账号。 2 3 #产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成 4 import random,string 5 # with open('email.txt','a+',encoding='utf-8') as f: 6 lenEmail = input('请输入要注册的账号长度:').strip() 7 numEmail = input('请输入要生成的账号条数:').strip() 8 lenEmail = int(lenEmail) 9 numEmail = int(numEmail) 10 # reEmail = [] 11 set_reEmail = set() 12 while True: 13 if lenEmail>=7: 14 reEmail = [] 15 endfix = '@163' 16 # for i in range (int(numEmail)): 17 Up = random.sample(string.ascii_uppercase,1) 18 # print(Up) 19 Lo = random.sample(string.ascii_lowercase,1) 20 # print(Lo) 21 Dig = random.sample(string.digits,1) 22 # print(Dig) 23 z = string.digits+string.ascii_lowercase+string.ascii_uppercase 24 num = lenEmail-len(endfix)-3 25 # print(num) 26 rdm = random.sample(z,num) 27 reEmail.extend(Up)#1个大写字母 28 reEmail.extend(Lo)#1个小写字母 29 reEmail.extend(Dig)#1个数字 30 reEmail.extend(rdm)#补上位数 31 random.shuffle(reEmail) 32 # print(reEmail) 33 account = ''.join(reEmail) 34 set_reEmail.add(account) 35 # print(set_reEmail) 36 # print(account+endfix) 37 with open('email.txt','w+') as fw: 38 for i in set_reEmail: 39 fw.write(i+endfix+'\n') 40 if len(set_reEmail)==numEmail: 41 break 42 43 else: 44 print('账号必须是包含大小写以及数字的,长度不足')
2.下面几段代码中哪个是错误的写法,为什么?
1 var = [1,2,3,4] 2 var[1]=5 3 var1 = 'abc123' 4 var1[0]='s' 5 var2 = ('a','b','c','d') 6 var2[-1]=2 7 var3={'a',3,'b','4'} 8 var3[2]='c' 9 var4={'name':'lily','sex':'nv'} 10 print(var4['age'])
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # var = [1,2,3,4] #定义一个list 2 # var[1]=5 #将下标为1的元素换成5 3 # print(var) 4 # 运行后 5 # [1, 5, 3, 4] 6 # var1 = 'abc123' #定义一个字符串 7 # var1[0]='s'#字符串不可修改 8 # print(var1) 9 #运行后 10 # Traceback (most recent call last): 11 # File "G:/catherine/python/day4/lianxi2.py", line 7, in <module> 12 # var1[0]='s' 13 # TypeError: 'str' object does not support item assignment 14 15 # var2 = ('a','b','c','d') #tuple 中元素不可更改 16 # var2[-1]=2 17 # print(var2) 18 # 运行后 19 # Traceback (most recent call last): 20 # File "G:/catherine/python/day4/lianxi2.py", line 16, in <module> 21 # var2[-1]=2 22 # TypeError: 'tuple' object does not support item assignment 23 24 # var3={'a',3,'b','4'} #集合中的元素不可以被修改 25 # var3[2]='c' 26 # print(var3) 27 # 运行后 28 # Traceback (most recent call last): 29 # File "G:/catherine/python/day4/lianxi2.py", line 25, in <module> 30 # var3[2]='c' 31 # TypeError: 'set' object does not support item assignment 32 # var4={'name':'lily','sex':'nv'} 33 # print(var4['age'])#在没有找到这个key的时候会报错 34 #运行后 35 # Traceback (most recent call last): 36 # File "G:/catherine/python/day4/lianxi2.py", line 33, in <module> 37 # print(var4['age']) 38 # KeyError: 'age'