Home > Software design >  how to convert a program made with python classes to a program only with functions
how to convert a program made with python classes to a program only with functions

Time:11-05

I was making a hangman game using a class. I was wondering how can I delete the class and run everything into a main function. I'm leaving the code here. Every feedback is appreciate it and it can really help me learn. Thank you.

class game:

    def __init__(user):
        user.guesses = 0
        user.letters_guessed = ""
        user.secret_word = user.secret_word()
        user.progress = list("?" * len(user.secret_word))
        user.start_game()

    def secret_word(user):
        while True:
            word = input("Please enter word to be guessed\nthat does not contain ? or white space: ")
            if " " in word or '?' in word:
                continue
            else:
                return word

    def get_guess(user):
        while True:
            character = input("Please enter your next guess : ")
            if len(character) > 1:
                print("you can only guess a single character")
                continue
            else:
                return character


    def display_guess(user):
        return "".join(user.progress) # user_letter_guess


    def is_game_over(user):
        if (user.guesses >= 7) or (user.secret_word == ''.join(user.progress)):
            if user.secret_word == ''.join(user.progress):
                print("\nCongratulations you cracked it...")
            else:
                print("\nSorry game over...")

            return True
        else:
            return False

    def update_progress(user, guess):
        i = 0
        while i < len(user.secret_word):
            if guess == user.secret_word[i]:
                user.progress[i] = guess

                i  = 1
            else:
                i  = 1

    def start_game(user):

        while not user.is_game_over():

            user.display_hangman()
            print("\n"   user.display_guess())
            print("So far you have guessed : "   user.letters_guessed)
            guess = user.get_guess()

            if guess in user.secret_word and guess not in user.letters_guessed:
                user.letters_guessed  = ","   guess
                user.update_progress(guess)

            elif guess not in user.secret_word and guess not in user.letters_guessed:
                user.guesses  = 1
                user.letters_guessed  = ","   guess

            else:
                print("you already guessed the character :" guess)

# display hang man graphic
    def display_hangman(user):

        if user.guesses == 0: #
            print("             ")

        elif user.guesses == 1:
            print("      |      ")
            print("      0      ")
            print("             ")
            print("             ")
            print("             ")
        elif user.guesses == 2:
            print("      |      ")
            print("      0      ")
            print("     /       ")
            print("             ")
            print("             ")
        elif user.guesses == 3:
            print("      |      ")
            print("      0      ")
            print("     /|      ")
            print("             ")
            print("             ")
        elif user.guesses == 4:
            print("      |      ")
            print("      0      ")
            print("     /|\     ")
            print("             ")
            print("             ")
        elif user.guesses == 5:
            print("      |      ")
            print("      0      ")
            print("     /|\     ")
            print("     /       ")
            print("             ")
        else:
            print("      |      ")
            print("      0      ")
            print("     /|\     ")
            print("     / \     ")
            print("             ")
            print("You failed to guess the secret word: "   user.secret_word)



run = game()

I think I have to make a main function and for the methods in the class I have to create standalone functions.

CodePudding user response:

Not sure why you'd want to do this after all of your current work, but the way that classes work is when they are instantiated using game(), their __init__ function is called. Your variable name "user" in these methods should follow standard practice and be called "self". To convert the member functions to basic functions, well, just delete line 1 (class game:), then remove the 4 spaces from all code within the class. Then make the member variables (like guesses) into either global variables or function parameters (so change your def function(...'s):). And put the code from __init__ into your main() function. Create a new line at the bottom of your code that looks like:

def main():
    # Insert your variables and function calls here...
 
if __name__ == "__main__":
    main()

And then run your code as a script like $ python your_filename.py.

However, if the case was that you couldn't figure classes out, you were getting close. Your use of user variable is probably confusing and again should be self. Maybe all you were missing was that after you created run, you then need to start calling member functions in a mainloop, like run.start_game() or whatever.

  • Related