def main():
num_z = 0
num_f = 0
sum = 0
data = 1
while data != 0:
data = int(input("请输入数字:"))
if data > 0:
num_z += 1
elif data < 0:
num_f +=1
sum += data
print("正数个数为:%d"%num_z)
print("负数个数为:%d"%num_f)
aver = sum / (num_z + num_f)
print("平均值为:%2f"%aver)
main()
#运行结果:
请输入数字:1
请输入数字:2
请输入数字:3
请输入数字:4
请输入数字:5
请输入数字:6
请输入数字:-1
请输入数字:-2
请输入数字:0
正数个数为:6
负数个数为:2
平均值为:2.250000
def main():
money = [1000]
for i in range(10):
x = money[i] * 1.05
money.append(x)
print("十年后的学费:%.2f"%money[10])
print("现在及十年后的学费:%.2f"%sum(money))
main()
#运行结果:
十年后的学费:1628.89
现在及十年后的学费:14206.79
def main():
count = 0
for i in range(100,1000):
if i % 5 == 0 and i % 6 == 0:
#数字间以空格分隔
print(i,end = ' ')
count += 1
if count % 10 ==0:
print("\n")
else:
continue
main()
#运行结果:
120 150 180 210 240 270 300 330 360 390
420 450 480 510 540 570 600 630 660 690
720 750 780 810 840 870 900 930 960 990
def main():
n2 = 0
n3 = 0
while n2 ** 2 < 12000:
n2 += 1
#最小的n满足n^2 > 12000的数字
print(n2)#110
while n3 ** 3 < 12000:
n3 += 1
#最小的n满足n^3 < 12000的数字
print(n3-1)#22
main()
def main():
sum = 0
for i in range(1,50001):
sum += 1/i
print(sum)
main()
#运行结果:
11.397003949278504
def main():
sum = 0
for i in range(1,98,2):
sum += i / (i + 2)
print(sum)
main()
#运行结果:
45.124450303050196
def main():
sum = 0
for i in range(1,100000):
sum += 4 * (-1) ** (i + 1) / (2 * i - 1)
print(sum)
main()
#运行结果:
#3.1416026536897204
def main():
for i in range(1,10000):
sum = 0
for j in range(1,i):
if i % j ==0:
sum += j
if i ==sum:
print(i)
main()
#运行结果:
6
28
496
8128
def main():
count = 0
for i in range(1,8,2):
for j in range(2,8):
if i != j:
print(i,j)
count += 1
print(count)
main()
#运行结果:
1 2
1 3
1 4
1 5
1 6
1 7
3 2
3 4
3 5
3 6
3 7
5 2
5 3
5 4
5 6
5 7
7 2
7 3
7 4
7 5
7 6
21
number = []
he = 0
for i in range(10):
data = float(input("请输入10个数字:"))
number.append(data)
average = sum(number) / len(number)
for x in number:
cha = (average - x) ** 2
he += cha
st = (he / (len(number)-1)) ** 0.5
print("The mean is %f"%average)
print("The Standard deviation is %f"%st)
#运行结果:
请输入10个数字:1
请输入10个数字:2
请输入10个数字:3
请输入10个数字:4
请输入10个数字:5
请输入10个数字:6
请输入10个数字:7
请输入10个数字:8
请输入10个数字:9
请输入10个数字:10
The mean is 5.500000
The Standard deviation is 3.027650