2

クラスの 1 つのプログラムを完成させようとしていますが、無効な構文が表示され続けます。Python は私にとって非常に新しいので、私の無知に耐えてください。

「ifguess==(num_x+num_y):」のコロンで無効な構文がポップアップしています。


update* かっこを修正しました abarnert に感謝しますが、今では次のようになりましたSyntaxError

Traceback (most recent call last):
  File "C:\Users\Franz\Desktop\randomstudy.py", line 48, in <module>
    main()
  File "C:\Users\Franz\Desktop\randomstudy.py", line 25, in main
    guess=int(input(num_x, "-", num_y, "=\n"))
TypeError: input expected at most 1 arguments, got 4

以下のコードを更新して、括弧を含めました。

def main():
    import random
    num_x=random.randint(-50,50)
    num_y=random.randint(-50,50)
    op=random.randint(0,3)
    control=True 
    print("Welcome! Here is your random problem:\n")   
    if op==0:
        while True:
            guess=int(input(num_x, "+", num_y, "=\n"))
            if guess==(num_x+num_y):
                print("Congratulations! You have answered the problem correctly!")
                break     
            else:
                print("I’m sorry, that is not correct.  Please try again.")
    elif op==1:
        while True:
            guess=int(input(num_x, "-", num_y, "=\n"))
            if guess==(num_x-num_y):
                print("Congratulations! You have answered the problem correctly!")
                break     
            else:
                print("I’m sorry, that is not correct.  Please try again.")
    elif op==2:
        while True:
            guess=int(input(num_x, "*", num_y, "=\n"))
            if guess==(num_x*num_y):
                print("Congratulations! You have answered the problem correctly!")
                break    
            else:
                print("I’m sorry, that is not correct.  Please try again.")
    elif op==3:
        while True:
            guess=int(input(num_x, "/", num_y, "=(Please round to two decimal places)\n"))
            if guess==(num_x/num_y):
                print("Congratulations! You have answered the problem correctly!")
                break    
            else:
                print("I’m sorry, that is not correct.  Please try again.")

main()
4

1 に答える 1

6

その直前の行に「)」がありません:

    guess=int(input(num_x, "+", num_y, "=\n")
    if guess==(num_x+num_y):

Igor がコメントで指摘しているように、閉じ括弧が欠落している他の行もあり、それらも修正する必要があります。そうしないと、別SyntaxErrorの数行を削除するだけです。括弧や同様の問題のバランスを取るのに役立つエディターの使用を検討することをお勧めします。これにより、私の作業が大幅に楽になります。

これは Python では他のほとんどの言語ほど一般的ではありませんが、説明できないSyntaxError.

(ほとんどの言語では、ほとんどすべての式またはステートメントを次の行に続けることができます。Python ではそうではありませんが、開き括弧がある場合は、括弧で囲まれた式を次の行に続けることができます。)

于 2013-02-08T11:02:34.563 に答える