Soldato
I am just doing a course on python and the project was to build a hang man game.
The step before this part I had implemented differently and I think my solution was better than the teachers because my list contained all the previously guessed letters, rather than the teacher list only containing the correct previous letters.
The final step was to add the TODOS to the teachers code.
Below I will post both mine and the teachers.
The thing I am trying to work out is why when I run my code does it continuously subtract from the lives a life even if its a previously guessed letter.
The teachers doesn't do the same and I cant figure out why this block is not being executed in the teachers code yet it is in mine:
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}, that's not in the word. You lose a life.")
For example if the word is baboon and I keep pressing v, v ,v I will lose lives whilst the teachers code does not.
---
The way I had implemented this before was to keep in the list all guessed letters rather than just the correct guessed letters, that way I could test was the current guess in the list of previous guesses, but the teachers code only seems to add the guess to the list if it matches a letter in the word.
So yea I don't understand because the teacher is testing if the guess is in correct_letters list. But if its v I keep pressing and the word is baboon, v doesn't go in to the correct_letters list so the block that subtracts the life should still execute. It does in mine but not in the teachers code...
The step before this part I had implemented differently and I think my solution was better than the teachers because my list contained all the previously guessed letters, rather than the teacher list only containing the correct previous letters.
The final step was to add the TODOS to the teachers code.
Below I will post both mine and the teachers.
Python:
import random
# TODO-1: - Update the word list to use the 'word_list' from hangman_words.py
from hangman_words import word_list
from hangman_art import stages, logo
lives = 6
# TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.
chosen_word = random.choice(word_list)
print(chosen_word)
print(logo)
placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
placeholder += "_"
print("Word to guess: " + placeholder)
game_over = False
correct_letters = []
while not game_over:
# TODO-6: - Update the code below to tell the user how many lives they have left.
print(f"****************************{lives}/6 LIVES LEFT****************************")
guess = input("Guess a letter: ").lower()
# TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
if guess in correct_letters:
print(f"You have already guessed this letter: {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("Word to guess: " + display)
# TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
# e.g. You guessed d, that's not in the word. You lose a life.
if guess not in chosen_word:
lives -= 1
print(f"You have guessed: {guess} this is not in the word. Lose a life")
if lives == 0:
game_over = True
# TODO 7: - Update the print statement below to give the user the correct word they were trying to guess.
print(f"***********************YOU LOSE**********************")
print(f"The correct word was: {chosen_word}")
if "_" not in display:
game_over = True
print("****************************YOU WIN****************************")
# TODO-2: - Update the code below to use the stages List from the file hangman_art.py
print(stages[lives])
Python:
import random
from hangman_words import word_list
from hangman_art import stages, logo
lives = 6
print(logo)
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)
game_over = False
correct_letters = []
while not game_over:
print(f"****************************{lives}/6 LIVES LEFT****************************")
guess = input("Guess a letter: ").lower()
if guess in correct_letters:
print(f"You've already guessed {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("Word to guess: " + display)
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}, that's not in the word. You lose a life.")
if lives == 0:
game_over = True
print(f"***********************IT WAS {chosen_word}! YOU LOSE**********************")
if "_" not in display:
game_over = True
print("****************************YOU WIN****************************")
print(stages[lives])
The thing I am trying to work out is why when I run my code does it continuously subtract from the lives a life even if its a previously guessed letter.
The teachers doesn't do the same and I cant figure out why this block is not being executed in the teachers code yet it is in mine:
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}, that's not in the word. You lose a life.")
For example if the word is baboon and I keep pressing v, v ,v I will lose lives whilst the teachers code does not.
---
The way I had implemented this before was to keep in the list all guessed letters rather than just the correct guessed letters, that way I could test was the current guess in the list of previous guesses, but the teachers code only seems to add the guess to the list if it matches a letter in the word.
So yea I don't understand because the teacher is testing if the guess is in correct_letters list. But if its v I keep pressing and the word is baboon, v doesn't go in to the correct_letters list so the block that subtracts the life should still execute. It does in mine but not in the teachers code...
Last edited: