David Dixon
David Dixon

Reputation: 13

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?

Upvotes: 1

Views: 414

Answers (2)

luke14free
luke14free

Reputation: 2539

Issues:

  1. You don't keep track of the guesses

  2. Use globals for the turn and the guesses, the turn and guesses vars shall be outside your function

  3. while(guess)> n should be if(guess) > n

  4. use raw_input instead of input

  5. Add a guesses.append(guess) to the winning cycle

  6. Check if the guesses generate the word:

    if sorted(list(secretWord))==sorted(list(guesses)):

  7. You messed up all your variable names, secret - secretWord..

  8. Add indentation after

    if turns == 0:

  9. if turns == 0: ... add an else block like this "else: checkGuess()"

Upvotes: 2

kojiro
kojiro

Reputation: 77187

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)

Upvotes: 7

Related Questions