python 3 上打印的语法问题

syntax issue with print on python 3

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" 处存在语法错误。怎么回事?

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

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

删除“=”后的第一个左括号“(”,或在行尾添加另一个右括号“)”。

您的 populationInFiveYears 行开头有一个额外的“(”。

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

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

使用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)

在打印时将所有的float转换为int。由于总人口不可能是浮点数,因为没有 0.293283 人这样的东西。

正如其他人所说,您在

末尾缺少括号 )
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?

''' 不接受评论。它是一个 multi-line 字符串...

如果您仔细查看错误消息,会发现有一个插入符号 (^) 指向解析器声明为语法错误的标记。

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

它指向 print 令牌。这表明解析器不希望 print 作为下一个标记。因为那是该行的第一个标记,所以回到上一行并在那里寻找问题。正如其他人指出的那样,您在上一行中的参数不匹配。