我是一名初學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
不匹配的語法突出顯示應該給你一些其他方面的調整。對於大多數輸入提示,「'''不匹配。 –
使用4個空格縮進代碼。 – 2012-09-03 17:02:40
一些無需求教的建議:有一種稱爲「不要重複自己」的編程原則DRY,其基本思想是,如果您要編寫很多代碼,除了一些小的更改外,其他代碼看起來都很像,您需要了解該模式的共同之處並將其分開。在這種情況下,我會使用字典來存儲不同股票的成本和金額。 – DSM