gpt4 book ai didi

Python 列表和函数,查找降雨量的最大/最小月份

转载 作者:太空宇宙 更新时间:2023-11-04 05:11:59 25 4
gpt4 key购买 nike

我的程序应该获取每个月的降雨量,并计算当年的总降雨量、月平均降雨量以及降雨量最高(最大)和最低(最小)的月份。

除最高月和最低月的产出外,一切都按计划进行。我需要这个来显示降雨量最高和最低的月份的名称。我可以获得要显示的正确值,只是不是月份的名称。

months = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
values = []
year = []

for i in months :
values.append(float(input("Enter total rain for " + i + ": ")))
print()



def total():
print("The total rainfall for the year is %.2f" % sum(values))
total()

def average():
print("The average monthly rainfall is %.2f" % float(sum(values)/ 12))
average()

def highest():
print("The highest monthly rainfall is", max(values))
highest()

def lowest():
print("The lowest monthly rainfall is", min(values))
lowest()

最佳答案

您可以 zip valuesmonths 并获取 minmax,作为 (值、月)。确保将值放在首位,以便用于排序。

>>> months = ["January", "February", "March", "April",
... "May", "June", "July", "August",
... "September", "October", "November", "December"]
>>> values = [47, 2, 28, 9, 4, 64, 28, 94, 1, 9, 4, 23] # or whatever
>>> val, month = min(zip(values, months))
>>> print("The lowest rainfall was %r in %s" % (val, month))
The lowest rainfall was 1 in September

关于Python 列表和函数,查找降雨量的最大/最小月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com