如何使用averageHighTemps函数将天气数据库作为参数,并返回每月高温的平均值?


def main():
highTemps = [-3, -2, 3, 11, 19, 23, 26, 25, 20, 13, 6, 0]
lowTemps = [-11, -10, -5, 1, 8, 13, 16, 15, 11, 5, -1, -7]
weatherDB = createDB(highTemps, lowTemps)  

for m in weatherDB: 
for t in weatherDB[m]: 
print(m, t, weatherDB[m][t])
m = input("Enter a Month Name: ")
if m in weatherDB: 
print(weatherDB[m])
else: 
print("Month not found")
def tempByMonth(weatherDB, month):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
weatherDB = {}
for i in range(len(months)):
month = months[i]
weatherDB[month]
return weatherDB
def averageHighTemps(weatherDB):
#Here is the function
return

#DO NOT change this function:
def createDB(highTemps, lowTemps):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
weatherDB = {}
for i in range(len(months)):
month = months[i]
weatherDB[month]={"high":highTemps[i],"low":lowTemps[i]}
return weatherDB

main()

如何使用averageHighTemps函数将天气数据库作为参数,并返回每月高温的平均值?此代码应该只在averageHighTemps函数中。

我不完全确定你想要什么,但这将weatherDB作为参数,创建每个月的高温列表,并从中计算平均值。

def averageHighTemps(weatherDB):
highs = [month["high"] for month in weatherDB.values()]
avg = sum(highs) / len(weatherDB)
return avg

你可能会想在你的main()函数中:

print(f"The average high temperature was: {averageHighTemps(weatherDB)}")

最新更新