Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a list into constructor

I'm coding a Hangman game and to this I created the class bellow.

One of the methods is to hide de word with *symbols and show the letters that were guessed.

For this, I created a list in method def hide_word(self) and call it in constructor to show the hided word on the board in the method print_game_status(self).

I'm wondering if the line marked above is valid or is a bad practice.

PS: Some lines are unfinished.

# Class
class Hangman:

    # Constructor
    def __init__(self, word):
        self.word = word
        self.hidedWord = self.hide_word() # <-- This is valid or is a bad practice?

    # Method to guess the letter
    def guess(self, letter):
        if letter in self.word:
            correctLetters.append(letter)
            self.hidedWord[1] = 'Y' # <-- This only a test, at first it works
        else:
            wrongLetters.append(letter)

    # Method to check if game is over
    # def hangman_over(self):

    # Method to check if the player won
    # def hangman_won(self):

    # Method to hide the letter on the board
    def hide_word(self):
        return ['*' for x in self.word]

    # Method to check the status game and print the board on the screen
    def print_game_status(self):
     print(board[0])
     print(*self.hidedWord)
     print('Wrong letters: ' + ', '.join(wrongLetters))
     print('Correct letters: ' + ', '.join(correctLetters))

# Function to read a word randomly from the word bank
def rand_word():
    with open('palavras.txt', 'rt') as f:
        bank = f.readlines()
    return bank[random.randint(0, bank.index(max(bank)))].strip()

# Main function
def main():
    # Object
    game = Hangman(rand_word())

    # While the game is not over, print the status, request a letter and read caracter
    while(exit != True):
        # Check the status game
        game.print_game_status()

        # Input from user
        inputUser = input('Type a letter: ')
        game.guess(inputUser)

    # According to the game status, print the message on screen to user
    # if game.hangman_won():
    #  print('\nCongratulations! You won!!!')
    # else:
    # print('\nGame over! You lost.')
    # print('The word was {}'.format(game.word))

    # print('\nIt was good to play with you. But now go study!\n')


# Run program
if __name__ == '__main__':
    main()

like image 543
Rafael Lima Avatar asked Dec 05 '25 19:12

Rafael Lima


1 Answers

since you are only dealing with words, meaning the input is usually short, you don't need an additional attribute for hidedWord in the constructor, whenever you need this value you could just call self.hide_word() .

However, if you are dealing with longer input, like long sentences or even texts, its better if you just called self.hide_word() once and had an attribute to hold its value, because calling the function everytime would slow down your game.

like image 185
Mulham Jarjanazi Avatar answered Dec 08 '25 09:12

Mulham Jarjanazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!