1、随机产生一个
3
位整数,将它的十位数字变为
0
。假设生成的
3
位整
数为
738
,则输出为
708.
import random
rdint = random.randint(100, 999)
print("生成的三位随机数是", rdint)
RDint = str(rdint)
RDintlist = list(RDint)
RDintlist[1] = '0'
print("输出三位随机数是", ''.join(RDintlist))
2、输入整数 x, y, z, 若 x^2+y^2+z^2 > 1000, 则输出千位以上的数字,否则输出三个数的和。假设 x=10, y=25, z=30, x^2+y^2+z^2 = 1625 > 1000, 则输出 1;若 x = 10, y = 25, z = 10, x2 + y2 + z2 = 825 > 1000, 则输 出 45。
x, y, z = map(int, input("请输入三个整数,并用空格隔开:").split())
pingfanghe = x**2+y**2+z**2 #计算平方和
if pingfanghe > 1000:
print(pingfanghe // 1000)
else:
print(x+y+z)
3、请编写一个 Python 程序,在给定年限 N 和年利率 r 的情况下,计算 当贷款金额为 P 时,每月需还贷的金额,每月还贷公式为
N, r = map(float, input("请输入给定年限N和年利率r,并用空格隔开:").split()) #处理输入的参数
print(N,r)
N_1 = 12*N
r_1 = r/12
P = float(input("请输入贷款金额P:"))
M = (P*r_1*(1+r_1)**N_1)/(((1+r_1)**N_1)-1)
print(M)
4、编写函数,接受一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。
num_Store = [] # 存储数字
up_Store = [] # 存储大写字母
low_Store = [] # 存储小写字母
other_Store = [] # 存储其他字符
def classify(a):
for i in a:
if i.isdigit():
num_Store.append(i)
elif i.isupper():
up_Store.append(i)
elif i.islower():
low_Store.append(i)
else:
other_Store.append(i)
return num_Store, up_Store, low_Store, other_Store
String_input = input("请输入一个字符串")
Up, Low, Num, Other = classify(String_input)
print('大写字母的个数:{}'.format(len(Up)))
print('小写字母的个数:{}'.format(len(Low)))
print('数字的个数:{}'.format(len(Num)))
print('其他字符的个数:{}'.format(len(Other)))
[Up, Low, Num, Other] = map(tuple, [Up, Low, Num, Other])
print(Up, Low, Num, Other)