python课后实验(4)
第4次实验
1、8位验证码
import random def GetRandomChar(): j = int(62*random.random()) if j<10: s = str(j) elif j<36: s = chr(ord("a"+j-10)) else: s = chr(ord("A"+j-36)) return s s = "" for i in range(8): s = s +GetRandomChar() print("验证码为:{}".format(s)) a = input('请输入验证码:') if a ==s: print('欢迎使用本系统') else: print('验证码错误,禁止登录')
2、
def DrawPic(n,char): for i in range(n): print('{}{}'.format((n-i-1)*'',(2*i+1)*char)) for i in range(1,n): print('{}{}.format(i*''),(2*(n-i)-1)*char')) n = eval(input('请输入一个整数:')) c = input('请输入一个字符:') DrawPic(n,c)
3、
def IsPerfectSquaue(n): #判断是否是完全平方数 x = int (n**0.5) if x*x==n: return True else: return False def IsSameDigit(n): s = str(n) for i in range(len(s)-1): for j in range(i+1,len(s)): if s[i]==s[j]: return True return False a,b =eval(input("请输入整数a:")),eval(input("请输入整数b:")) c=0 for i in range(a,b+1): if IsPerfectSquare(i) and IsSameDight(i): c=c+1 print('在[{},{}]共有{}个数满足条件'.format(a,b,c))
4、输入一个字符串,找出该字符串中出现次数最多的字符
s = input("请输入字符串:") a = set(s) count, ch = 0,'' for i in a: j=s.count(i) if j>count: count,ch=j,i print('出现次数最多的字符是{}'.format(ch))
5、写程序,对于输入的字符串,统计每个字符出现的次数,例如输入aabbccc,则输出{'a':2, 'b':2, 'c':3}(假定字符串只包含小写字母),要求使用字典结构完成
def calcCt(s): dic={} for c in s: if c not in dic: dic[c]=0 dic[c]+=1 return dic s = str(input()) print(calcCt(s))
6、
def judge(): i=0 j=len(s)-1 while i<j: if s[i]!=s[j]: return False i+=1 j+=1 return True s = str(input()) if judge(s): print(s,'是回文串') else: print(s,'不是回文串')
7、
import random l = int(input("请输入列表的长度:")) m = [] for i in range(l): num = random.randint(65,65+25) m.append(chr(num)) print(m)