6
\$\begingroup\$

This is a project that I remade to help me understand function definitions better. It may not be great but I am proud of it.

I would love to have you review this project and tell me what I could improve in this project, as well as in general.

from all_wrd import words
import random
import re

class Game:
    def __init__(self, user_guess, secret_word):

        # the user_guess is the the letter that the user/player inputs first
        # the secret_word is the random word that has been choicen out of the imported all_wrd file

        self.user_guess = user_guess
        self.secret_word = secret_word

    def checker(self):

        # creates a list of dots that are the same lenght as the secret_word
        right_guess = ["."]*len(self.secret_word)

        # amount of wrong user guesses 
        wrong_guess = 0

        while True :
            if self.user_guess not in self.secret_word:
                print("wrong guess")

                wrong_guess += 1

                print(right_guess)
                user = input("guess a letter : ")

                if user in self.secret_word:
                    print('you got it right 1 ')
                    
                     # replaces 1 or more of the dots with the correct user_input and puts it in the correct position  
                    for m in re.finditer(user , self.secret_word):
                        right_guess[m.end()-1] = user

                    print(right_guess)
                
                    # checks to see if all the user/player guessed all the letters of the secret_word
                    if '.' not in right_guess:
                        print("you got all the letters!! ")

                        finish_game = input("finish the game by typing the entire word please : ")

                        # checks to see if user spells the enitere word correctly 
                        # if so he or she wins the game end the game ends 
                        if finish_game == self.secret_word:
                            print("YOU GOT IT RIGHT , YOU WIN !!!")
                            break

                        # else the user loses and the game ends 
                        else:
                            print("you lose\nwell played ")
                            break

                    if user not in self.secret_word:
                        print("you got it wrong ")
                        wrong_guess += 1

                        user = input("guess another letter : ")

                        # if the user guesses the word wrong 3 times the game ends and user loses
                        if wrong_guess == 3:
                            print("you are out of guesses ")
                            print(f"\nthe word was {self.secret_word}")
                            break

            # checks to see if the first input was in the secret_word 
            if self.user_guess in self.secret_word:
                print("right guess ")
       
                # replaces 1 or more of the dots with the correct user_input and puts it in the correct position
                for m in re.finditer(self.user_guess, self.secret_word):
                    right_guess[m.end() - 1] = self.user_guess
                print(right_guess)


                new_guess = input("guess a letter please : ")

                if new_guess in self.secret_word :
                    print("got it right")

                     # replaces 1 or more of the dots with the correct user_input and puts it in the correct position
                    for m in re.finditer(new_guess, self.secret_word):
                        right_guess[m.end() - 1] = new_guess
                    print(right_guess)

                    # checks to see if the user/player guessed all the letters correctly
                    if '.' not in right_guess:

                        print("you got all the letters!! ")
                        last = input("finish the game by typing the entire word please : ")

                        if last == self.secret_word:
                            print("GOOD JOB YOU GUESSED THE WORD , CONGRATULATIONS!!!")
                            break

                        else:
                            print("you guess the wrong letter :(\nyou losed")
                            break

                if new_guess not in self.secret_word:

                    print("you got it wrong")

                    wrong_guess+=1

                    print(right_guess)
                    new_guess = input("try again : ")

                    if wrong_guess == 3:
                        print("you are out of guesses")
                        print(f"\nthe word was {self.secret_word}")
                        break


if __name__ == '__main__':
    the_word = random.choice(words)
    g = Game(input("guess a letter : "),the_word)
    g.checker()
\$\endgroup\$
4
\$\begingroup\$

It looks like you'd benefit from moving duplicated logic into functions. Additionally, you manage all of your state within the checker() method, when this state could be stored on the Game object. Passing a user guess into the constructor seems really strange. There's nothing special about the first guess, so don't treat it as a special case.

class Game:
    def __init__(self, secret_word):

        # the user_guess is the the letter that the user/player inputs first
        # the secret_word is the random word that has been choicen out of the imported all_wrd file

        self.secret_word = secret_word

        self.wrong_guesses = 0
        self.guessed = set()

    def guess(self,letter):
        if letter in self.secret_word:
            print("you got it right")
            self.guessed.add(letter)
        else:
            print("wrong guess")
            self.wrong_guesses+=1

    # answer with dots for letters not guessed
    def partial_answer(self):
        return ''.join([
            letter if letter in self.guessed else '.'
            for letter in self.secret_word
        ])
    
    def have_won(self):
        return self.partial_answer()==self.secret_word

    def play(self):
        while self.wrong_guesses<3 and not self.have_won():
            self.guess(input("guess a letter : "))
            print(self.partial_answer())

        if self.have_won():
            print("YOU GOT IT RIGHT , YOU WIN !!!")
        else:
            print("you are out of guesses ")
            print(f"\nthe word was {self.secret_word}")

if __name__ == '__main__':
    game = Game("dude")
    game.play()
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Verbs for methods

g.checker()

should be

g.check()

"Checker" would be an appropriate name for a class or variable; "check" for a function.

Grammar

you losed -> you lost

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.