Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCC10 completed by ananda23-cs #864

Open
wants to merge 1 commit into
base: community
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions 10/ananda23-cs/hangman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from string import ascii_lowercase
import sys

from movies import get_movie as get_word # keep interface generic
from graphics import hang_graphics

ASCII = list(ascii_lowercase)
HANG_GRAPHICS = list(hang_graphics())
ALLOWED_GUESSES = len(HANG_GRAPHICS)
PLACEHOLDER = '_'


class Hangman(object):
def __init__(self, word: str) -> None:
self.correct_word = word
self.wrong_guesses = 0
self.guesses = set()

def __str__(self):
return HANG_GRAPHICS[self.wrong_guesses] + f"\n{self.status}"

@property
def status(self):
statusString = ""
for c in self.correct_word:
if c.lower() in self.guesses or c in [" ", "'", ".", ":"]:
statusString += c
else:
statusString += PLACEHOLDER
return statusString

def guess(self):
inputGuess = input("Guess any letter: ")
if len(inputGuess) > 1:
raise ValueError("Can't guess more than one letter")
else:
return inputGuess

def play(self):
while self.wrong_guesses < ALLOWED_GUESSES-1:
print(str(self))
if self.status == self.correct_word:
break
else:
try:
charGuessed = self.guess()
if charGuessed not in self.guesses:
self.guesses.add(charGuessed)
if charGuessed not in self.correct_word.casefold():
self.wrong_guesses += 1
else:
print(f"Sorry. The letter '{charGuessed}' already taken. Try again.")
except ValueError as e:
print(str(e))
if self.wrong_guesses == ALLOWED_GUESSES-1:
print(str(self) + f"\nToo bad. The word was {self.correct_word}.")
else:
print("Nice going. You guessed the correct word.")
print("Thanks for playing.")
# or use functions ...


if __name__ == '__main__':
if len(sys.argv) > 1:
word = sys.argv[1]
else:
word = get_word()
#print(word)

# init / call program
game = Hangman(word)
game.play()