Skip to main content
Post Reopened by Peilonrayz, 301_Moved_Permanently, Vogel612
Edit question so it's a little cleaner.
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

Refactoring python Secret hangman game codehomework

Recently I was given homework to make a hangman homework test andgame, where I wrote something likethe below (python3)..
How can I refactor this to use classes?

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()

How do I refactor this to use Classes?

Refactoring python hangman game code

Recently I was given a hangman homework test and I wrote something like below (python3)..

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()

How do I refactor this to use Classes?

Secret hangman homework

Recently I was given homework to make a hangman game, where I wrote the below.
How can I refactor this to use classes?

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()
Post Closed as "Not suitable for this site" by πάντα ῥεῖ, Stephen Rauch, Billal Begueradj, Daniel, Sᴀᴍ Onᴇᴌᴀ

refactoring Refactoring python hangman game code

Recently I was given a hangman homework test and I wrote something like below (python3)..

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()

howHow do I refactor this to use Classes?

refactoring python hangman game code

Recently I was given a hangman homework test and I wrote something like below (python3)..

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()

how do I refactor this to use Classes?

Refactoring python hangman game code

Recently I was given a hangman homework test and I wrote something like below (python3)..

import secrets


def guess_the_word():
    tries_left = 9
    output = ["_"] * len(secret_word)

    print(str(' '.join([letter for letter in output])).strip())

    while tries_left >= 0 and not secret_word == str(''.join([letter for letter in output])):
        # print tries left
        print("\nYou have {} tries left".format(tries_left))
        # display the _ which is length of the secret_word and fill in _ as and when the user 
        # types correct word

        guessed_letter = input("Guess a letter from secret word:")

        if guessed_letter in secret_word:
            print("You guessed correct")
            for i, x in enumerate(secret_word):
                if x is guessed_letter:
                    output[i] = guessed_letter
        else:
            tries_left -= 1
            print("You guessed incorrect")

        print(str(' '.join([letter for letter in output])).strip())


print("\nWelcome to Hangman Game!\n")
# open words.txt file to read words
with open("words.txt") as words_file:
    words_list = words_file.read().split()
# choose a random word from words_list
secret_word = secrets.choice(words_list)
guess_the_word()

How do I refactor this to use Classes?

edited tags; edited tags; edited tags
Link
Source Link
vkk07
  • 31
  • 1
Loading