From ddacedb0b08e31f1476064bc941903f119ad8d96 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 10 Mar 2022 06:48:51 +0100 Subject: [PATCH] STY: Fix Flake8 issues --- 46_Hexapawn/python/hexapawn.py | 4 +- 64_Nicomachus/python/nicomachus.py | 8 +- 70_Poetry/python/poetry.py | 39 ++-- 79_Slalom/python/slalom.py | 295 ++++++++++++++++------------- 4 files changed, 196 insertions(+), 150 deletions(-) diff --git a/46_Hexapawn/python/hexapawn.py b/46_Hexapawn/python/hexapawn.py index e89e3426..79d19f3b 100644 --- a/46_Hexapawn/python/hexapawn.py +++ b/46_Hexapawn/python/hexapawn.py @@ -362,12 +362,12 @@ def human_has_move(board): def get_board_spaces(): - """ generates the space names (1-9)""" + """generates the space names (1-9)""" yield from range(1, 10) def get_board_spaces_with(board, val): - """ generates spaces containing pieces of type val""" + """generates spaces containing pieces of type val""" for i in get_board_spaces(): if board_contents(board, i) == val: yield i diff --git a/64_Nicomachus/python/nicomachus.py b/64_Nicomachus/python/nicomachus.py index 33c5ab58..861d5555 100644 --- a/64_Nicomachus/python/nicomachus.py +++ b/64_Nicomachus/python/nicomachus.py @@ -16,6 +16,7 @@ filenames. import time + def print_with_tab(spaces_count, msg): if spaces_count > 0: spaces = " " * spaces_count @@ -23,6 +24,7 @@ def print_with_tab(spaces_count, msg): spaces = "" print(spaces + msg) + def get_yes_or_no(): while True: response = input().upper() @@ -59,7 +61,7 @@ def play_game(): print("I FEEL YOUR ARITHMETIC IS IN ERROR.") print() print("LET'S TRY ANOTHER") - + def main(): print_with_tab(33, "NICOMA") @@ -67,12 +69,12 @@ def main(): print() print() print() - + print("BOOMERANG PUZZLE FROM ARITHMETICA OF NICOMACHUS -- A.D. 90!") print() while True: play_game() - + if __name__ == "__main__": main() diff --git a/70_Poetry/python/poetry.py b/70_Poetry/python/poetry.py index 9e9a1af1..3866c10a 100644 --- a/70_Poetry/python/poetry.py +++ b/70_Poetry/python/poetry.py @@ -27,22 +27,27 @@ k = 0 phrase = 1 line = "" + def print_centered(msg): spaces = " " * ((PAGE_WIDTH - len(msg)) // 2) - print (spaces + msg) + print(spaces + msg) + def process_phrase_1(): global line - - line_1_options = ["MIDNIGHT DREARY", - "FIERY EYES", - "BIRD OR FIEND", - "THING OF EVIL", - "PROPHET"] + + line_1_options = [ + "MIDNIGHT DREARY", + "FIERY EYES", + "BIRD OR FIEND", + "THING OF EVIL", + "PROPHET", + ] line = line + line_1_options[i] return line + def process_phrase_2(): global line global u @@ -52,12 +57,14 @@ def process_phrase_2(): ("THRILLED ME", None), ("STILL SITTING....", None), ("NEVER FLITTING", 2), - ("BURNED", None)] + ("BURNED", None), + ] words, u_modifier = line_2_options[i] line += words if not (u_modifier is None): u = u_modifier + def process_phrase_3(): global line @@ -73,6 +80,7 @@ def process_phrase_3(): if (not only_if_u) or (u > 0): line = line + words + def process_phrase_4(): global line @@ -81,10 +89,12 @@ def process_phrase_4(): ("YET AGAIN"), ("SLOWLY CREEPING"), ("...EVERMORE"), - ("NEVERMORE")] + ("NEVERMORE"), + ] line += phrases[i] + def maybe_comma(): # line 210 global u @@ -112,7 +122,7 @@ def pick_phrase(): global phrase global line global i, j, k - + i = random.randint(0, 4) j += 1 k += 1 @@ -121,7 +131,7 @@ def pick_phrase(): # random indentation is fun! line += " " * 5 phrase = j + 1 - + def main(): print_centered("POETRY") @@ -136,9 +146,9 @@ def main(): 1: process_phrase_1, 2: process_phrase_2, 3: process_phrase_3, - 4: process_phrase_4 + 4: process_phrase_4, } - + while True: if phrase >= 1 and phrase <= 4: phrase_processors[phrase]() @@ -155,6 +165,7 @@ def main(): phrase = 2 continue pick_phrase() - + + if __name__ == "__main__": main() diff --git a/79_Slalom/python/slalom.py b/79_Slalom/python/slalom.py index a764e84a..4e361f26 100644 --- a/79_Slalom/python/slalom.py +++ b/79_Slalom/python/slalom.py @@ -4,162 +4,195 @@ print("Slalom".rjust(39)) print("Creative Computing Morristown, New Jersey\n\n\n".rjust(57)) medals = { - "gold": 0, - "silver": 0, - "bronze": 0, + "gold": 0, + "silver": 0, + "bronze": 0, } -max_speeds = [14,18,26,29,18,25,28,32,29,20,29,29,25,21,26,29,20,21,20,18,26,25,33,31,22] +max_speeds = [ + 14, + 18, + 26, + 29, + 18, + 25, + 28, + 32, + 29, + 20, + 29, + 29, + 25, + 21, + 26, + 29, + 20, + 21, + 20, + 18, + 26, + 25, + 33, + 31, + 22, +] + def ask(question): - print(question, end="? ") - return input().upper() + print(question, end="? ") + return input().upper() + def ask_int(question): - reply = ask(question) - return int(reply) if reply.isnumeric() else -1 + reply = ask(question) + return int(reply) if reply.isnumeric() else -1 + def pre_run(): - print("\nType \"INS\" for instructions") - print("Type \"MAX\" for approximate maximum speeds") - print("Type \"RUN\" for the beginning of the race") - cmd = ask("Command--") - while cmd != "RUN": - if cmd == "INS": - print("\n*** Slalom: This is the 1976 Winter Olypic Giant Slalom. You are") - print(" the American team's only hope for a gold medal.\n") - print(" 0 -- Type this if you want to see how long you've taken.") - print(" 1 -- Type this if you want to speed up a lot.") - print(" 2 -- Type this if you want to speed up a little.") - print(" 3 -- Type this if you want to speed up a teensy.") - print(" 4 -- Type this if you want to keep going the same speed.") - print(" 5 -- Type this if you want to check a teensy.") - print(" 6 -- Type this if you want to check a little.") - print(" 7 -- Type this if you want to check a lot.") - print(" 8 -- Type this if you want to cheat and try to skip a gate.\n") - print(" The place to use these options is when the Computer asks:\n") - print("Option?\n") - print(" Good Luck!\n") - cmd = ask("Command--") - elif cmd == "MAX": - print("Gate Max") - print(" # M.P.H.") - print("----------") - for i in range(0, gates): - print(f" {i + 1} {max_speeds[i]}") - cmd = ask("Command--") - else: - cmd = ask(f"\"{cmd}\" is an illegal command--Retry") + print('\nType "INS" for instructions') + print('Type "MAX" for approximate maximum speeds') + print('Type "RUN" for the beginning of the race') + cmd = ask("Command--") + while cmd != "RUN": + if cmd == "INS": + print("\n*** Slalom: This is the 1976 Winter Olypic Giant Slalom. You are") + print(" the American team's only hope for a gold medal.\n") + print(" 0 -- Type this if you want to see how long you've taken.") + print(" 1 -- Type this if you want to speed up a lot.") + print(" 2 -- Type this if you want to speed up a little.") + print(" 3 -- Type this if you want to speed up a teensy.") + print(" 4 -- Type this if you want to keep going the same speed.") + print(" 5 -- Type this if you want to check a teensy.") + print(" 6 -- Type this if you want to check a little.") + print(" 7 -- Type this if you want to check a lot.") + print(" 8 -- Type this if you want to cheat and try to skip a gate.\n") + print(" The place to use these options is when the Computer asks:\n") + print("Option?\n") + print(" Good Luck!\n") + cmd = ask("Command--") + elif cmd == "MAX": + print("Gate Max") + print(" # M.P.H.") + print("----------") + for i in range(0, gates): + print(f" {i + 1} {max_speeds[i]}") + cmd = ask("Command--") + else: + cmd = ask(f'"{cmd}" is an illegal command--Retry') + def run(): - global medals - print("The starter counts down...5...4...3...2...1...Go!") - time = 0 - speed = int(random() * (18 - 9) + 9) - print("You're off") - for i in range(0, gates): - while True: - print(f"\nHere comes gate #{i + 1}:") - print(f" {int(speed)} M.P.H.") - old_speed = speed - opt = ask_int("Option") - while opt < 1 or opt > 8: - if(opt == 0): - print(f"You've taken {int(time)} seconds.") - else: - print("What?") - opt = ask_int("Option") + global medals + print("The starter counts down...5...4...3...2...1...Go!") + time = 0 + speed = int(random() * (18 - 9) + 9) + print("You're off") + for i in range(0, gates): + while True: + print(f"\nHere comes gate #{i + 1}:") + print(f" {int(speed)} M.P.H.") + old_speed = speed + opt = ask_int("Option") + while opt < 1 or opt > 8: + if opt == 0: + print(f"You've taken {int(time)} seconds.") + else: + print("What?") + opt = ask_int("Option") - if opt == 8: - print("***Cheat") - if random() < .7: - print("An official caught you!") - print(f"You took {int(time + random())} seconds.") - return - else: - print("You made it!") - time += 1.5 - else: - match opt: - case 1: - speed += int(random() * (10 - 5) + 5) + if opt == 8: + print("***Cheat") + if random() < 0.7: + print("An official caught you!") + print(f"You took {int(time + random())} seconds.") + return + else: + print("You made it!") + time += 1.5 + else: + match opt: + case 1: + speed += int(random() * (10 - 5) + 5) - case 2: - speed += int(random() * (5 - 3) + 3) + case 2: + speed += int(random() * (5 - 3) + 3) - case 3: - speed += int(random() * (4 - 1) + 1) + case 3: + speed += int(random() * (4 - 1) + 1) - case 5: - speed -= int(random() * (4 - 1) + 1) + case 5: + speed -= int(random() * (4 - 1) + 1) - case 6: - speed -= int(random() * (5 - 3) + 3) + case 6: + speed -= int(random() * (5 - 3) + 3) + + case 7: + speed -= int(random() * (10 - 5) + 5) + print(f" {int(speed)} M.P.H.") + if speed > max_speeds[i]: + if random() < ((speed - max_speeds[i]) * 0.1) + 0.2: + print( + f"You went over the maximum speed and {'snagged a flag' if random() < .5 else 'wiped out'}!" + ) + print(f"You took {int(time + random())} seconds") + return + else: + print("You went over the maximum speed and made it!") + if speed > max_speeds[i] - 1: + print("Close one!") + if speed < 7: + print("Let's be realistic, ok? Let's go back and try again...") + speed = old_speed + else: + time += max_speeds[i] - speed + 1 + if speed > max_speeds[i]: + time += 0.5 + break + print(f"\nYou took {int(time + random())} seconds.") + avg = time / gates + if avg < 1.5 - (lvl * 0.1): + print("Yout won a gold medal!") + medals["gold"] += 1 + elif avg < 2.9 - (lvl * 0.1): + print("You won a silver medal!") + medals["silver"] += 1 + elif avg < 4.4 - (lvl * 0.01): + print("You won a bronze medal!") + medals["bronze"] += 1 - case 7: - speed -= int(random() * (10 - 5) + 5) - print(f" {int(speed)} M.P.H.") - if speed > max_speeds[i]: - if random() < ((speed - max_speeds[i]) * .1) + .2: - print(f"You went over the maximum speed and {'snagged a flag' if random() < .5 else 'wiped out'}!") - print(f"You took {int(time + random())} seconds") - return - else: - print("You went over the maximum speed and made it!") - if speed > max_speeds[i] - 1: - print("Close one!") - if speed < 7: - print("Let's be realistic, ok? Let's go back and try again...") - speed = old_speed - else: - time += max_speeds[i] - speed + 1 - if speed > max_speeds[i]: - time += .5 - break - print(f"\nYou took {int(time + random())} seconds.") - avg = time / gates - if avg < 1.5 - (lvl * .1): - print("Yout won a gold medal!") - medals["gold"] += 1 - elif avg < 2.9 - (lvl * .1): - print("You won a silver medal!") - medals["silver"] += 1 - elif avg < 4.4 - (lvl * .01): - print("You won a bronze medal!") - medals["bronze"] += 1 while True: - gates = ask_int("How many gates does this course have (1 to 25)") - if gates < 1: - print("Try again,") - else: - if(gates > 25): - print("25 is the limit.") - break + gates = ask_int("How many gates does this course have (1 to 25)") + if gates < 1: + print("Try again,") + else: + if gates > 25: + print("25 is the limit.") + break pre_run() while True: - lvl = ask_int("Rate yourself as a skier, (1=Worst, 3=Best)") - if lvl < 1 or lvl > 3: - print("The bounds are 1-3.") - else: - break + lvl = ask_int("Rate yourself as a skier, (1=Worst, 3=Best)") + if lvl < 1 or lvl > 3: + print("The bounds are 1-3.") + else: + break while True: - run() - while True: - answer = ask("Do you want to play again?") - if answer == "YES" or answer == "NO": - break - else: - print("Please type \"YES\" or \"NO\"") - if answer == "NO": - break + run() + while True: + answer = ask("Do you want to play again?") + if answer == "YES" or answer == "NO": + break + else: + print('Please type "YES" or "NO"') + if answer == "NO": + break print("Thanks for the race") if medals["gold"] > 0: - print(f"Gold medals: {medals['gold']}") + print(f"Gold medals: {medals['gold']}") if medals["silver"] > 0: - print(f"Silver medals: {medals['silver']}") + print(f"Silver medals: {medals['silver']}") if medals["bronze"] > 0: - print(f"Bronze medals: {medals['bronze']}") + print(f"Bronze medals: {medals['bronze']}")