Any one good with a little python problem?

Soldato
Joined
18 May 2010
Posts
22,831
Location
London
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.

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:
I think you should add a keyword continue on line right after print(f"You have already guessed this letter: {guess<span>}")
so that it skips immediately to next iteration of while loop and doesn't go through all the code below (including lives decrement)
 
I think you should add a keyword continue on line right after print(f"You have already guessed this letter: {guess<span>}")
so that it skips immediately to next iteration of while loop and doesn't go through all the code below (including lives decrement)

We haven't covered continue yet.

My point is the teachers code is operating differently to mine and I can understand why.
 
We haven't covered continue yet.

My point is the teachers code is operating differently to mine and I can understand why.
ok, read the problem again

if the word is baboon and I keep pressing v, v ,v I will lose lives whilst the teachers code does not.
pretty sure the other code should also lose lives in this case

for your purpose then, you are missing:
- before lives decrement, check that guess was present in correct_letters (and don't take lives then). And if not, then check if guess was in chosen_word
- add wrong guess to correct_letters, so that on next iteration the user warning could happen
 
ok, read the problem again


pretty sure the other code should also lose lives in this case

for your purpose then, you are missing:
- before lives decrement, check that guess was present in correct_letters (and don't take lives then). And if not, then check if guess was in chosen_word
- add wrong guess to correct_letters, so that on next iteration the user warning could happen

This is exactly my point. The teachers code which works doesn't do these things.

Anyway it's not the end of the world. I understand the concepts here and my code was actually better than the code the teacher has given us to work with here because my list was not a list of correct guess but a list of previous guesses so I could iterate over that and check if the current guess (right or wrong) was in the list.

The way the teacher has done it they are only appending correct letters to the list so I don't see how the check they are doing checks for previous incorrect guess and NOT deduct a life...

But somehow it works like that. Hence why I posted it here.
 
Last edited:
Back
Top Bottom