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
102
103
UPPERCASE_LETTERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
LOWERCASE_LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
def to_uppercase(letter: str) -> str:
if letter in LOWERCASE_LETTERS:
# swap it out for the uppercase version
idx: int = LOWERCASE_LETTERS.index(letter)
upper_version: str = UPPERCASE_LETTERS[idx]
return upper_version
return letter
def uppercase(word: str) -> str:
"""
Returns a new version of `word` where every letter is uppercased.
Args:
word (str): The string to uppercase
Returns:
str: The uppercased version of `word`
"""
output: str = ""
for letter in word:
letter = to_uppercase(letter)
output += letter
return output
#####################
"""
done: bool = False
while not done:
ans = input("Are we done yet?").lower()
if ans == "yes":
done = True
done: bool = False
while not done:
ans = input("Are we done yet?").lower()
done = ans == "yes"
ans: str = "no"
while ans != "yes":
ans = input("Are we done yet?").lower()
"""
#####################
def ask_for_choice(prompt: str, valid_choices: list[str]) -> str:
"""
Repeatedly prompts the user asking them to make a choice that is
a member of `valid_choices`.
Args:
prompt (str): The prompt to give the user
valid_choices (list[str]): A list of valid choices
Returns:
str: The choice the user inputed
"""
resp = None
while resp not in valid_choices:
resp = input(prompt)
assert isinstance(resp, str)
return resp
def is_answer(s: str | None, yn: set) -> bool:
"""
Returns True when `s` is a member of `yn` and False otherwise
Args:
s(str | None): The string to test
yn(set): A set of potential valid answers
Returns:
bool: True iff `s` in `yn`
"""
return False
def prompt_yes_no(prompt: str) -> bool:
"""
Repeatedly prompts the user with prompt until they respond with
an answer starting with y or n.
Args:
prompt (str): The prompt to give to the user
Returns:
bool: Returns True when the user eventually inputs y and False when they input n.
"""
result = None
while not is_answer(result, set(["Y", "N"])):
result = input(prompt)
return is_answer(result, set(["Y"]))