第六次作业
问题1
现有a=【1,2,3,4,5,6】,用多种方式实现列表的反转,并写出推导过程
复制代码
1
2
3
4
5
6
7
8
9
10
11a=[1,2,3,4,5,6] #方法一 a=a[::-1] print(a) #方法二 a.reverse() print(a) #方法三 a.sort(reverse=True) print(a)
问题2
给用户9次机会,猜1-10个数字随机猜数字,如果随机的数字和用户的数字一致则表示正确,如果不一致则表示错误,最终结果要求用户怎么也猜不对
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import random i=1 temp=random.randint(0,11) guess = int(input("请输入:")) #用户猜测的数字 while i<=9: i+=1 if temp!=guess: guess=int(input(("猜错了,请重新输入:"))) while guess==temp: temp=random.randint(0,11) if guess!=temp: continue print(f"guess={guess},temp={temp}")
问题3
问题3:有两个列表lst1=[11,22,33],lst2=[22,33,44],获取内容相同的元素
复制代码
1
2
3
4
5
6
7lst1=[11,22,33] lst2=[22,33,44] for i in range(0,3): for j in range(0,3): if lst1[i]==lst2[j]: print(lst1[i])
问题4
现在有8位老师,3个办公室,要求8位老师随机分配到三个办公室中
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import random ale=['教师1', '教师2', '教师3', '教师4', '教师5', '教师6', '教师7', '教师8'] # print(ale[1]) def classrom(): class1=[] class2=[] class3=[] num=7 for i in range(0,3): a=random.randint(0,num) class1.append(ale[a]) ale.remove(ale[a]) num-=1 for i in range(0,3): a=random.randint(0,num) class2.append(ale[a]) ale.remove(ale[a]) num-=1 for i in range(0,2): a=random.randint(0,num) class3.append(ale[a]) ale.remove(ale[a]) num-=1 print(class1) print(class2) print(class3) classrom()
问题5
要求从键盘输入用户名和密码,校验格式是否符合规则,如果不符合规则,打印出不符合的原因,并提示从新输入~
要求:1、用户名长度6-20,用户名必须以字母开头
2、密码长度至少6位,不能为纯数字,不能有空格
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39guess=input("请输入:")#用户账号 while str!="": if (guess[0].isalpha())==False: guess=input("账号输入错误(首字母不能为数字),请重新输入:") if (guess[0].isalpha())==True: break while str!="": if len(guess)<6 or len(guess)>20: guess=input("账号输入错误(字数不够),请重新输入:") else: break while (type(guess[0])==str)==False and len(guess)<6 or len(guess)>20: guess=input("账号输入错误,请重新输入:") # while 6<=len(guess)<=20 and (guess[0].isalpha())==True: while 6 <= len(guess) <= 20 and (guess[0].isalpha()) == True: temp = input("请输入密码:") #2、密码长度至少6位,不能为纯数字,不能有空格 if temp!="": while len(temp)<6: temp=input("密码输入错误(最少为6位),请重新输入:") if len(temp)>=6: break while temp.isdigit()==True: temp = input("密码输入错误(纯数字),请重新输入:") if temp.isdigit()==False: break while " " in temp: temp=input("密码输入错误(有空格),请重新输入:") if (" " in temp)==False: break if len(temp)>=6 and temp.isdigit()==False and (" " in temp)==False: break while len(temp)>=6 and temp.isdigit()==False and (" " in temp)==False: print(f"账号为:{guess}") # print(temp) print(f"密码为:{temp}") print("账号密码输入正确") break
最后
以上就是英俊香烟最近收集整理的关于python列表练习第六次作业问题2问题3问题4问题5的全部内容,更多相关python列表练习第六次作业问题2问题3问题4问题5内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复