新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:找到大于平均值的列表項
創(chuàng)新互聯(lián)python教程:

專注于為中小企業(yè)提供成都網(wǎng)站建設、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)元寶山免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
寫一個 Python 程序來查找大于平均值的列表項。這個 python 示例允許我們輸入列表項,然后我們會找到列表項的平均值。接下來,for 循環(huán)將迭代列表項,if 條件根據(jù)列表平均值檢查每個項。最后,如果大于平均值,則打印該列表項。
listAvg = []
Number = int(input("Total Number of List Items = "))
for i in range(1, Number + 1):
value = int(input("Enter the %d List Item = " %i))
listAvg.append(value)
total = sum(listAvg)
avg = total / Number
print('\nList Sum = {0} and Average = {1}'.format(total, avg))
print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
if listAvg[i] > avg:
print(listAvg[i], end = ' ')這個 Python 示例使用 for 循環(huán)查找大于平均值的列表項。
listAvg = []
Number = int(input("Total Number of List Items = "))
total = 0
for i in range(Number):
value = int(input("Enter the %d List Item = " %(i+1)))
listAvg.append(value)
total = total + listAvg[i]
avg = total / Number
print('\nList Sum = {0} and Average = {1}'.format(total, avg))
print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
if listAvg[i] > avg:
print(listAvg[i], end = ' ')Total Number of List Items = 7
Enter the 1 List Item = 22
Enter the 2 List Item = 9
Enter the 3 List Item = 11
Enter the 4 List Item = 18
Enter the 5 List Item = 29
Enter the 6 List Item = 139
Enter the 7 List Item = 44
List Sum = 272 and Average = 38.857142857142854
Total List Items Greater than the List Average
139 44 Python 程序使用 while 循環(huán)查找大于平均值的列表項。
listAvg = []
Number = int(input("Total Number of List Items = "))
total = 0
i = 1
while(i <= Number):
value = int(input("Enter the %d List Item = " %(i+1)))
listAvg.append(value)
i = i + 1
i = 0
while(i < Number):
total = total + listAvg[i]
i = i + 1
avg = total / Number
print('\nList Sum = {0} and Average = {1}'.format(total, avg))
print('Total List Items Greater than the List Average')
for i in range(len(listAvg)):
if listAvg[i] > avg:
print(listAvg[i], end = ' ')Total Number of List Items = 9
Enter the 2 List Item = 4
Enter the 3 List Item = 11
Enter the 4 List Item = 3
Enter the 5 List Item = 2
Enter the 6 List Item = 9
Enter the 7 List Item = 22
Enter the 8 List Item = 7
Enter the 9 List Item = 15
Enter the 10 List Item = 17
List Sum = 90 and Average = 10.0
Total List Items Greater than the List Average
11 22 15 17 本文題目:Python程序:找到大于平均值的列表項
分享URL:http://m.5511xx.com/article/dhdsede.html


咨詢
建站咨詢
