2014-04-14 29 views
0

我正在Python中編寫一個簡單的腳本來返回GDP值。從第6行開始,它給出了一個「語法錯誤」,沒有進一步的闡述。我評論了這個問題(第6行)和第16行的所有行,我得到了「解析EOF」錯誤。我真的不確定要做什麼,因爲我檢查了不匹配的分隔符,不正確的語法等,但我能找到的唯一方法就是執行我的打印語句的方式,但由於它們都是相同的,只有一個解析錯誤不太可能是這種情況。下面是代碼:爲什麼我一直在Python中獲取任意語法錯誤?

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

「因爲我檢查不匹配的分隔符」:不是很成功,似乎.. – DSM

+0

在您第三行和第四行,則缺少右括號。另外,在'gdp_capita = ...' – BrenBarn

+0

的命令行中,'p_i(p_g - y_f)'也不行,它試圖調用一個整數。你可能會想'p_i *(p_g - y_f)'。 – DSM

回答

0

你的錯誤在你syntaxing。你錯過了小括號。在第3,4和9行,你有這個:int(input("...")沒有最後一個括號!你也試圖在第5行調用一個整數,p_f = p_i(p_g...)。我以爲你試圖繁殖,所以我在那裏放了一個乘號(星號)。此外,請確保您現在雙星號意味着'對'的力量。 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 :)

相關問題