pythonindentation

Understanding indentation in Python


I'm starting out on Python at school, and as homework, we have to make a hangman game. My only problem is it always seems to try and indent everything, and even when I do, it still wants an indentation.

Heres my code:

import random
global guess

def checkGuess():
    turns = 5
    n = 0
    guess = input('Please guess a letter:')
    guesslen = len(guess)
    while len(guess) > n:
        if guesslen >1:
            print ('Please only guess a single letter.')
            checkGuess()
        elif guess in secretWord:
            print (guess)
            n = n+1
            checkGuess()
        elif guess not in secret:
            turns -= 1
            print ('Nope.')
            print (turns), 'more turns'
            if turns < 5: print ('        OMMMMMD')
            if turns < 5: print ('       MMMMMMMMM')
            if turns < 5: print ('      =MMMMMMMMM?')
            if turns < 5: print ('      ,MMMMMMMMM:')
            if turns < 5: print ('       MMMMMMMMM')
            if turns < 5: print ('        .MMMMM,')
            if turns < 5: print ('                       ')
            if turns < 3: print ('  ZMMMMMMMMMMMMMMMMM7L')
            if turns < 3: print (' MMMMMMMMMMMMMMMMMMMMM')
            if turns < 3: print ('MMMMMMMMMMMMMMMMMMMMMMM')
            if turns < 3: print ('MMMMMMMMMMMMMMMMMMMMMMM')
            if turns < 3: print ('MMMMIMMMMMMMMMMMMMIMMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print ('MMMM DMMMMMMMMMMMM MMMM')
            if turns < 3: print (' ZD  DMMMMM?MMMMMM  DO')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 2: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('     DMMMMM MMMMMM')
            if turns < 1: print ('      MMMM+ ?MMMM')
            if turns == 0:
            print ('The answer is'), (secretWord)

print('H A N G M A N')
secretWord = random.choice (['crocodile','elephant','penguin','pelican', 'leopard', 'hamster', 'lion',])
i = ('')
g = len(secretWord) <--- THIS IS THE LINE WHERE THE PROBLEM IS
length = 0
print (secretWord)
while g > length:
    i = i + (' _')
    length = length + 1
print(i)
guess = ('')
checkGuess()

I've tried every method already suggested on this website and others. Can anyone help?


Solution

  • Actually the problem is on line 57. You have an if statement, but you haven't indented its contents.

            if turns == 0:
            print ('The answer is'), (secretWord)
    

    Should be

            if turns == 0:
                print ('The answer is'), (secretWord)