我正在使用Python进行作业,需要将降雨量与每个月相关联。我需要打印降雨量的高低月份,但是目前编写代码的方式只能打印实际数量,而不能打印与该数量对应的月份。我想我可以在enter_rainfall函数下将两者关联起来,但是不确定如何将两者关联起来。 任何建议表示赞赏。
def main():
months_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
rain = enter_rainfall()
average = avg_rainfall(rain)
lowhigh = low_High(rain)
print(format(average, '.2f'))
def enter_rainfall():
rain_fall = []
months = 12
for months_list in range(months):
rain_amount = float(input('Please enter the amount of rain:',))
rain_fall.append(rain_amount)
return(rain_fall)
def avg_rainfall(rain):
total = 0
for value in rain:
total += value
average = total / len(rain)
return(average)
def low_High(rain, months_list):
low = min(rain)
month = months_list[rain.index(low)]
print(month)
high = max(rain)
print(high)
main()
答案 0 :(得分:1)
致电low_High
时,实际上并没有传递月份列表(month_list
)。相反,您应该这样称呼它:low_High(rain, months_list)
。当您打印最小值和最大值时,您可能要同时打印月份和月份值-现在,您要同时打印月份和月份的最小值和最大值。
def low_High(rain, months_list):
low_val = min(rain)
low_month = months_list[rain.index(low_val)]
high_val = max(rain)
high_month = months_list[rain.index(high_val)]
print('{month} had the least amount of rain with {val}'.format(month=low_month, val=low_val))
print('{month} had the most amount of rain with {val}'.format(month=high_month, val=high_val))
如果您想在提示用户时跟踪月份,则可以通过修改enter_rainfall
来访问该数组:
def main():
...
rain = enter_rainfall(month_list)
...
def enter_rainfall(month_list)
for month in month_list:
rain_amount = float(input('Please enter the amount of rain for {month}:'.format(month=month),))
rain_fall.append(rain_amount)
...
答案 1 :(得分:1)
您可以设置一个字典,记录下相应月份的降雨,然后可以使用字典查找min
和max
> values
以及使用keys
def enter_rainfall():
months_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
rain_fall = {i: 0 for i in months_list}
for k in rain_fall:
rain_fall[k] += float(input('Please enter the amount of rain for {}: '.format(k)))
return rain_fall
def low_High(dicta):
all_rain = [v for v in dicta.values()]
low = ([k for k in dicta if dicta[k] == min(all_rain)], min(all_rain))
high = ([k for k in dicta if dicta[k] == max(all_rain)], max(all_rain))
return low, high
x = enter_rainfall()
low, high = low_High(x)
print(low, high)
Please enter the amount of rain for January: 10 Please enter the amount of rain for February: 11 Please enter the amount of rain for March: 12 Please enter the amount of rain for April: 10 Please enter the amount of rain for May: 22 Please enter the amount of rain for June: 18 Please enter the amount of rain for July: 1 Please enter the amount of rain for August: 9 Please enter the amount of rain for September: 2 Please enter the amount of rain for October: 22 Please enter the amount of rain for November: 8 Please enter the amount of rain for December: 10 (['July'], 1.0) (['May', 'October'], 22.0)
如果两个或两个月以上的降雨与max
或min
相同,则所有对应的月份都将返回列表中
答案 2 :(得分:0)
print
和return
可以接受多次返回并打印字符串。您不需要限制数量。
您只需要这样更改main
。
def enter_rainfall():
rain_fall = []
months = 12
for months_list in range(months):
rain_amount = float(input('Please enter the amount of rain:',))
rain_fall.append(rain_amount)
print(months_list,rain_amount)
# it will print corresponding month and its rain amount.
return(rain_fall)