-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangmanPython.py
More file actions
45 lines (37 loc) · 1.29 KB
/
hangmanPython.py
File metadata and controls
45 lines (37 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import random
def choose_word():
words = ["apple", "banana", "orange", "grape", "kiwi", "pineapple"]
return random.choice(words)
def display_word(word, guessed_letters):
displayed_word = ""
for letter in word:
if letter in guessed_letters:
displayed_word += letter
else:
displayed_word += "_"
return displayed_word
def hangman():
word = choose_word()
guessed_letters = []
attempts = 6
print("Welcome to Hangman!")
print(display_word(word, guessed_letters))
while True:
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter.")
continue
elif guess in word:
guessed_letters.append(guess)
print(display_word(word, guessed_letters))
else:
attempts -= 1
print("Incorrect! Attempts left:", attempts)
print(display_word(word, guessed_letters))
if attempts == 0:
print("You're out of attempts! The word was:", word)
break
if "_" not in display_word(word, guessed_letters):
print("Congratulations! You guessed the word:", word)
break
hangman()