我是一个在mac上使用python 2.5.4的初学者python用户。 过去几天我一直在尝试在python中创建游戏股票代码(仅文本),我差不多完成了,但是我在while循环中收到“语法错误:无效语法”。这是代码的一部分,它给我带来了问题,错误发生在第5行,我得到了一个^指向的时间。 (我会发布整件事,但超过300行)
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 :(得分: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 :(得分:0)
if sell_var == 1:
temp_sell = int(raw_input('How many ore stock would you like to sell?)
您忘记了'
和)
。