Skip to content

Commit

Permalink
Create 14.1.5 Guess the Word, Part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeHS-Solutions authored Mar 2, 2024
1 parent 40d2f22 commit f27c074
Showing 1 changed file with 50 additions and 0 deletions.
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))

0 comments on commit f27c074

Please sign in to comment.