1

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
4
  • You should try the elif statement. Also, instead of seeing if its >=, check ==. Commented May 4, 2017 at 1:14
  • Any time you find yourself creating variables with names like letter1, letter2, etc. you probably should be using a list (or array in other languages), not separate variables. Commented May 4, 2017 at 1:17
  • 1
    There's no need for else: pass. You can just leave out the else: statement if there's nothing to do. Commented May 4, 2017 at 1:17
  • The letters function never returns anything, what are you expecting print(letters(word)) to print? Commented May 4, 2017 at 1:21

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

5 Comments

How do you know that letters is supposed to return a list?
@Barmar, the point is not to do the letters() function for OP, but rather show the fact that the entire if/else can be replaced with built-in python feature.
But the if/else is just setting a bunch of variables, there's no mention of creating a list from them anywhere.
Thank you so much for the help. I did'nt mention a list, primarily because I'm very new to programming and don't really know how to ask a good question with the right terminology. I do believe this may be a great solution though. I think a list will work. I'm planning to have the user guess a letter and if it is correct, it will show the letter, I'm sure there is a simple way to match the user input with the potential letter in a random word.
You are most welcome! As you learn more about python, you will start to pick up the right features for different situations. Have fun learning python and have a great day! It is certainly one of the best programming languages.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.