Skip to content

Commit f27c074

Browse files
author
CodeHS-Solutions
authored
Create 14.1.5 Guess the Word, Part 4
1 parent 40d2f22 commit f27c074

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import random
2+
words = ['hello', 'world', 'this', 'is', 'python']
3+
4+
secret_word = random.choice(words)
5+
dashes = []
6+
for i in range(len(secret_word)):
7+
dashes.append("-")
8+
print("".join(dashes))
9+
guesses_left = 10
10+
won = False
11+
12+
13+
def get_guess():
14+
while True:
15+
guess = (input("Guess: ")).lower()
16+
if len(guess) != 1:
17+
print("Your guess must have exactly one character! ")
18+
elif guess.isdigit() == True:
19+
print("Your guess must be a lowercase letter! ")
20+
elif guess is not None:
21+
return guess
22+
else:
23+
continue
24+
25+
def update_dashes(dashes, secret_word, guess, guesses_left):
26+
for i in range(len(secret_word)):
27+
if guess == secret_word[i]:
28+
dashes[i] = guess
29+
return dashes
30+
31+
32+
while won == False and guesses_left != 0:
33+
if ("".join(dashes)) == secret_word:
34+
won = True
35+
else:
36+
print(str(guesses_left) + " incorrect guesses left.")
37+
guess = get_guess()
38+
if guess in secret_word:
39+
dashes = update_dashes(dashes, secret_word, guess, guesses_left)
40+
print("That letter is in the word")
41+
else:
42+
print("That letter is not in the secret word! ")
43+
guesses_left -= 1
44+
if guesses_left != 0:
45+
print("".join(dashes))
46+
47+
if won == True:
48+
print("Congrats! You win. The word was: " + str(secret_word))
49+
else:
50+
print("You lose! The word was: " + str(secret_word))

0 commit comments

Comments
 (0)