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

この種の問題を最小化/回避する 1 つの方法は、マッチングを行うエディターを使用することです。つまり、括弧と場合によっては引用符をマッチングします。また、構文の強調表示/色付けは、引用符が閉じられていない場合 (および一部の言語では複数行のコメントが終了していない場合) に表示されるため、非常に便利なツールであることは明らかです。これらの領域のコードも調べる価値があります。

余談ですが、次から次へとたくさんありif sell_var == 1:ます..それは意図的なものですか?その場合、そのうちの1つで十分なようです。

最後に、PEP8 - The Style Guide for Pythonを参照すると、Python コードを記述する際の書式設定、命名規則などに関する提案が得られます。たとえば、ループの本体がインデントされすぎています (ただし、これはコードをここに貼り付けた結果にすぎない可能性があります)。たとえば、PEP8 では indentation に 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?)

'あなたはそこにaとaを忘れました)

于 2012-09-03T16:39:57.923 に答える