Helper 4: Getting the user\'s guess [7 marks]¶ get_guessed_word(word_to_guess, l
ID: 3920904 • Letter: H
Question
Helper 4: Getting the user's guess [7 marks]¶
get_guessed_word(word_to_guess, letters_guessed): takes two parameters: a string, word_to_guess, and a list of letter, letters_guessed. This function returns a string that is comprised of letters and underscores, spaces are optional, based on what letters in letters_guessed are in word_to_guess. This shouldn't be too different from isGoodGuess!
For readability separate each underscore with a space: _ _ _ _. Without a space it is hard to distinguish whether ____ is four elements or three ___. Adding the space helps to improve your program's usability.
def get_guessed_word(word_to_guess, letters_guessed):
""" Provide function docstring
"""
return "YOUR ANSWER HERE"
Test get_guessed_word() to verify that the created string correctly presents the guessed letters in word_to_guess
def a4():
guess1 = get_guessed_word('apple', ['i'])
guess2 = get_guessed_word('apple', ['l', 'i', 'k', 't', 'r', 's'])
guess3 = get_guessed_word('apple', ['a', 'l', 'k', 'p', 'r', 'e'])
if guess1 == '_ _ _ _ _' and guess2 == '_ _ _ l_' and guess3 == 'apple':
return 'Correct'
else:
return 'Wrong'
a4()
Explanation / Answer
def get_guessed_word(word_to_guess, letters_guessed): result = '' for i in range(len(word_to_guess)): ch = word_to_guess[i] if ch in letters_guessed: result += ch else: result += '_' if i != len(word_to_guess) - 1: result += ' ' return result def a4(): guess1 = get_guessed_word('apple', ['i']) guess2 = get_guessed_word('apple', ['l', 'i', 'k', 't', 'r', 's']) guess3 = get_guessed_word('apple', ['a', 'l', 'k', 'p', 'r', 'e']) if guess1 == '_ _ _ _ _' and guess2 == '_ _ _ l_' and guess3 == 'apple': return 'Correct' else: return 'Wrong' print(a4())
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.