-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhangmanproject_unit6.1.py
101 lines (85 loc) · 3.49 KB
/
hangmanproject_unit6.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# hangman project - unit six part 2 exercise.
# author - lirom mizrahi
'''
In this exercise, you will act in accordance with the new letter that the player guessed:
either you will add it to the list of guesses, or you will print a message if it is not
possible to add it.
Write a function called try_update_letter_guessed defined as follows:
def try_update_letter_guessed(letter_guessed, old_letters_guessed):
The function's acceptance values
A string called letter_guessed. The string represents the character received from the player.
A list called old_letters_guessed. The list contains the letters the player has guessed so far.
function operation
If the character is correct (ie one English letter) and has not been guessed before, the
function will add the letter_guessed character to the old_letters_guessed list. Then it will
return a true value (True) indicating that the addition was successful.
If the character is not correct (that is, it is not a single English letter) or it is already
in the list of guesses, the function will print the character X (the capital letter X) and below
it the list old_letters_guessed as a string of small letters that are sorted from small to large
and separated from each other by arrows (an arrow consists of the signs: <- , see sample output).
The printing of the organs is to remind the player which characters he has already guessed.
At the end, the function will return a false value (False), which means that it is not possible
to add the character to the list of already guessed characters.
directive
Use the check_valid_input function that you implemented in the previous exercise.
Examples of running the function
>>> old_letters = ['a', 'p', 'c', 'f']
>>> try_update_letter_guessed('A', old_letters)
X
a -> c -> f -> p
False
>>> try_update_letter_guessed('s', old_letters)
True
>>> old_letters
['a', 'p', 'c', 'f', 's']
>>> try_update_letter_guessed('$', old_letters)
X
a -> c -> f -> p -> s
False
>>> try_update_letter_guessed('d', old_letters)
True
>>> old_letters
['a', 'p', 'c', 'f', ‘s’, 'd']
'''
def error_check(user_note):
if len(user_note) > 1:
if user_note.isalpha():
return "E1"
else:
return "E3"
elif len(user_note) == 1 and user_note.isalpha() != True:
return "E2"
else:
return user_note.lower()
def check_valid_input(letter_guessed, old_letters_guessed):
if letter_guessed == "E1" or letter_guessed == "E3" or letter_guessed == "E2":
return False
else:
if letter_guessed not in old_letters_guessed:
old_letters_guessed = old_letters_guessed.append(letter_guessed)
return True
else:
return False
def try_update_letter_guessed(letter_guessed, old_letters_guessed):
letter_guessed = error_check(letter_guessed)
if check_valid_input(letter_guessed, old_letters_guessed) == False:
print('X')
old_letters_guessed = ' -> '.join(sorted(old_letters_guessed))
print(old_letters_guessed)
return False
else:
return True
def main():
old_letters = ['a', 'p', 'c', 'f']
check_update = try_update_letter_guessed('A', old_letters)
print(check_update)
check_update = try_update_letter_guessed('s', old_letters)
print(check_update)
print(old_letters)
check_update = try_update_letter_guessed('$', old_letters)
print(check_update)
check_update = try_update_letter_guessed('d', old_letters)
print(check_update)
print(old_letters)
if __name__ == "__main__":
main()