Python 类型错误:Can';t转换为';int';对象到str隐式/排序数组问题

Python 类型错误:Can';t转换为';int';对象到str隐式/排序数组问题,python,python-3.x,sorting,Python,Python 3.x,Sorting,代码的某一部分有问题(我不熟悉编码,并尝试通过StackOverflow寻求帮助): def总降雨量(降雨量): 总雨量=0 月份=0 月份

代码的某一部分有问题(我不熟悉编码,并尝试通过StackOverflow寻求帮助):

def总降雨量(降雨量):
总雨量=0
月份=0
月份
TypeError:无法将“int”对象隐式转换为str

我尝试了多种方法来更改代码,使其显式成为字符串,因为它仍然会给我带来各种问题

我还很难增强代码,以便按升序对数组进行排序并显示它包含的值

完整代码如下:

def main ():
    rainfall = rainInput ()
    totalRain = totalRainfall (rainfall)
    average_Rainfall = averageRainfall (totalRain)
    highestMonth, highestMonthly = highestMonthNumber (rainfall)
    lowestMonth, lowestMonthly = lowestMonthNumber (rainfall)
    print #this is for spacing output
    print ('The total rainfall for the year was: ' +str(totalRain) + ' inche(s)')
    print #this is for spacing output
    print ('The average rainfall for the year was: ' +str(average_Rainfall) +\
          ' inche(s)') 
    print #this is for spacing in output
    print ('The highest amount of rain was', highestMonthly, 'in' , highestMonth)
    print #this is for spacing in output
    print ('The lowest amount of rain was', lowestMonthly, 'in' , lowestMonth)

def rainInput ():
    rainfall = ['January','Febuary','March','April','May','June','July','August'\
    ,'September','October','November','December']
    month = 0
    while month < len(rainfall):
        rainfall[month] = input ('Please enter the amount for month ' + str\
        (month + 1) + ': ')
        month += 1

    return rainfall

def totalRainfall (rainfall):
    totalRain = 0
    month = 0
    while month < len(rainfall):
        totalRain = rainfall[month] + totalRain
        month += 1

    return totalRain

def averageRainfall (totalRain):
    average_Rainfall = totalRain / 12

    return average_Rainfall

def highestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    highestMonthly = 0
    for m, n in enumerate(rainfall):
        if n > highestMonthly:
            highestMonthly = n
            highestMonth = m

    return month[highestMonth], highestMonthly

def lowestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    lowestMonthly = 0
    for m, n in enumerate(rainfall):
        if n < lowestMonthly:
            lowestMonthly = n
            lowestMonth = m

    return month[lowestMonth], lowestMonthly

main()
defmain():
降雨=降雨输入()
总降雨量=总降雨量(降雨量)
平均降雨量=平均降雨量(总降雨量)
最高月数,最高月数=最高月数(降雨量)
lowestMonth,lowestMonthly=lowestMonthNumber(降雨量)
打印#用于间距输出
打印('当年总降雨量为:'+str(总降雨量)+'英寸)
打印#用于间距输出
打印('当年的平均降雨量为:'+str(平均降雨量)+\
"寸(s)
打印#用于输出中的间距
打印('最高降雨量为',最高月降雨量,'年',最高月降雨量)
打印#用于输出中的间距
打印('最低降雨量为',最低月份,'英寸',最低月份)
def rainInput():
降雨量=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
月份=0
月份最高每月:
最高每月=n
最高月份=m
返回月份[最高月份],最高月份
def最低月数(降雨量):
月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
最低每月=0
对于枚举中的m,n(降雨量):
如果n<每月最低值:
最低月份=n
最低月数=m
返回月[lowestMonth],lowestMonthly
main()

如果已在数组中存储字符串,则需要在添加之前将其转换为整数

def totalRainfall (rainfall):
    totalRain = 0
    month = 0
    while month < len(rainfall):
        totalRain = int(rainfall[month]) + totalRain
        month += 1

    return totalRain
def总降雨量(降雨量):
总雨量=0
月份=0
月份
如果您想将总降雨量作为每月的数字之和,只需在整数列表中使用
sum()
。但正如您的错误所示,您有一个字符串列表,您必须显式地转换这些字符串

类似于

def totalRainfall (rainfall):
    return sum([int(x) for x in rainfall])
列表是字符串的问题将继续困扰您,因此作为一个快速解决方案,我建议您更改这一行

    rainfall[month] = input ('Please enter the amount for month ' + str\
    (month + 1) + ': ')

这样,您的列表只包含数字,而所有其他比较都会起作用

您还应该在
lowestMonthNumber
函数中添加此初始化,以避免
UnboundLocalError:赋值前引用的局部变量“lowestMonth”:

lowestMonth = 0

请注意,通过将
lowestMonthly
初始化为
0
,您很可能永远不会得到正确的结果,因为任何月份的降雨量都不太可能少。

谢谢您,这帮了大忙。仍然很难理解其中的一些概念。您是否也能帮助解决这个问题:我也很难增强代码,以按升序(字母顺序)对数组进行排序,并显示它包含的值,可能是使用自定义键函数<代码>已排序(['c','a','a','b'])
->
['a','a','b','c']
    rainfall[month] = int(input('Please enter the amount for month ' + str\
    (month + 1) + ': '))
lowestMonth = 0