2012-09-03 83 views
-5

我是一名初學python用戶,在mac上使用python 2.5.4。 我一直試圖在python中創建最近幾天的遊戲股票代碼(僅用於文本),並且我差不多完成了,但在while循環中出現「Syntax error:invalid syntax」。這裏是代碼的一部分,它給我錯誤的錯誤發生在第5行,我得到了一個^指向的時間。 (我會後整個事情,但其超過300線)Python:while循環的語法無效

while (keep_going ==0): 
      sell_var = int(raw_input('Please choose what you would like to sell, for grain enter 1, for technology enter 2, for ore enter 3, for construction enter 4, for bonds enter 5, and for trade enter 6, to skip enter any other key')) 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many grain stock would you like to sell?') 
       while (temp_sell > playgrain[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playgrain[x] = playgrain[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[1] * temp_sell 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many technology stock would you like to sell?') 
       while (temp_sell > playertech[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playtech[x] = playtech[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[2] * temp_sell 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many ore stock would you like to sell?) 
       while (temp_sell > playore[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playore[x] = playore[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[3] * temp_sell 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many construction would you like to sell?) 
       while (temp_sell > playconst[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playconst[x] = playconst[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[4] * temp_sell 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many bonds stocks would you like to sell?) 
       while (temp_sell > playbonds[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playbonds[x] = playbonds[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[5] * temp_sell 
      if sell_var == 1: 
       temp_sell = int(raw_input('How many trade stock would you like to sell?) 
       while (temp_sell > playtrade[x]): 
        temp_sell = int(raw_input('Please choose a different amount to sell, you do not have that many stock') 
       playtrade[x] = playtrade[x]-temp_sell 
       playmoney[x] = playmoney[x] + stock[6] * temp_sell 
+0

不匹配的語法突出顯示應該給你一些其他方面的調整。對於大多數輸入提示,「'''不匹配。 –

+0

使用4個空格縮進代碼。 – 2012-09-03 17:02:40

+2

一些無需求教的建議:有一種稱爲「不要重複自己」的編程原則DRY,其基本思想是,如果您要編寫很多代碼,除了一些小的更改外,其他代碼看起來都很像,您需要了解該模式的共同之處並將其分開。在這種情況下,我會使用字典來存儲不同股票的成本和金額。 – DSM

回答

3

你缺少收盤")"

int(raw_input('How many grain stock would you like to sell?') 
                  ^

在許多地方,你可能想回去看看在你的代碼。

這應該是:

int(raw_input('How many grain stock would you like to sell?')) 
                  ^

,正如你可以從代碼顏色看看,你的一些字符串不終止。例如,

temp_sell = int(raw_input('How many ore stock would you like to sell?) 
                    ^^ 

需要終止單引號,結束")"

temp_sell = int(raw_input('How many ore stock would you like to sell?')) 
                    ^^ 

的一種方式,以最小化/避免這些類型的問題是使用編輯器,它爲你匹配 ,即它會匹配parens和有時引號。顯然語法突出顯示/着色是一個非常有用的工具,因爲它顯示引號未關閉時(並且在某些語言中多行註釋未終止)。這也是值得在這些領域檢查代碼的。

作爲一邊,有很多if sell_var == 1:一個接一個..是故意的?在這種情況下,似乎其中一個就足夠了。

最後,你可能想看看PEP8 - The Style Guide for Python,它會在編寫Python代碼時給出格式化,命名約定等方面的建議。例如,循環的主體縮進太多(儘管這可能只是在此粘貼代碼的工件)。例如,PEP8 recommends 4 spaces for indentation

+1

非常感謝,我完全錯過了。 – Colin

+0

@ user1644240它發生..它值得尋找一個編輯器,將突出顯示匹配的parens和報價。 – Levon

+0

符合'('主題,'while(keep_going == 0):'可以重寫爲'while keep_going:' –

0
 if sell_var == 1: 
      temp_sell = int(raw_input('How many ore stock would you like to sell?) 

你忘了')那裏。

+0

也謝謝你,它現在可以完美工作 – Colin