Home > database >  Variable initialization and loops
Variable initialization and loops

Time:07-31

I'm new to coding and was trying to make a hangman game. My code works but it does not add and remove letters from the sets above. Could somebody please explain what I'm doing wrong. Thank you in advance. The code is written below;

from random_word import RandomWords
import string
lives = 160
number_of_plays = 0
all_letters = set(string.ascii_lowercase)
print("Please type a letter to play or 1 to quit")
print('You only have 6 chances to guess the correct word')
correct_word = RandomWords()
generated_word = correct_word.get_random_word()  # a random word

while number_of_plays < lives:
    player_command = input('> ').lower()  # player letter input
    generated_word_letters = set(generated_word)  # tabulates letters in the aforementioned word
    guessed_letters = set()  # input from the player
    correct_guessed_word = (letter if letter in guessed_letters else '_' for letter in generated_word)
    if player_command == '1':
        print('Game over')
        break
    elif player_command in all_letters:
        guessed_letters.add(player_command)
        if player_command in generated_word_letters:
            generated_word_letters.remove(player_command)
            print('Current correct guessed word: ', ''.join(correct_guessed_word))
            continue
        elif player_command not in generated_word_letters:
            number_of_plays  = 1
            print('You have lost one life')
            print('You have used these letters: ', ''.join(guessed_letters))
            print('Current correct guessed word: ', ''.join(correct_guessed_word))
            continue
    elif player_command not in all_letters:
        number_of_plays  = 1
        print('You have lost one life')
        print('Please type in a letter!')

CodePudding user response:

You are on the right track. You just made the mistake of initializing your sets inside the loop. So, what is happening is that every time your loop starts again, you are wiping out what was in them before. Simply initialize your sets before jumping into the loop as shown below. Note: I just overrode your RandomWords with a fixed word for troubleshooting. Nice game!

#from random_word import RandomWords
import string
lives = 160
number_of_plays = 0
all_letters = set(string.ascii_lowercase)
print("Please type a letter to play or 1 to quit")
print('You only have 6 chances to guess the correct word')
#correct_word = RandomWords()
generated_word = "lunch" #correct_word.get_random_word()  # a random word

generated_word_letters = set(generated_word)  # tabulates letters in the aforementioned word
guessed_letters = set()  # input from the player


while number_of_plays < lives:
    player_command = input('> ').lower()  # player letter input
    correct_guessed_word = (letter if letter in guessed_letters else '_' for letter in generated_word)
    if player_command == '1':
        print('Game over')
        break
    elif player_command in all_letters:
        guessed_letters.add(player_command)
        if player_command in generated_word_letters:
            generated_word_letters.remove(player_command)
            print('Current correct guessed word: ', ''.join(correct_guessed_word))
            continue
        elif player_command not in generated_word_letters:
            number_of_plays  = 1
            print('You have lost one life')
            print('You have used these letters: ', ''.join(guessed_letters))
            print('Current correct guessed word: ', ''.join(correct_guessed_word))
            continue
    elif player_command not in all_letters:
        number_of_plays  = 1
        print('You have lost one life')
        print('Please type in a letter!')
  • Related