Python:不能将关键字用作表达式

Python:不能将关键字用作表达式,python,Python,我应该设计一个程序,让用户在数组中输入12个月的总降雨量。该程序应计算并显示该年的总降雨量、月平均降雨量以及降雨量最高和最低的月份 def main(): months = [0] * 12 name_months = ['Jan','Feb','Mar','Apr','May','Jun', \ 'Jul','Aug','Sep','Oct','Nov','Dec'] def total(months): total = 0

我应该设计一个程序,让用户在数组中输入12个月的总降雨量。该程序应计算并显示该年的总降雨量、月平均降雨量以及降雨量最高和最低的月份

def main():
    months = [0] * 12
    name_months = ['Jan','Feb','Mar','Apr','May','Jun', \
               'Jul','Aug','Sep','Oct','Nov','Dec']
    def total(months):
        total = 0
        for num in months:
            total += num
        return total
    for index in range(12):
        print ('Enter the amount of rain in',
        months[index] = input(name_months[index] + ': '))
    print ('The total is', total(months), 'mm.')
    avarage = total(months) / 12.0
    print ('The avarage rainfall is', avarage, 'months')
    m_copy = months[0:]
    months.sort()
    lowest = months[0]
    print ('Lowest is', lowest, 'in',)
    lows = []
    for i in range (12):
        if m_copy[i] == lowest:
            lows.append( name_months[i] )
    for i in range (len(lows)):
        print (lows[i],)
        if i < len(lows)-1: print ('and',)
    print
    highest = months[11]
    print ('Highest is', highest, 'in',)
    highs = []
    for i in range (12):
        if m_copy[i] == highest:
            highs.append( name_months[i] )
    for i in range (len(highs)):
        print (highs[i],)        
        if i < len(highs)-1: print ('and',)
    print

main()

它一直在说我不能使用关键字作为表达式,我已经盯着它看了一个多小时了,现在我可能已经看了一些东西。

Python通常会给出错误的行号,这对于解决这个问题是非常宝贵的

如果您在Python 2中运行此功能,第一个问题在于:

print ('Enter the amount of rain in',
months[index] = input(name_months[index] + ': '))
第一行没有右括号,第二行有太多右括号

当我把它改成

print ('Enter the amount of rain in'),
months[index] = input(name_months[index] + ': ')
它可以工作,尽管使用奇怪的列表输出格式,但至少在Python 2.7 v3中可能会有所不同:

Enter the amount of rain in Jan: 1
Enter the amount of rain in Feb: 2
Enter the amount of rain in Mar: 3
Enter the amount of rain in Apr: 4
Enter the amount of rain in May: 5
Enter the amount of rain in Jun: 6
Enter the amount of rain in Jul: 7
Enter the amount of rain in Aug: 8
Enter the amount of rain in Sep: 9
Enter the amount of rain in Oct: 0
Enter the amount of rain in Nov: 1
Enter the amount of rain in Dec: 2
('The total is', 48, 'mm.')
('The avarage rainfall is', 4.0, 'months')
('Lowest is', 0, 'in')
('Oct',)

('Highest is', 9, 'in')
('Sep',)

顺便说一句,如果Python已经提供了一个非常好的满足您需要的求和功能,我就不会实现像total这样的函数。

我无法重现您提到的错误,但是您的代码似乎有点混乱,无法作为注释来解释。您似乎混合使用了Python 2和Python 3代码,这是行不通的-例如,您似乎期望这样:

print ('and',)
打印字符串“and”,但将打印通常产生的换行符加上。在Python 2中,它打印以下内容:

('and',)
在Python 3中,这是:

'and'
在这两种情况下,都使用了新行

之所以会出现这种情况,是因为在Python2中逗号抑制了换行符,但括号不是语句的一部分——因此,您告诉它打印一个一项元组

在Python 3中,它是一个普通的函数调用,因此括号是它的一部分,您可以告诉它在完成打印后在末尾放置一个任意字符串-它默认为换行符,但您可以将其更改为,例如,如下所示的空格:

打印“和”,结束=“”

你似乎也期望:

print
放一个空行。在Python2中,它将。在Python 3中,它不会做任何事情-现在需要调用该函数:

print()
使用输入的方式也会遇到问题:

months[index] = input(name_months[index] + ': ')
在Python2中,使用输入函数被认为是一个坏主意,通常建议使用原始输入。现在,input执行原始输入的操作,即返回字符串。代码的其余部分假设每个月[索引]都是一个数字,因此:

total += num
我会做算术。当来自months的num是一个字符串时,实际上会得到一个错误。解决这个问题的方法是告诉Python在获得它后将其转换为数字:

months[index] = int(input(name_months[index] + ': '))    

你能用{}大括号编辑你的原始文章并正确格式化代码吗?即使只用粗体文本也不要大声喊叫。请不要在问题标题中包含“问题”;总结一下你的问题。不知何故,我怀疑这段代码是否是SSCCE。错误信息在哪一行?这将直接告诉您问题所在。您在Python3.x代码上使用Python2.x是否存在问题?Python2.x抱怨代码第12行的打印,但它抱怨的符号是嵌入的赋值。