2016-08-06 3 views
-1
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) 

나는 잠시 동안 게임을 만들었지 만 방금 파이썬을 시작했습니다. 번호 추측 게임을 지루함에서 벗어나지 만 17 행에 들여 쓰기 오류가 계속 발생하거나 if 문 마지막에 다른 오류가 계속 발생합니다.에 들여 쓰기 오류 If 문에 If 문에

+1

BTW, 코드 블록의 시작을 'if event.type'가 들여 쓰기가 너무 많은 경우 :'print' 호출과 같은 들여 쓰기가되어야합니다. 그리고 물론'else event.type == pygame.R'은'elif event.type == pygame.R'이어야합니다. –

답변

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") 
+0

들여 쓰기 오류가 계속 발생합니다. – CabbageOverlord

+0

IndentationError 행을 확인하십시오. 그것은 다른 문제이기 때문에 다른 사람 아래에 있습니다. –

+0

파일 "number_game.py", 줄 17 else ^ 들여 쓰기 오류 : 들여 쓰기가 바깥 쪽 들여 쓰기 레벨과 일치하지 않습니다. – CabbageOverlord

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 
+0

여전히 "else :"에 들여 쓰기 오류가 발생했습니다. – CabbageOverlord

+0

업데이트 된 답변이 잘못되었습니다. [내 의견] (http://stackoverflow.com/questions/38803093/indentation-error-on-if-statement#comment64975319_38803093)을 참조하십시오. 게시하기 전에 응답 코드를 테스트하는 것이 좋습니다 ... –