python课后实验(3)
1、
n = eval(input("请输入一个正整数:")) s = str(n) + '=1' a=2 while n>2: if n%a==0: s = s+"*" +str(a) n=n/a else: a=a+1 print(s)
2、
s = input("请输入字符串:") a,b,c,d=0,0,0,0 for i in s: if i=="": a+=1 elif '0'<=i<='9': b+=1 elif 'a'<=i<='z' or 'A'<=i<='Z': c+=1 else: d+=1 print("输入的字符串有{}个空格,{}个数字,{}个英文字母,{}个其他字符".format(a,b,c,d))
3、
a=eval(input("请输入第一个正整数:")) b=eval(input("请输入第二个正整数:")) c=0 for i in range(a,b+1): s = str(i) if s.count('8')>0: c=c+1 print('{}和{}之间有{}个数含有数字8'.format(a,b,c))
4、
a = input("请输入整数a:") b = input("请输入整数b:") import random random.seed(1000) for i in range(a): s = random.randint(100,999) if i%5==4: print(s) else: print('{}'.format(x),end=' ') if x%b ==0: c=c+1 print('\n总共{}个数能被{}整除'.format(c,b))
5、
def func(a): sum = 0 while a!=0: sum = sum+(a%10) a = a//10 return sum a = int(input('请输入一个整数:')) sum = func(a) print('它的各位数字和为{}'.format(sum))
6、
def func(s): ct = 0 for c in s: if c>='A' and c<='Z': ct+=1 return ct s = str(input()) print(func(s))
7、
def count(string):
c,d=0,0
for s in string:
if '0'<=s<='9':
c = c+1
if 'a'<=s<='z' or 'A'<=s<='Z':
d = d+1
return c,d
str = input("请输入一个参数字符串:")
c,d=count(str)
print("字符串中有{}个数字,{}个字母".format(c,d))
8、
def IsPrime(n): for i in range(2,n-1): if n%i==0: return False return True m,n = eval(input('请输入整数m:')),eval(input('请输入整数n:')) c=0 for i in range(m,n+1): if IsPrime(i): c = c+i print("范围在{},{}的素数和为{}".format(m,n,c))