-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7_hangman.py
More file actions
78 lines (60 loc) · 1.51 KB
/
day7_hangman.py
File metadata and controls
78 lines (60 loc) · 1.51 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
"""
words = ["cat", "dog", "book", "car", "apple"]
word = random.choice(words)
guessed = ""
tries = 6
print("Welcome to Hangman!")
while tries > 0:
# Show the word with underscores and spaces
display = ""
for letter in word:
if letter in guessed:
display += letter + " "
else:
display += "_ "
print(display.strip())
# Check win
if display.replace(" ", "") == word:
print("You win! The word was:", word)
break
guess = input("Guess a letter: ")
if guess in word:
guessed += guess
print("Good guess!")
else:
tries -= 1
print("Wrong! Tries left:", tries)
if tries == 0:
print("Game over! The word was:", word)
"""
lives = 6
word_list = ["artwark", "baboon", "camel"]
word = random.choice(word_list)
print(word)
correct_letters = []
game_over = False
while not game_over:
placeholder = ""
for i in range(len(word)):
placeholder += "-"
print(placeholder)
guess = input("Guess a letter: ").lower()
display = ""
for letter in word:
if letter == guess:
display += letter
correct_letters.append(letter)
elif letter in correct_letters:
display += letter
else:
display += "_"
if guess not in word:
lives -= 1
if lives == 0:
game_over = True
print("You lose")
if "_" not in display:
game_over = True
print("You win")
print(display)