You are on page 1of 1
10.1.4: Guess the Word, Part 3 CE ap @ In this exercise, you should start with your solution code for Guess the Word, Part 2. [Atthis point. you should have a program that can print a string with dashes and letters in the correct places based on the users guesses. 1 # Repeatedly asks the user for a guess until they enter a valid lowercase letter. Returns the guess. 2+ def get_guess(): 3+ while True: 4a guess = input(“cuess: 5. if len(guess) |= 1: 6 rint "Your guess mist have exactly ane character!” 7 elif not guess.islower(): a print "Your guess must be 2 lowercase letter!" 3 else: 10 return guess uu 12 # update_dashes: This function takes the word, the current state of dashes, and the user's last guess. 13 # It returns a version of dashes with all instances of the guess exposed. 14> def update _dashes(word, dashes, guess): 15+ for 4 in range(len(word)): 16- if word[i] == guess vy dashes = dashes[:i] + guess + dashes[i + 1:] 18 return dashes 19 20 # store word 21 secret_word - “eggplant 22 # Store dashes 23 dashes = “-" * len(secret_word) 24 # Guesses left 25 guesses left = 10 27 # Repeatedly ask for guesses 28~ while guesses_left > @ and dashes !- secret_wor 29° print dashes 30 print str(guesses left) + " incorrect guesses left. 31 32 Get guess, update game state 33 guess = get_guess() 34 dashes ~ update_dashes(secret_word, dashes, guess) 35~ if guess in secret_word 36 print “That letter is in the secret word!” 37> else 38 print "That letter is not in the secret word.” 33 guesses left = guesses_left - 1 40 41 Print result 42> if guesses_left =- 6 43 print "You lose. The word was: \ secret_word 45 print “Congrats! You win. The word was: “ + secret_word

You might also like