在python 3上打印的语法问题

时间:2017-01-31 00:02:56

标签: python syntax syntax-error

def main():

    birthRate = (60 / 7)    # Births in a minute
    deathRate = (60 / 13)       # Deaths in a minute
    immigrantRate = (60 / 45)       # New immigrants in a minute
    minutesInaYear = (24 * 60 * 365)        # The number of seconds in a years
    minutesInaLeapYear = (24 * 60 * 366)
    currentPopulation = 312032486
    totalRate = (birthRate - deathRate + immigrantRate)

    populationIncreaseInaYear = (totalRate * minutesInaYear) # Calculates the increase in population after a year

    populationInaYear = (currentPopulation + populationIncreaseInaYear) # Total population after a year

    populationInTwoYears = (currentPopulation + (2 * populationIncreaseInaYear))

    populationInThreeYears = (currentPopulation + (3 * populationIncreaseInaYear))

    populationInFourYears = (currentPopulation + (4 * populationIncreaseInaYear))

    populationInFiveYears = ((populationInFourYears +  (totalRate * minutesInaYear))

    print("The population after one year is: " , populationInaYear)  '''PRINT FUNCTION DOESN'T WORK?'''

    print("The population after two years is: " , populationInTwoYears)
    print("The population after three years is: " , populationInThreeYears)
    print("The population after four years is: " , populationInFourYears)
    print("The population after five years is: " , populationInFiveYears)

main()

为什么打印不工作?即使我用括号替换括号中的所有内容,它也表示在" t"在印刷品。发生了什么事?

6 个答案:

答案 0 :(得分:3)

行中有一个不匹配的左括号:

(IF (field not null) then field)

要么删除第一个左括号'(' =' ='或者添加另一个右括号')'在最后一行。

答案 1 :(得分:1)

您在populationInFiveYears = ((populationInFourYears + (totalRate * minutesInaYear)) 行的开头有一个额外的"("

答案 2 :(得分:1)

您需要关闭上一行的圆括号。正确的代码是:

populationInFiveYears = ((populationInFourYears +  (totalRate * minutesInaYear)))

答案 3 :(得分:1)

正如其他人所说,你在

末尾错过了一个括号)
populationInFiveYears = ((populationInFourYears +  (totalRate * minutesInaYear))

但是还有一个错误:

print("The population after one year is: " , populationInaYear)  '''PRINT FUNCTION DOESN'T WORK?'''

如果您想对其进行评论,请使用#

print("The population after one year is: " , populationInaYear)  # PRINT FUNCTION DOESN'T WORK?

'''不是评论。它是一个多行字符串......

答案 4 :(得分:1)

如果仔细查看错误消息,就会有一个指向解析器声称为语法错误的令牌的插入符号(^)。

  File "C:\test.py", line 22
    print("The population after one year is: " , populationInaYear)  '''PRINT FUNCTION DOESN'T WORK?'''
        ^

它指向print令牌。这表明解析器并不期望print作为下一个标记。由于这是该行的第一个令牌,请返回上一行并在那里查找问题。正如其他人所指出的那样,您在前一行上的参数不匹配。

答案 5 :(得分:0)

使用Python进行计算并不需要括号,Python也会自动遵循操作顺序,因此:

birthRate = 60 / 7    
deathRate = 60 / 13
immigrantRate = 60 / 45
minutesInaYear = 24 * 60 * 365
minutesInaLeapYear = 24 * 60 * 366
currentPopulation = 312032486
totalRate = birthRate - deathRate + immigrantRate

populationIncreaseInaYear = totalRate * minutesInaYear

populationInaYear = currentPopulation + populationIncreaseInaYear

populationInTwoYears = currentPopulation + 2 * populationIncreaseInaYear

populationInThreeYears = currentPopulation + 3 * populationIncreaseInaYear

populationInFourYears = currentPopulation + 4 * populationIncreaseInaYear

populationInFiveYears = populationInFourYears +  totalRate * minutesInaYear 
#you got an extra bracket after '=' which caused the error

print("The population after one year is: " , populationInaYear)  
print("The population after two years is: " , populationInTwoYears)
print("The population after three years is: " , populationInThreeYears)
print("The population after four years is: " , populationInFourYears)
print("The population after five years is: " , populationInFiveYears)

我也建议这样做:

print("The population after one year is: %i" %populationInaYear)  
print("The population after two years is: %i" %populationInTwoYears)
print("The population after three years is: %i" %populationInThreeYears)
print("The population after four years is: %i" %populationInFourYears)
print("The population after five years is: %i" %populationInFiveYears)

在打印时将所有浮点数转换为int。因为总人口不可能是浮动的,因为那里没有0.293283人。