|
| 1 | +# hangman project - unit seven part 1 and 2 exercise. |
| 2 | +# author - lirom mizrahi |
| 3 | +''' |
| 4 | +7.3.1 |
| 5 | +In this exercise you will show the player his progress in guessing the secret word. |
| 6 | +
|
| 7 | +Write a function called show_hidden_word defined as follows: |
| 8 | +
|
| 9 | +def show_hidden_word(secret_word, old_letters_guessed): |
| 10 | +The function's acceptance values |
| 11 | +A string called secret_word. The string represents the secret word that the player has to guess. |
| 12 | +A list called old_letters_guessed. The list contains the letters the player has guessed so far. |
| 13 | +The return values of the function |
| 14 | +The function returns a string consisting of letters and underscores. The string shows the |
| 15 | +letters from the old_letters_guessed list that are in the secret_word string in their |
| 16 | +appropriate position, and the other letters in the string (which the player has not yet guessed) |
| 17 | +as underlines. |
| 18 | +
|
| 19 | +Example of running the show_hidden_word function: |
| 20 | +
|
| 21 | +>>> secret_word = "mammals" |
| 22 | +>>> old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k'] |
| 23 | +>>> print(show_hidden_word(secret_word, old_letters_guessed)) |
| 24 | +m _ m m _ _ s |
| 25 | +Guidelines |
| 26 | +To make it clear to the player how many letters are left to guess, space the string |
| 27 | +when you print the underscores |
| 28 | +
|
| 29 | +7.3.2 |
| 30 | +Time to check if the player has already won, right? |
| 31 | +In this exercise you will write a function that checks whether the player was able to guess the secret word and thus won the game! |
| 32 | +
|
| 33 | +Write a function called check_win defined as follows: |
| 34 | +
|
| 35 | +def check_win(secret_word, old_letters_guessed): |
| 36 | +The function's acceptance values |
| 37 | +A string called secret_word. The string represents the secret word that the player has to guess. |
| 38 | +A list called old_letters_guessed. The list contains the letters the player has guessed so far. |
| 39 | +The return values of the function |
| 40 | +The function returns true if all the letters that make up the secret word are included in the list of letters that the user guessed. Otherwise, the function returns false. |
| 41 | +
|
| 42 | +Examples of running the check_win function |
| 43 | +>>> secret_word = "friends" |
| 44 | +>>> old_letters_guessed = ['m', 'p', 'j', 'i', 's', 'k'] |
| 45 | +>>> print(check_win(secret_word, old_letters_guessed)) |
| 46 | +False |
| 47 | +>>> secret_word = "yes" |
| 48 | +>>> old_letters_guessed = ['d', 'g', 'e', 'i', 's', 'k', 'y'] |
| 49 | +>>> print(check_win(secret_word, old_letters_guessed)) |
| 50 | +True |
| 51 | +
|
| 52 | +''' |
| 53 | + |
| 54 | +def show_hidden_word(secret_word, old_letters_guessed): |
| 55 | + result = '' |
| 56 | + for letter in secret_word: |
| 57 | + if letter in old_letters_guessed: |
| 58 | + result += letter + ' ' |
| 59 | + else: |
| 60 | + result += '_ ' |
| 61 | + return result.strip() |
| 62 | + |
| 63 | +def check_win(secret_word, old_letters_guessed): |
| 64 | + for letter in secret_word: |
| 65 | + if letter not in old_letters_guessed: |
| 66 | + return False |
| 67 | + return True |
| 68 | + |
| 69 | +def main(): |
| 70 | + secret_word = "lironrrrg" |
| 71 | + old_letters_guessed = ['s', 'p', 'j', 'i', 'l', 'r'] |
| 72 | + print(show_hidden_word(secret_word, old_letters_guessed)) |
| 73 | + secret_word = "friends" |
| 74 | + old_letters_guessed = ['m', 'p', 'j', 'i', 's', 'k'] |
| 75 | + print(check_win(secret_word, old_letters_guessed)) |
| 76 | + secret_word = "yes" |
| 77 | + old_letters_guessed = ['d', 'g', 'e', 'i', 's', 'k', 'y'] |
| 78 | + print(check_win(secret_word, old_letters_guessed)) |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
| 81 | + |
0 commit comments