-5

我是在 mac 上使用 python 2.5.4 的初学者 python 用户。在过去的几天里,我一直在尝试在 python 中创建游戏股票代码(仅限文本),我几乎完成了,但我在 while 循环中收到“语法错误:无效语法”。这是给我带来问题的代码部分,错误发生在第 5 行,我得到一个 ^ 指向 while 的 e。(我会发布整个内容,但超过 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
4

2 回答 2

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?'))
                                                                     ^^

最小化/避免此类问题的一种方法是使用为您匹配的编辑器,即它会匹配括号,有时还会匹配引号。显然,语法高亮/着色是一个非常有用的工具,因为它显示了引号未关闭时(并且在某些语言中,多行注释未终止)。也值得检查这些领域的代码。

顺便说一句,有很多if sell_var == 1:一个接一个..这是故意的吗?在这种情况下,其中一个似乎就足够了。

最后,您可能想看看PEP8 - The Style Guide for Python,它会在编写 Python 代码时提供有关格式、命名约定等方面的建议。例如,循环的主体缩进太多(尽管这可能只是在此处粘贴代码的产物)。例如,PEP8 建议缩进 4 个空格

于 2012-09-03T16:39:45.783 回答
0
        if sell_var == 1:
            temp_sell = int(raw_input('How many ore stock would you like to sell?)

你忘了一个'和一个)

于 2012-09-03T16:39:57.923 回答