If 语句的缩进错误
indentation error on If Statement
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 语句的最后一个 else 上收到缩进错误。
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
else
子句不带条件。您可以将其写成 elif
子句,也可以直接写成 else.
注意,顺便说一句,您应该从前两个条件中删除 =
以使代码正确:
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")
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 语句的最后一个 else 上收到缩进错误。
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
else
子句不带条件。您可以将其写成 elif
子句,也可以直接写成 else.
注意,顺便说一句,您应该从前两个条件中删除 =
以使代码正确:
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")