ddd
作者:互联网
cx1.py参考代码: #encoding=utf-8 from random import * Passtr="0123456789abcdefghijklmnopqrstuvwxyz" def code(str): #生成6位密码 Pas="" for i in range(6): Pas += str[randint(0, len(str) - 1)] return Pas #循环判定密码输入的正确与否 Pas=code(Passtr) print(Pas) for Count in range(1,4): Guess=input("输入猜的密码:") if Guess==Pas: print("密码正确") break else: print("密码错误,你还有{}次机会".format(3-Count)) print("游戏结束!") cx2.py参考代码: import random def Pzc(n): if n%3==0: return 1 else: return 0 def Lst(n): ss=[random.randint(0,100) for i in range(n)] return ss def main(): B=[] A=Lst(15) print("随机生成的列表A为:",A) for m in A: if Pzc(m)==1: B.append(m) B.sort() print("A列表中有{}个数能被3整除,分别是{}:".format(len(B),B)) main() cx3.prg参考代码: def addlist(n): sum=0 for i in n: sum=sum+i return sum List=[1,3,8,9,5,6] jieguo=addlist(List) print(jieguo) fp= open('t1.txt',mode='w+') fp.write(str(jieguo)) fp.close() cx4.prg参考代码: import random def prime(n): flag=True for i in range(2,n): if n%i==0: flag=False return 0 break else: return 1 def lt(m): ls=[random.randint(1,200) for i in range(m)] return ls ls=lt(12) print("随机生成的列表为:",ls) print("素数有:",end="") s=0 for i in ls: if prime(i)==1: print(i,end=" ") s=s+i print("\n素数和为:",s) cx5.prg参考代码: import random all_chars = '0123456789' user_name = ['user0','user1','user2','user3','user4','user5'] user_pwd_dict = {} for name in user_name: pwd = '' for i in range(6): pwd += random.choice(all_chars) user_pwd_dict[name] = pwd print("已分配初始密码,字典数据为:",user_pwd_dict) fid = open('user_inf.txt','wt') for name,pwd in user_pwd_dict.items(): fid.write(name + ':' + pwd + '\n') fid.close() print("用户名和密码已存至user_inf.txt中!") cx6.prg参考代码: x=eval(input('请输入x:')) print('x={}'.format(x)) def fact(n): s=1 for i in range(1,n+1): s=s*i return s sum=1 i=1 while True: sum=sum+(x**(i))/fact(i) if (x**(i))/fact(i)<1e-6: break i=i+1 print('e的{}次幂为:{}'.format(x,sum)) cx7.prg参考代码: def GetTxt(file): op=open(file,"r") txt=op.readlines() return txt def TxtD(lstr): luser=[] for line in lstr: line = line.replace("\n", "") lp = line.split(":") luser.append(tuple(lp)) MyDict=dict(luser) return(MyDict) ftxt=GetTxt("data.txt") MD=TxtD(ftxt) user_name = input("请输入用户名:") password = input("请输入密码:") if user_name not in MD: print("用户名输入不正确") elif password != MD[user_name]: print("密码输入如错误") else: print("登录成功") cx8.prg参考代码: def fenci(): x=open("c://xx.txt").read() #注意文件地址根据实际情况决定 m=x.split(",") return m def tongji(m): dic={} for s in m: dic[s[0]]=dic.get(s[0],0)+1 return dic M=fenci() D=tongji(M) print("统计结果:",D) new=list(D.items()) new.sort(key=lambda x:x[1],reverse=True) print("排名前三的姓氏为:") for i in range(3): word,count=new[i] print(word,count) cx9.prg参考代码: def jiami(s): L = [] for i in s: if(i == ' '): L.append(i) else: L.append(chr(ord(i) - 32 + 3)) L1 = ''.join(L) #列表转换为字符 return str(L1) s1 = str(input("请输入待转换的英语字母:")) s2=jiami(s1) zd = dict(zip(s1, s2)) print(zd) cx10.prg参考代码: # 按优秀、良好、中等、及格和不及格五个等级进行统计 def fs(): x=open("data.txt").read() ls=list(map(int,x.split(","))) return ls def dj(s): d={} for i in s: if i>= 90: d['优秀']=d.get('优秀',0)+1 elif i>= 80: d['良好']=d.get('良好',0)+1 elif i>= 70: d['中等']=d.get('中等',0)+1 elif i>= 60: d['及格']=d.get('及格',0)+1 else: d['不及格']=d.get('不及格',0)+1 return d score = fs() print("学生分数:",end="") for i in score: print(i,end=" ") dic=dj(score) counts=list(dic.items()) counts.sort(key=lambda x:x[1],reverse=True) print("\n分等级统计结果排序:") for i in counts: print("{}:{}人".format(i[0],i[1])) cx11.prg参考代码: def is_cdata(num): lt = [] for i in range(1,num): if num % i == 0: lt.append(i) if sum(lt) == num: return 1,lt else: return 0,[] data = eval(input("请输入一个数:")) flag,lt_data = is_cdata(data) if flag == 1: lt_data_str = [str(ch) for ch in lt_data] print("是,{}={}".format(data,'+'.join(lt_data_str))) else: print("不是") cx12.prg参考代码: import random a=('黄盖','曹操','刘备','诸葛亮','关羽','吕布') b= {} b=dict.fromkeys(a) for i in range(0,6): c=[] for j in range(0,10): c.append(random.randint(0,11)) b[a[i]]=c ##文件保存 with open('score.txt','w+') as f: f.write(str(b)) ##元素查找 name=input('请输入要查找的人:') if name in a: score=b[name] m=max(score) n=min(score) s=(sum(score)-m-n)/(len(score)-2) print('歌手{}的最终得分为{}'.format(name,s)) else: print('你输入的歌手{}不存在!!!'.format(name))
标签:return,name,random,lt,range,print,ddd 来源: https://www.cnblogs.com/dxfDXF/p/16387025.html