Finding matches in two lists

Question:

I’am creating my first python project called hangman. And i’am stuck in place, where i need to compare letters from given word and if all letters is correct (not depending from order), program shows, that i won.

import random

class HangMan():

    def __init__(self):
        self.words_list = 'sunny', 'number', 'darius', 'calendar', 'table'
        self.words_randomizer = random.choice(self.words_list)




    def guess_letter(self):
        word_splitter = list(self.words_randomizer)
        empty_list = []
        while True:
            guess_letter = str(input('Please enter letter'))
            empty_list.append(guess_letter)
            if guess_letter in word_splitter:
                print(f'You are correct, there is letter - {guess_letter}, please guess another !')
                continue
            elif sorted(empty_list) == sorted(word_splitter):
                print('Congrats ! You won')
                break



8hm = HangMan()
print(hm.words_randomizer)
hm.guess_letter()

I thought, if i’ll create empty list and append guessing letters it will work. But it dont.

Asked By: DaBa

||

Answers:

You should only append to the empty list if the word is correct, like this:

import random

class HangMan():

    def __init__(self):
        self.words_list = 'sunny', 'number', 'darius', 'calendar', 'table'
        self.words_randomizer = random.choice(self.words_list)




    def guess_letter(self):
        word_splitter = list(self.words_randomizer)
        empty_list = []
        while True:
            guess_letter = str(input('Please enter letter'))
            if guess_letter in word_splitter:
                print(f'You are correct, there is letter - {guess_letter}, please guess another !')
                empty_list.append(guess_letter)
            if sorted(empty_list) == sorted(word_splitter):
                print('Congrats ! You won')
                break



hm = HangMan()
print(hm.words_randomizer)
hm.guess_letter()

Although, this algorithm has a flaw, that suppose the word is sunny, and I input "s" twice, it will add it twice to the empty_list, which will then give a logical error in the game.

So, you can try popping element from the list instead:

    def guess_letter(self):
        word_splitter = list(self.words_randomizer)
        while True:
            guess_letter = str(input('Please enter letter'))
            if guess_letter in word_splitter:
                print(
                    f'You are correct, there is letter - {guess_letter}, please guess another !')
                word_splitter.remove(guess_letter)
            if len(word_splitter) == 0:
                print('Congrats ! You won')
                break
Answered By: Divyessh

You need to only add the letter to the list when it’s correct, also don’t use an elif, finding a letter isn’t an excluding condition for solving the exercice, that’s the opposite, you need to find letter to solve it so put the winning condition into the valid-guessing block

def guess_letter(self):
    word_splitter = sorted(self.words_randomizer)
    guesses = []
    while True:
        guess_letter = input('Please enter letter: ')

        if guess_letter in guesses:
            print("You already test that")
            
        elif guess_letter in word_splitter:
            print(f'You are correct, there is letter - {guess_letter}, please guess another !')
            guesses.append(guess_letter)
            if sorted(guesses) == word_splitter:
                print('Congrats ! You won')
                break

Also

  • check for already tested letter
  • input already returns a string
Answered By: azro
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.