-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 14.1.5 Guess the Word, Part 4
- Loading branch information
CodeHS-Solutions
authored
Mar 2, 2024
1 parent
40d2f22
commit f27c074
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Unit 14: Project: Guess the Word/14.1 Project: Guess the Word/14.1.5 Guess the Word, Part 4
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 | ||
words = ['hello', 'world', 'this', 'is', 'python'] | ||
|
||
secret_word = random.choice(words) | ||
dashes = [] | ||
for i in range(len(secret_word)): | ||
dashes.append("-") | ||
print("".join(dashes)) | ||
guesses_left = 10 | ||
won = False | ||
|
||
|
||
def get_guess(): | ||
while True: | ||
guess = (input("Guess: ")).lower() | ||
if len(guess) != 1: | ||
print("Your guess must have exactly one character! ") | ||
elif guess.isdigit() == True: | ||
print("Your guess must be a lowercase letter! ") | ||
elif guess is not None: | ||
return guess | ||
else: | ||
continue | ||
|
||
def update_dashes(dashes, secret_word, guess, guesses_left): | ||
for i in range(len(secret_word)): | ||
if guess == secret_word[i]: | ||
dashes[i] = guess | ||
return dashes | ||
|
||
|
||
while won == False and guesses_left != 0: | ||
if ("".join(dashes)) == secret_word: | ||
won = True | ||
else: | ||
print(str(guesses_left) + " incorrect guesses left.") | ||
guess = get_guess() | ||
if guess in secret_word: | ||
dashes = update_dashes(dashes, secret_word, guess, guesses_left) | ||
print("That letter is in the word") | ||
else: | ||
print("That letter is not in the secret word! ") | ||
guesses_left -= 1 | ||
if guesses_left != 0: | ||
print("".join(dashes)) | ||
|
||
if won == True: | ||
print("Congrats! You win. The word was: " + str(secret_word)) | ||
else: | ||
print("You lose! The word was: " + str(secret_word)) |