Michael Phillips
Michael Phillips

Reputation: 33

What is wrong with my if statement? Python

I cannot seem to figure out why my if statement is not working. I'm new to programming and I am trying to create a hangman game to test how I'm doing. I have it set up so that the code will select a random word, count the letters of that word, place dashes to show where each letter will be. At this point I'm trying to use an if statement that is going to index each letter, until it is finished with all of the letters. Here is the code.

"""
python simple.py
"""

from random_words import RandomWords
rw = RandomWords()
word = rw.random_word()
print(word)

word_len = len(word)
print(("___" + "   ") * word_len)
def letters(word):
    if word_len >= 0:
        letter1 = word[0]
    else:
        pass
    if word_len >= 1:
        letter2 = word[1]
    else:
        pass
    if word_len >= 2:
        letter3 = word[2]
    else:
        pass
    if word_len >= 3:
        letter4 = word[3]
    else:
        pass
    if word_len >= 4:
        letter5 = word[4]
    else:
        pass
    if word_len >= 5:
        letter6 = word[5]
    else:
        pass
    if word_len >= 6:
        letter7 = word[6]
    else:
        pass
    if word_len >= 7:
        letter8 = word[7]
    else:

print(letters(word))

The error I get is as follows:

Traceback (most recent call last):
  File "simple.py", line 46, in <module>
    print(letters(word))
  File "simple.py", line 30, in letters
    letter5 = word[4]
IndexError: string index out of range

Upvotes: 1

Views: 1690

Answers (4)

Batman
Batman

Reputation: 8927

A great many things.

What you have isn't an if statement. It's eight separate if statements. When your code runs it evaluates the first if statement. Then, regardless of what it did in the first statement, it then evaluates the second one, then the third one, etc.

What you really want to use is the elif statement. That turns your code into one statement, and the subsequent elif statements are only evaluated if one of the the preceding if/elif statements hasn't evaluated to True.

On top of that, Python uses zero based indexing. So if your word is "foo", you will have len(word) == 3. But the indexing means that word[0] == 'f', word[1] == 'o', and word[2] == 'o'. There's no word[3].

So, when the code runs

if word_len >= 4:
    letter5 = word[4]

your word has length 4, so there's not word[4], but you're still trying to reference it.

Lastly, your function doesn't actually return anything. What do you want to get back from the function?

Upvotes: 0

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Put aside the issues with how if/else was used, you can easily replace the whole chain of if/else with a simple list(). Try below code, it demos this:

a = "abcd"
print(list(a))

A coding style thing, try not to use variables like letter1, letter2, any time you come down to this, try to think whether they can be replaced by collections like list etc.

Upvotes: 0

Joe D
Joe D

Reputation: 388

Say the word is "Cats". len(word) will return 4, but you can only index the string from 0 to 3. Change your >= signs to just >.

Aside from that, you can eliminate the else - pass statements everywhere. If the if statement isn't true, it automatically passes to the next line.

Upvotes: 1

c..
c..

Reputation: 239

Python is 0-indexed, so a word of length 4 will only have word[0] to word[3].

Trying to access word[4] gives you the index out of range error.

Note your first condition

if word_len >= 0:
        letter1 = word[0]

This will try to access word[0] even if the length of word is zero.

Upvotes: 1

Related Questions