Python 平均降雨量计算器

Python 平均降雨量计算器,python,average,Python,Average,这是我到目前为止编写的更新程序: # 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 th

这是我到目前为止编写的更新程序:

# 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'
回溯(最近一次调用上次):文件
“C:/Users/Alex/桌面/编程概念/作业2/章节
5/平均降雨量可能为.py“,第23行,in
平均英寸=总数/月
TypeError:/:“float”和“str”的未支持的操作数类型
关于如何修复/改进此代码有何想法


现在,我只需要修正我的计算。我认为它们是错误的(第23-27行)。

错误消息引用了错误发生的位置:

average_inches = total / month
具体来说,

TypeError: unspupported operand type(s) for /: 'float' and 'str'
…的意思是它不能用字符串(
月份
)除以浮动(
总计

month
是一个完全错误的除法(它只是一个包含“一月”之类的字符串)。。你想除以

作为提示,我建议从以下几点开始:

ALL_MONTHS = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'):
然后将循环更改为:

for month in ALL_MONTHS:

这样,您可以在以后再次参考
所有月份

错误消息引用了错误发生的位置:

average_inches = total / month
具体来说,

TypeError: unspupported operand type(s) for /: 'float' and 'str'
…的意思是它不能用字符串(
月份
)除以浮动(
总计

month
是一个完全错误的除法(它只是一个包含“一月”之类的字符串)。。你想除以

作为提示,我建议从以下几点开始:

ALL_MONTHS = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'):
然后将循环更改为:

for month in ALL_MONTHS:

这样,您可以稍后再次引用所有月份。

不提供两个参数的输入,删除
,month
,并在传递的字符串中添加额定月份
输入('输入以月份%s“%month”为单位测量的英寸)
不使用输入。仅在Python 2中使用原始输入。@ColinDunklau。在Python3(看起来是这样)中,您应该使用
input
@Liquid\u Fire很好,谢谢!不要提供带有两个参数的输入,删除
,month
,并在传递的字符串中添加额定月份
输入('输入以月份%s“%month”测量的英寸)
不要使用输入。仅在Python 2中使用原始输入。@ColinDunklau。在Python3(看起来是这样)中,您应该使用
input
@Liquid\u Fire很好,谢谢!我刚拍了两张。由于错误消息已被编辑,我的答案不再回答以下问题:P,无论如何+1。我只是做了一个双重判断。由于错误消息已被编辑,我的回答不再回答以下问题:P,无论如何+1。