当前分类:300例题>>正文

Python示例:计算 10 个数字并找出它们的总和和平均值

来源:互联网   更新时间:2023年6月18日  

Python 示例

写一个 Python示例来读取 10 个数字,并找出它们的总和和平均值。在这个 Python 示例中,for 循环范围从 1 到 10 迭代,读取用户输入的 10 个数字,并在输入时找到总和。接下来,我们将总和除以 10 得到平均值。

Sum = 0

print("Please Enter 10 Numbers\n")
for i in range(1, 11):
    num = int(input("Number %d = " %i))
    Sum = Sum + num

avg = Sum / 10

print("The Sum of 10 Numbers     = ", Sum)
print("The Average of 10 Numbers = ", avg)

这个 Python示例从用户输入中读取 10 个数字,并使用 while 循环找到它们的总和和平均值。

Sum = 0

print("Please Enter 10 Numbers\n")
i = 1
while(i <= 10):
    num = int(input("Number %d = " %i))
    Sum = Sum + num
    i = i + 1

avg = Sum / 10

print("The Sum of 10 Numbers     = ", Sum)
print("The Average of 10 Numbers = ", avg)
Please Enter 10 Numbers

Number 1 = 34
Number 2 = 2
Number 3 = 65
Number 4 = 45
Number 5 = 76
Number 6 = -98
Number 7 = 33
Number 8 = 66
Number 9 = 88
Number 10 = 122
The Sum of 10 Numbers     =  433
The Average of 10 Numbers =  43.3
本文固定链接:https://6yhj.com/leku-p-4284.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 百度爬虫