Question: I have this code for a hangman game Basically what the code does is ask the user to guess a letter from the random word

I have this code for a hangman game
Basically what the code does is ask the user to guess a letter from the random word given and it adds it to a new list
Here it the python code and the error i am getting
#Hangman
import random
stages=['''
+---+
||
0|
/|\|
/\|
|
=========
''','''
+---+
||
0|
/|\|
/|
|
=========
''','''
+---+
||
0|
/|\|
|
|
=========
''','''
+---+
||
0|
/||
|
|
=========
''','''
+---+
||
0|
|
|
|
=========
''','''
+---+
||
|
|
|
|
=========
''']
word_list=["aardvark","baboon","camel"]
lives =6
chosen_word=random.choice(word_list)
print(chosen_word)
placeholder=""
word_length=len(chosen_word)
for position in range(word_length):
placeholder+="_"
print("Word to guess:" + placeholder)
#use a while loop to let the user guess again
game_over=False
correct_letters=[]
while not game_over:
guess=input("Guess a letter: ").lower()
print(guess)
display=""
for letter in chosen_word:
if letter == guess:
display+=letter
correct_letters.append(guess)
elif letter in correct_letters:
display+=letter
else:
display+="_"
print(display)
if guess not in chosen_word:
lives -=1
if lives==0:
game_over=True
print("You lose!")
if "_" not in display:
game_over=True
print("You Win")
print(stages[lives])
This is the error
```
un Task x
\
```
```
camel
Word to guess:
Guess a letter: a
a
_a_--
Guess a letter: m
m
_am__
Guess a letter: e
e
_ame_
Guess a letter: c
c
came_
Guess a letter: l
l
Traceback (most recent call last):
File "C:\Users\te\PycharmProjects\Python_Project_Final\.venv\Python_Scripts_Personal\100 Days of Code The Complete Python Pro Bootcamp\Day7\Task.py", line 87, in
print(stages[lives])
IndexError: list index out of range
camel
You Win
Process finished with exit code 1
```
I have this code for a hangman game Basically

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!