Help with Python error messages

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hey @nickthefisherman06 - welcome to the forum!

I can see you’re having some trouble with errors in Python from the topic, but you’ve not posted any of the errors you’re stuck with? :slight_smile: Are you still having trouble?

1 Like

Yeah, I have a few errors I need help with. I can’t figure out how to paste my project, even though I converted the .py format into a .txt document.

Here it is @thepitycoder. I couldn’t attach the document so I just pasted the words. The error example is below the code.

Code:

import random


lives = 9

words = ['pizza', 'fairy', 'teeth', 'shirt', 'otter', 'plane', 'quick', 'happy', 'whizz', 'hippo']
secret_word = random.choice(words)
clue = []
index = 0
while index < len(secret_word):
    clue.append('?')
    index = index + 1
heart_symbol = u'\u2764'
guessed_word_correctly = False
unknown_letters = len(secret_word)
def update_clue(guessed_letter, secret_word, clue, unknown_letters):
    index = 0
    while index < len(secret_word):
        if guessed_letter == secret_word[index]:
            clue[index] = guessed_letter
            unknown_letters = unknown_letters - 1
        index = index + 1



while lives > 0:
    print(clue)
    print('Lives left: ' + heart_symbol * lives)
    guess = input('Guess a letter or the whole word: ')


    if guess == secret_word:
        guessed_word_correctly = True
        break


    if guess in secret_word:
        unknown_letters = update_clue(guess, secret_word, clue, unknown_letters)
    else:
        print('Incorrect. You lose a life.')
        lives = lives - 1


    if unknown_letters == 0:
        guessed_word_correctly = True
        break


if guessed_word_correctly:
    print('You won! The secret word was ' + secret_word)
else:
    print('You lost! The secret word was ' + secret_word)


**The error says:**

Traceback (most recent call last):
  File "/Users/Family/Documents/9 Lives.txt", line 38, in <module>
    unknown_letters = update_clue(guess, secret_word, clue, unknown_letters)
  File "/Users/Family/Documents/9 Lives.txt", line 21, in update_clue
    unknown_letters = unknown_letters - 1
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

I would appreciate any help.

Thanks for that. When posting code, it’s helpful - especially with Python, where the indentation is important - to format it on here using the code blocks like this:

```
my code goes here
```

which would render as:

my code goes here

That way, we can see the code as is - otherwise the forum removes any whitespace which makes it harder to follow.

Can you edit your post, and re-paste the code as a codeblock please? :slight_smile:

The error that’s stopping your code is this one:

File “/Users/Family/Documents/9 Lives.txt”, line 21, in update_clue
unknown_letters = unknown_letters - 1
TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘int’

but when I run your code, having re-indented it myself, I don’t get that error.

Your code is somehow attempting to subtract 1, which is an int type, from the value of unknown_letters but at the point it’s making the attempt unknown_letters = None. Subtraction isn’t a valid operation for a NoneType variable, so Python throws the error you’re seeing.

Can you re-paste the code, formatted as I’ve shown you with the ``` before and after, so we can see how you’ve got it written? I must’ve either fixed something or changed something because it doesn’t run the same way for me…

1 Like

In your if code block you are assigning the value of calling your update_clue() function to your unknown_letters variable. The return value is None, so the second time subtracting 1 from unknown_letters is attempted, the error is thrown. All you need to do here is call the function. Hope this helps!

2 Likes

I got it, thank you @midlindner!

1 Like