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

first commit #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added __pycache__/images.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/words.cpython-39.pyc
Binary file not shown.
94 changes: 71 additions & 23 deletions hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
'''

def is_word_guessed(secret_word, letters_guessed):
'''
secret_word: word guess by the user
letters_guessed: list hold all the word guess by the user
returns:
return True (if user guess the world correctly )
return False (wrong selection)
'''
return False
n = len(secret_word)
for i in range(n):
if secret_word[i] not in letters_guessed:
return False
return True

# if you want to test this function please call function -> get_guessed_word("kindness", [k, n, d])

Expand Down Expand Up @@ -51,7 +48,31 @@ def get_available_letters(letters_guessed):
return sting is -> `bcdfghijklmnopqrstuvwxyz`
'''
letters_left = string.ascii_lowercase
return letters_left
word = ""
for ch in letters_left:
if ch in letters_guessed:
continue
else:
word += ""

return word


def is_valid(letter):
if len(letter) == 1:
chars = string.ascii_lowercase
if letter in chars:
return True
return False


def hang_the_man(count):
print(IMAGES[count - 1])

def give_hint(letters_guessed, secret_word):
for ch in letters_guessed:
if ch not in secret_word:
print("Hint: {}".format(ch))


def hangman(secret_word):
Expand All @@ -70,28 +91,55 @@ def hangman(secret_word):
* Display partial word guessed by the user and use underscore in place of not guess word
'''
print("Welcome to the game, Hangman!")

print("I am thinking of a word that is {} letters long.".format(
str(len(secret_word))), end='\n\n')

letters_guessed = []
flag = True
remaining_lives = 8
count = 0

available_letters = get_available_letters(letters_guessed)
print("Available letters: {} ".format(available_letters))

guess = input("Please guess a letter: ")
letter = guess.lower()

if letter in secret_word:
letters_guessed.append(letter)
print("Good guess: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
if is_word_guessed(secret_word, letters_guessed) == True:
print(" * * Congratulations, you won! * * ", end='\n\n')
else:
print("Oops! That letter is not in my word: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
letters_guessed.append(letter)
print("")
while remaining_lives > 0:

# available_letters = get_available_letters(letters_guessed)
# print("Available letters: {} ".format(available_letters))

guess = input("Please guess a letter: ")
letter = guess.lower()

if len(letter) == 4 and letter == "hint" and flag:
give_hint(letters_guessed, secret_word)
flag = False

if is_valid(letter):
remaining_lives -= 1
count += 1

hang_the_man(count)

print(remaining_lives)

if letter in secret_word:
letters_guessed.append(letter)
print("Good guess: {} ".format(get_guessed_word(secret_word, letters_guessed)))

if is_word_guessed(secret_word, letters_guessed) == True:
print(" * * Congratulations, you won! * * ", end='\n\n')
break
else:
print("Oops! That letter is not in my word: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
letters_guessed.append(letter)
print("")


if remaining_lives == 0:
print("You lost", end = '\n')
hang_the_man(count)


# Load the list of words into the variable wordlist
Expand Down