我正在试图找出编程课程介绍的一些功课。看来我已经走了很长一段路,找到了大部分信息。我必须设计一个程序,在12个月的每个月中,用户输入总降雨量。它应该计算并显示当年的总降雨量,平均降雨量以及最高和最低量的月份。我可以让我的程序执行所有操作,但显示最高和最低的月份名称。就像我之前提到的,我知道我已经采取了很长的路要走,而且很可能不是最有效的途径。这是我的代码。
year=[]
jan=float(input('Please enter Jan rainfall: '))
feb=float(input('Please enter Feb rainfall: '))
mar=float(input('Please enter Mar rainfall: '))
apr=float(input('Please enter Apr rainfall: '))
may=float(input('Please enter May rainfall: '))
jun=float(input('Please enter Jun rainfall: '))
jul=float(input('Please enter Jul rainfall: '))
aug=float(input('Please enter Aug rainfall: '))
sep=float(input('Please enter Sep rainfall: '))
oct=float(input('Please enter Oct rainfall: '))
nov=float(input('Please enter Nov rainfall: '))
dec=float(input('Please enter Dec rainfall: '))
year.extend((jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec))
def lowest():
print('The minimum rainfall is', min(year))
lowest()
def highest():
print('The most rainfall is', max(year))
highest()
def total():
print('The total rainfall is', sum(year))
total()
def average():
print('The average rainfall is', float(sum(year))/len(year))
average()
当我这样做时,我得到输入提示,最小值,最大值,总数和平均值。我只是不确定从哪里获得最小值和最大值的月份名称。如果有一个更快的方式我也都是为了它。感谢。
答案 0 :(得分:1)
我可能会在这里使用dict
:
year = {}
year['jan'] = float(input('Please enter Jan rainfall: '))
...
现在要达到最低限度:
def lowest():
print('The minimum rainfall is', min(year.values()))
lowest()
如果你想知道哪个月最低:
def lowest2():
print('The month of minimum rainfall is', min(year, key=lambda x:year[x]))
lowest2()
答案 1 :(得分:0)
由于您无法使用词典,因此可以为月份名称单独列出:
months = ["jan", "feb", "mar", ... ]
然后在任何给定函数中使用返回值的索引从另一个列表中获取月份名称,例如
def lowest():
low = min(year)
month = months[year.index(low)]
print ...
答案 2 :(得分:0)
编辑:你添加了没有字典的要求,但我留下了我的答案,以防它对其他人有用。
如果你使用dict,你可以:https://stackoverflow.com/a/268285/341744
In [1]: months = {"jan":20, "feb:":32, "mar":45}
In [2]: max(months.iteritems(), key=operator.itemgetter(1))[0]
Out[2]: 'mar'
你可以自动输入,为了简洁我只用了两个月。
In [3]:
def input_rainfall(months):
r = {}
for month in months:
while True:
try:
rain = float(input("Enter rainfall for {}: ".format(month)))
r[month] = rain
break
except ValueError:
print("You must enter a float")
return r
months = input_rainfall(['jan', 'feb'])
Enter rainfall for jan: 3.4
Enter rainfall for feb: 3.41
In [4]: max(months.iteritems(), key=operator.itemgetter(1))[0]
Out[4]: 'feb'
In [5]: min(months.iteritems(), key=operator.itemgetter(1))[0]
Out[5]: 'jan'
答案 3 :(得分:0)
如果无法使用词典,您可以为列表中的元素实现具有不同数据表示的类似逻辑。您可以将其设置为“Jan | 10.0”格式,其中第一部分引用月份,第二部分引用降雨量值。在这种情况下的代码将是这样的:
year=[]
months = ["Jan","Feb","March","April","May","June","July","August","September","October","November","December"]
for i in months:
year.append(i+"|"+str(input('Please enter '+i+' rainfall: ')))
key_func = lambda s:s.split("|")[1]
split_func = lambda s,index:s.split("|")[index]
min_rainfall = min(year,key=key_func)
print("Minimum Rainfall: "+split_func(min_rainfall,1)+" in the month of "+split_func(min_rainfall,0))
max_rainfall = max(year,key=key_func)
print("Max Rainfall: "+split_func(max_rainfall,1)+" in the month of "+split_func(max_rainfall,0))
total = sum([float(split_func(p,1)) for p in year])
print("Total Rainfall: "+str(total))
print("Average Rainfall: "+str(float(total/len(year))))