2
这是更新的程序我至今写:平均降雨量计算器
# This program averages rainfall per month. It asks the user for the number
# of years. It will then display the number of months, the total inches of
# rainfaill, and the average rainfall per month for the entire period.
# Get the number of years.
total_years = int(input('Enter the amount of years: '))
# Get the amount of rainfall for each month of each year.
for years in range(total_years):
# Initialize the accumulator.
total = 0.0
print('Year', years + 1)
print('----------------')
for month in ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'):
inches = float(input(month))
total += inches
total_inches = total
total_month = total_years * 12
average_inches = total/total_month
# Display the average.
print('The total number of months is: ', total_month)
print('The total inches of rainfall is: ', total_inches)
print('The average rainfall per month for the entire period is: ', average_inches)
print()
这是试图测试代码时,我得到了新的错误消息:在
Traceback (most recent call last): File
"C:/Users/Alex/Desktop/Programming Concepts/Homework 2/Chapter
5/Average Rainfall maybe.py", line 23, in <module>
average_inches = total/month
TypeError: unspupported operand type(s) for /: 'float' and 'str'
任何想法如何修复/改进此代码?
现在,我需要解决的是我的计算。我认为他们错了(23-27行)。
不提供带有两个参数的输入,删除',月份'并在传入的字符串中添加有奖月份 – rlemon 2012-07-13 23:13:46
'input('输入在月份%s'%月份中测量的英寸数量'' – 2012-07-13 23:25:34
请勿使用输入。使用raw_input。 – 2012-07-13 23:28:28