If语句

时间:2016-08-06 10:10:50

标签: python if-statement indentation

if mssge <= answr:
    print("Too High, Try Again")
elif mssge >= answr:
    print("Too Low, Try Again")
else:
    print("Nice Job! R to play again; Q to quit")

    if event.type == pygame.Q:
            sys.exit()

    else event.type == pygame.R:
            break
            gotoline(7)

我一直在制作游戏但我刚开始使用python。我正在做一个无聊的数字猜谜游戏,但我一直在第17行得到一个缩进错误,或者if语句的最后一个。

2 个答案:

答案 0 :(得分:2)

else子句不具备条件。您可以将其写为elif子句,也可以将其作为直接else.注意事项(BTW),从前两个条件中删除=以使代码正确:

if message_1 < rand_numb:
    # <= replaced with < in this condition ^
    print("Too High, Try Again")
elif message_1 > rand_numb:
    print("Too Low, Try Again")
    # >= replaced with > in this condition ^
else:
    # No condition on else ^
    print("Nice Job! R to play again; Q to quit")

答案 1 :(得分:0)

else - 陈述中不能包含条件。 Else应该抓住前一个if/elifs 抓住的每个案例。

你的第一个代码块应该是

if message_1 <= rand_numb:
    print("Too High, Try Again")
elif message_1 >= rand_numb:
    print("Too Low, Try Again")
else:  # <------ no conditions here!
    print("Nice Job! R to play again; Q to quit")

if event.type == pygame.Q:
    sys.exit()

else event.type == pygame.R:
    gotoline(5)
    break