第六周 book4

4.1
t=425
guess = 0
count=0
while guess !=t: #未知循坏次数,故用while一直循环,直到数值正确
guess=eval(input('请输入猜测数字(1~1000):'))
if guess>t:
print('猜大了')
count+=1
elif guess<t:
print('猜小了')
count+=1
else:
print('猜对了')
count+=1
print('猜了{}次'.format(count))
4.2
a=input('请输入一个字符串:')
y=0
s=0
k=0
q=0
for i in a:
if i.isupper():
y+=1
elif i.islower():
y+=1
elif '\u4e00'<=i<='\u9fa5': #用于统计字符串里的中文字
y+=1
elif i.isnumeric():
s+=1
elif i.isspace():
k+=1
else:
q+=1
print('中英文字母的个数:{}\n 数字的个数:{}\n 空格的个数:{}\n 其他字符个数:{} \n'.format(y,s,k,q) )

4.3
import math as m
a=int(input('请输入一个整数:') )
b=int(input('请再次输入一个整数:') )
n=0

if语句是求最大公倍数

if b==0:
n=a
print('最大公约数是:{}'.format(n))
else:
n=m.gcd(b,a%b)
print('最大公约数是:{}'.format(n))#求余为0时,即返回'b'=0,则找到了最大公约数
x=(a*b)/n
print('最大公倍数是:{}'.format(x))

4.4
4.5
4.6
4.7
4.8

posted @ 2025-04-04 16:56  荔枝Y  阅读(7)  评论(0)    收藏  举报