Matthew
Matthew

Reputation: 63

Hangman Game Error TypeError: 'NoneType' object is not iterable

I am a beginner coder for couple weeks and this is my first program after learning some basics. It is a hangman game but its a very crude version of it with no visuals (that will come later). When I run it, it allows me to pick one letter but then after that it gives me the error "noneType object is not iterable"

Can anyone explain why this is? Sorry for the ultra-noobness in the quality of code but when I was browsing through other questions of similar errors, the code was too complex for me to apply to my situation

import time
import random

"""This is a hang man game
"""

name = input("what is your name?")


print("Hello" + " " + name)
time.sleep(1)
print("Ok, lets paly hangman, let me choose a word......")
time.sleep(1)
print("Thinking, thinking, thinking....")
time.sleep(3)
print("AHA! got it, ok " + name + ", take your first guess")
time.sleep(1)
def hangman_game():

    word_list = ["herd", "heaven", "manly", "startle"]
    number = random.randint(0, len(word_list) - 1)
    secret_word = word_list[number] #chooses a word from the word list


    blank_char = "-"
    word_length = len(secret_word) * blank_char
    word_length_split = list(word_length)
    print("Try and guess the word: ", word_length) #displays the length of the word in '_'


    previous_guesses = []
    guesses_taken = 0
    turns = 10
    secret_word_list = list(secret_word)

    while turns > 0:
        letter_guessed = input("Pick a letter: ")
        if letter_guessed in previous_guesses:
                print("You have already guessed this letter")
        else:
            previous_guesses = previous_guesses.append(letter_guessed)
            if letter_guessed in secret_word_list:
                secret_word_list = secret_word_list.remove(letter_guessed)
                guesses_taken = guesses_taken + 1
                print("That is correct," + letter_guessed + " is in the word")
                time.sleep(1)
            else:
                turns = turns - 1
                guesses_taken = guesses_taken + 1
                print("Try again")

    print("Sorry mate, no turns left, play again?")

hangman_game()

Upvotes: 0

Views: 101

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180482

change previous_guesses = previous_guesses.append(letter_guessed)

to previous_guesses.append(letter_guessed)

Also secret_word_list = secret_word_list.remove(letter_guessed)

to secret_word_list.remove(letter_guessed)

If you try the following:

In [5]: l = [1,2,3,4]

In [6]: print (type(l.remove(1)))
<class 'NoneType'>

if I used l = l.remove(1), I am setting it to a method which is a Nonetype as it does not return a list it mutates the original and returns None, not an updated list.

Appending is also the same:

 `In [7]: print (type(l.append(5)))
  <class 'NoneType'>`

`

Upvotes: 4

Related Questions