Python 3.4.2中无法解释的无效语法

时间:2014-11-19 00:09:37

标签: python syntax

每当我按F5运行我的代码时,会弹出一条错误消息,其中显示“无效语法”,以及以下行中“True”之后的空格:

while not chosen4 == True

我不知道导致此错误的原因。 其余的源代码如下:

import time
playAgain = False
while not playAgain == True:
    gameOver = False
    print ("""Press ENTER to begin!
""")
    input()
    print("""text""")
    time.sleep (2)
    print ("""text""")
    time.sleep (7)
    print ("""text""")
    print("""(Press a number and then ENTER!)""")
    while not gameOver == True:
        direction = input ("Your chosen action: ")
        print("You chose {0}!".format(direction))
        time.sleep (2)
        if direction == "1":
            print ("""text""")
            time.sleep (6)
            print ("""text""")
            chosen1 = False
            while not chosen1 == True:
                direction1 = input ("Your chosen action: ")
                print ("You chose {0}!".format(direction1))
                time.sleep (2)
                if direction1 == "3":
                    print ("""text""")
                    time.sleep (2)
                    print("""text""")
                    time.sleep (8)
                    print("""text""")
                    gameOver = True
                    break
                elif direction1 == "4":
                print("""text""")
                    time.sleep (3)
                    chosen4 = False
                    while not chosen4 == True
                    #The above line is where the 'invalid syntax' is.
                    direction4 = input ("Your chosen action: ")
                    print "You chose {0}!".format(chosen4)
                    time.sleep (2)
                    if chosen4 chosen4 == "7":
                        print ("text")
                        gameOver = True
                        break
                    elif chosen4 == "8":
                        print ("text")
                        gameOver = True
                        break
                    else:
                        tryagain
                else:
                    print ("""text""")
                    time.sleep (3)
        elif direction == "2":
            print ("""text""")
            time.sleep (2)
            print("""text""")
            time.sleep (3)
            print("""text""")
            chosen2 = False
            while not chosen2 == True:
                direction2 = input ("Your chosen action: ")
                print ("You chose {0}!".format(direction2))
                time.sleep (2)
                if direction2 == "5":
                    print ("""text""")
                    time.sleep (8)
                    gameOver = True
                    break
                elif direction2 == "6":
                    print ("""text""")
                    break
                else:
                    print ("""text""")
        elif direction == "0":
            print ("""text""")
        else:
            print ("""text""")
if gameOver == True:
    input()

2 个答案:

答案 0 :(得分:0)

你错过了:
循环体也需要缩进

这个缩进问题也突然出现在我身上

                elif direction1 == "4":
                print("""text""")

答案 1 :(得分:0)

bit_cast上缺少冒号,应该是while not chosen4 == True

您在下面的打印声明中的while not chosen4 == True:处缺少缩进。

这也不是一个错误,而是代替elif direction1 == "4":将其更改为while not chosen2 == True:while chosen == False:,因为它有助于清除错误。