-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05603ae
commit 5b7078d
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import random | ||
#,"programming","data","computer","cloud" | ||
|
||
def hangman(): | ||
|
||
words = ["python","algorithm"] | ||
word = random.choice(words).lower() | ||
attempts = 6 | ||
guessed_word = ['_']*len(word) | ||
guessed_letter = [] | ||
|
||
print("Welcome to Hangman!") | ||
print("You have to guess the word one letter at time.") | ||
print(f"The word has {len(word)} letters.") | ||
print(" ".join(guessed_word)) | ||
|
||
while attempts > 0 and '_' in guessed_word: | ||
print(f"\nRemaning attempts: {attempts}") | ||
guess = input("Enter letter:") | ||
|
||
if len(guess) != 1 or not guess.isalpha(): | ||
print("Please guess a single letter") | ||
continue | ||
|
||
if guess in guessed_letter: | ||
print("You already guessed that letter. Try another one") | ||
continue | ||
|
||
guessed_letter.append(guess) | ||
|
||
if guess in word: | ||
print(f"Good job! '{guess}' is in the word.") | ||
for i,letter in enumerate(word): | ||
if letter == guess: | ||
guessed_word[i] = guess | ||
|
||
else: | ||
print(f"Sorry, '{guess}' is not in the word.") | ||
attempts -= 1 | ||
|
||
print(" ".join(guessed_word)) | ||
|
||
|
||
if "_" not in guessed_word: | ||
print("\nCongratulations! You've guessed the word:",word) | ||
else: | ||
print("\nGame over! The word was:",word) | ||
|
||
|
||
hangman() |