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
elifstatement. Also, instead of seeing if its>=, check==.letter1,letter2, etc. you probably should be using a list (or array in other languages), not separate variables.else: pass. You can just leave out theelse:statement if there's nothing to do.lettersfunction never returns anything, what are you expectingprint(letters(word))to print?