import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(chosen_word)
placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
placeholder += "_"
print(placeholder)
game_over = False
correct_letters = []
lives = 6
while not game_over:
guess = input("Guess a letter: ").lower()
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)
print(lives)
if "_" not in display:
game_over = True
print("You win.")
elif lives == 0:
game_over = True
print("You lose.")
else:
for i in range(len(chosen_word)):
if guess not in chosen_word[i]:
lives -= 1
Long story short this /\ /\ takes a life for EACH wrong letter in the word. I know why it does that but not necessarily how to change it so it only takes one instead for a wrong guess.
Its asking me to do more details to post so I will say this is for the 100 days of coding class. I'm just getting back to it still day seven just part 4 now.
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(chosen_word)
placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
placeholder += "_"
print(placeholder)
game_over = False
correct_letters = []
lives = 6
while not game_over:
guess = input("Guess a letter: ").lower()
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)
print(lives)
if "_" not in display:
game_over = True
print("You win.")
elif lives == 0:
game_over = True
print("You lose.")
else:
for i in range(len(chosen_word)):
if guess not in chosen_word[i]:
lives -= 1
Long story short this /\ /\ takes a life for EACH wrong letter in the word. I know why it does that but not necessarily how to change it so it only takes one instead for a wrong guess.
Its asking me to do more details to post so I will say this is for the 100 days of coding class. I'm just getting back to it still day seven just part 4 now.
Share Improve this question edited 2 hours ago Barmar 782k56 gold badges546 silver badges660 bronze badges asked 3 hours ago Logan SmithLogan Smith 92 bronze badges 3 |1 Answer
Reset to default 0In your else
block, you are looping through every letter in chosen_word
and reducing lives each time guess is not found in a letter. This results in multiple life deductions per wrong guess.
# remove the print(lives) and use when there is a wrong guess.
if guess in chosen_word:
for position in range(len(chosen_word)):
if chosen_word[position] == guess:
display = display[:position] + guess + display[position + 1:]
else:
lives -= 1 # if letter not found, only 1 life deduction will happen
print("Remaining lives",lives)
break
after subtracting from lives once to leave that loop. It looks like that will work fine. – Carcigenicate Commented 3 hours agofor
loop could be replaced entirely withif guess not in chosen_word: lives -= 1
– JonSG Commented 3 hours ago