首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用最小索引时未显示该索引

调用最小索引时未显示该索引
EN

Stack Overflow用户
提问于 2020-04-14 14:31:00
回答 2查看 27关注 0票数 1

我们要让用户在列表中输入每12个月的总降雨量。程序应该计算和显示全年的总降雨量,即月平均降雨量。

问题:最大值很好,但最小值停留在0

代码:

代码语言:javascript
运行
复制
Rainfall=[0]*13
i=1
total=0
print("Enter the Rainfall each month")
while i<=12:
    print('Month #',i, ': ',end=' ')
    Rainfall[i]=float(input())
    total+=Rainfall[i]

    i+=1
ave=total/12

High = max (Rainfall)
Low = min(Rainfall)


print("total Amount= ",total)
print("Average Average Amount {:0.2f}".format(ave))


print ("The months with the highest value are : ")
print ([i for i, j in enumerate(Rainfall) if j == High])
print ("The months with the Lowest value are : ")
print ([i for i, j in enumerate(Rainfall) if j == Low])

output:Enter the Rainfall each month
Month # 1 :  1
Month # 2 :  1
Month # 3 :  399
Month # 4 :  900
Month # 5 :  900
Month # 6 :  900
Month # 7 :  900
Month # 8 :  2323
Month # 9 :  42
Month # 10 :  100
Month # 11 :  10000
Month # 12 :  10000
total Amount=  26466.0
Average Average Amount 2205.50
The months with the highest value are : 
[11, 12]
The months with the Lowest value are : 
[0]

Process finished with exit code 0
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-14 14:41:05

因为循环从i=1开始,所以您没有填充列表Rainfall中的第一个索引,但是列表的第一个索引是0(它的值是0)。

试试这个:

代码语言:javascript
运行
复制
Rainfall=[0]*12
i=0
print("Enter the Rainfall each month")
while i<len(Rainfall):
    print('Month #',i, ': ',end=' ')
    Rainfall[i]=float(input())
    i+=1

total = sum(Rainfall)
ave=total/len(Rainfall)

High = max (Rainfall)
Low = min(Rainfall)


print("total Amount= ",total)
print("Average Average Amount {:0.2f}".format(ave))


print ("The months with the highest value are : ")
print ([i+1 for i, j in enumerate(Rainfall) if j == High])
print ("The months with the Lowest value are : ")
print ([i+1 for i, j in enumerate(Rainfall) if j == Low])
票数 1
EN

Stack Overflow用户

发布于 2020-04-14 14:44:31

您的数组有13个元素(而不是12个)。数组从索引0开始,但您从1开始填充它。

降雨量始终为0

调整你的代码。变化

代码语言:javascript
运行
复制
Rainfall=[0]*12
i=0

....

while i<12
...
print('Month #',i+1, ': ',end=' ')
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61201871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档