Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
lecture-code
Commits
53d09884
Commit
53d09884
authored
5 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Add new file
parent
c8eb8152
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
05/game_utils.py
+103
-0
05/game_utils.py
with
103 additions
and
0 deletions
+103
-0
05/game_utils.py
0 → 100644
View file @
53d09884
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"
]))
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help