为什么我一直在Python中获取任意语法错误?

时间:2014-04-14 03:10:57

标签: python parsing math syntax

我在python中编写一个简单的脚本来返回GDP值。从第6行开始,它给出了一个"语法错误"没有进一步阐述。我评论了这个问题的所有行(第6行)和第16行,我在解析"时得到了#34; EOF。错误。我真的不知道该怎么做,因为我检查了不匹配的分隔符,不正确的语法等等,但我唯一能找到的就是我打印语句的方式,但因为它们都是相同的,只有一个人得到了一个不太可能出现的解析错误。这是代码:

y_i = int(input("What year did your alt history begin?"))
y_f = 2014
p_i = int(input("Enter population of your territory starting from your alt history.")
p_g = int(input("Enter average population growth from year to year in a numerical value. No percentages.")
p_f = (p_i(p_g - y_f) ** 2)/(10000 * y_i ** 2)

print("This is your nation's population.", p_f, "If you got an error, check that you put in all inputs correctly.")

gdp_capita = int(input("What is your GDP per capita? Please use the number only, in your own currency.")


gdp = pop * gdp_capita

print("This is your nation's GDP.", gdp, "If you get an error, please check that you entered everything in correctly.")

1 个答案:

答案 0 :(得分:0)

您的语法错误 。您缺少近括号。在第3行,第4行和第9行中,您有:int(input("...")没有最后一个括号!您还尝试使用p_f = p_i(p_g...)调用第5行中的整数。我以为你试图繁殖,所以我在那里放了一个乘法符号(星号)。此外,请确保您现在双星号意味着“强大的力量”。 2**3 = 8,而非6

将您的代码更改为:

y_i = int(input("What year did your alt history begin?"))
y_f = 2014
p_i = int(input("Enter population of your territory starting from your alt history."))
p_g = int(input("Enter average population growth from year to year in a numerical value. No percentages."))
p_f = (p_i*(p_g - y_f) ** 2)/(10000 * y_i ** 2)

print("This is your nation's population.", p_f, "If you got an error, check that you put in all inputs correctly.")

gdp_capita = int(input("What is your GDP per capita? Please use the number only, in your own currency."))


gdp = pop * gdp_capita

print("This is your nation's GDP.", gdp, "If you get an error, please check that you entered everything in correctly.")

但你在第1行得到了正确的答案:)