diff --git a/05/game_utils.py b/05/game_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..cb617b6dd5d56c22ff2d50b7f0544d3ac919aa70 --- /dev/null +++ b/05/game_utils.py @@ -0,0 +1,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"]))