|
| 1 | +# hangman project - unit six part 1 exercise. |
| 2 | +# author - lirom mizrahi |
| 3 | +''' |
| 4 | +To remind you, in the unfolding task at the end of the previous unit, you wrote a function to check the correctness |
| 5 | +of input from the user. In this exercise, you will upgrade the function so that it also refers to the letters that |
| 6 | +the player has already guessed in the game in the tests. |
| 7 | +
|
| 8 | +Write the function, this time call it check_valid_input, and define it as follows: |
| 9 | +
|
| 10 | +def check_valid_input(letter_guessed, old_letters_guessed): |
| 11 | +Acceptance values of the function: |
| 12 | +A string called letter_guessed. The string represents the character received from the player. |
| 13 | +A list called old_letters_guessed. The list contains the letters the player has guessed so far. |
| 14 | +The return values of the function |
| 15 | +The function returns a Boolean value representing the correctness of the string and whether the user has already guessed the character before. |
| 16 | +
|
| 17 | +The function returns false (False, an invalid string) in the following cases: |
| 18 | +If the letter_guessed string consists of two or more characters |
| 19 | +If the string letter_guessed contains a sign that is not an English letter (like: &, *) |
| 20 | +If the letter_guessed string is a character already in the old_letters_guessed list (that is, this character has |
| 21 | +been guessed before and therefore it is illegal to guess it again) |
| 22 | +The function returns true (True, a valid character) if the string letter_guessed consists of only one letter |
| 23 | +that is an English letter (and not another sign) that is not in the list old_letters_guessed |
| 24 | +
|
| 25 | +Examples of running the check_valid_input function |
| 26 | +
|
| 27 | +>>> old_letters = ['a', 'b', 'c'] |
| 28 | +>>> check_valid_input('C', old_letters) |
| 29 | +False |
| 30 | +>>> check_valid_input('ep', old_letters) |
| 31 | +False |
| 32 | +>>> check_valid_input('$', old_letters) |
| 33 | +False |
| 34 | +>>> check_valid_input('s', old_letters) |
| 35 | +True |
| 36 | +
|
| 37 | +''' |
| 38 | + |
| 39 | + |
| 40 | +def error_check(user_note): |
| 41 | + if len(user_note) > 1: |
| 42 | + if user_note.isalpha(): |
| 43 | + return "E1" |
| 44 | + else: |
| 45 | + return "E3" |
| 46 | + elif len(user_note) == 1 and user_note.isalpha() != True: |
| 47 | + return "E2" |
| 48 | + else: |
| 49 | + return user_note.lower() |
| 50 | + |
| 51 | + |
| 52 | +def check_valid_input(letter_guessed, old_letters_guessed): |
| 53 | + if letter_guessed == "E1" or letter_guessed == "E3" or letter_guessed == "E2": |
| 54 | + return False |
| 55 | + else: |
| 56 | + if letter_guessed not in old_letters_guessed: |
| 57 | + old_letters_guessed = old_letters_guessed.append(letter_guessed) |
| 58 | + return True |
| 59 | + else: |
| 60 | + return False |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + guess_a_letter = input("Guess a letter:") |
| 65 | + letter_guessed = error_check(guess_a_letter) |
| 66 | + check_valid = check_valid_input(letter_guessed, old_letters_guessed=['a', 'b', 'c']) |
| 67 | + print(check_valid) |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + main() |
0 commit comments