I'm still a beginner in python and made this simple program to practice OOP, I've ended up using the self
keyword a lot and I'm not sure if this is normal or if I just misunderstood something.
Any criticism on the program itself or good practices in python are also greatly appreciated.
(Text File 'Words' is just 3000+ English words listed)
Edit: Instance variable cont
was only used in 1 function so it was switched to a local variable inside function play
Hangman.py
import random
from phases import hang_phases
class HangMan:
def __init__(self, random_word, letters):
self.word = random_word
self.letters = letters
self.excluded_letters = []
self.included_letters = []
self.original_word = random_word
self.chances = 9
def remove_letter(self, guessed_letter):
self.word = self.word.replace(guessed_letter, "")
self.included_letters.append(guessed_letter)
def guess_letter(self):
print(hang_phases[self.chances])
guessed_letter = input("Guess A Letter: ").upper()
if guessed_letter in self.excluded_letters:
print("You Already Guessed This Letter")
elif guessed_letter in self.word:
number_of_letter = self.word.count(guessed_letter)
if number_of_letter == 1:
print(f"There is {number_of_letter} {guessed_letter}")
else:
print(f"There are {number_of_letter} {guessed_letter}'s")
self.remove_letter(guessed_letter)
else:
print(f"There are no {guessed_letter}'s")
self.excluded_letters.append(guessed_letter)
self.chances -= 1
def play(self):
cont = True
while cont:
if self.word == "":
print(f"Congrats! The Word Was {self.original_word}")
cont = False
else:
try:
if self.chances == 0:
print("The Man Has Been Hung! Better Luck Next Time :/")
print(f"The Word Was {self.original_word}")
print(hang_phases[0])
cont = False
else:
print()
print("What Would You Like To Do?:\n*Please Enter The Integer Value Corresponding To The Action")
print("1. Guess A Letter\n"
"2. Check Included Guessed Letters\n"
"3. Check Excluded Guessed Letters\n"
"4. Get Length Of The Word\n"
"5. Give Up\n")
action = int(input("> "))
int(action)
if action == 1:
self.guess_letter()
elif action == 2:
if len(self.included_letters) == 0:
print("You Have Not Guessed Any Letters That Are In The Word")
else:
print(self.included_letters)
elif action == 3:
if len(self.excluded_letters) == 0:
print("You Have Not Guessed Any Letters That Are Not In The Word")
else:
print(self.excluded_letters)
elif action == 4:
print(len(self.original_word))
elif action == 5:
print(f"The Word Is {self.original_word}")
cont = False
else:
print("Pleas Enter A Value Between 1 & 5")
except ValueError:
print("Please Enter A Valid Integer")
def main():
play = True
while play:
word_list = open('Words').read().split("\n")
word = random.choice(word_list).upper()
hm = HangMan(word, len(word))
hm.play()
print("Play Again?")
play_again = input("Y/n > ").upper()
if play_again == "Y":
print("Setting Up...")
elif play_again == "N":
print("Goodbye")
play = False
if __name__ == "__main__":
main()
phases.py
hang_phases = [
'''
______ rip lol.
| |
| O
| \|/
| |
| _/ \_
_|_
'''
,
'''
______
| |
| O
| \|/
| |
| _/ \\
_|_
'''
,
'''
______
| |
| O
| \|/
| |
| _/
_|_
'''
,
'''
______
| |
| O
| \|/
| |
| /
_|_
'''
,
'''
______
| |
| O
| \|/
| |
|
_|_
'''
,
'''
______
| |
| O
| \|/
|
|
_|_
'''
,
'''
______
| |
| O
| \|
|
|
_|_
'''
,
'''
______
| |
| O
| |
|
|
_|_
'''
,
'''
______
| |
| O
|
|
|
_|_
'''
,
'''
______
| |
|
|
|
|
_|_
'''
]