From 69944fecc1ea083f90bf656e81c15319d823a477 Mon Sep 17 00:00:00 2001 From: Gabry Date: Thu, 24 Feb 2022 19:20:23 +0100 Subject: [PATCH 1/3] Ported [79 - Slalom] to python --- 79_Slalom/python/slalom.py | 165 +++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 79_Slalom/python/slalom.py diff --git a/79_Slalom/python/slalom.py b/79_Slalom/python/slalom.py new file mode 100644 index 00000000..aab08e68 --- /dev/null +++ b/79_Slalom/python/slalom.py @@ -0,0 +1,165 @@ +from random import random + +print("SLALOM".rjust(39)) +print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n".rjust(57)) + +medals = { + "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] + +def ask(question): + print(question, end="? ") + return input().upper() + +def ask_int(question): + 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 OLYMPIC GIANT SLALOM. YOU ARE") + print(" THE AMERICAN TEAM'S ONLY HOPE OF A GOLD MEDAL.\n") + print(" 0 -- TYPE THIS IS 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") + + 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) + + case 2: + speed += int(random() * (5 - 3) + 3) + + case 3: + speed += int(random() * (4 - 1) + 1) + + case 5: + speed -= int(random() * (4 - 1) + 1) + + 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]) * .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 NAXIMUM 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("YOU 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 + +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 + +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 + +print("THANKS FOR THE RACE") +if medals["gold"] > 0: + print(f"GOLD MEDALS: {medals['gold']}") +if medals["silver"] > 0: + print(f"SILVER MEDALS: {medals['silver']}") +if medals["bronze"] > 0: + print(f"BRONZE MEDALS: {medals['bronze']}") From 74ad6e9ea7c5263b8fc739430113c6a8920370ba Mon Sep 17 00:00:00 2001 From: Gabry Date: Thu, 24 Feb 2022 19:34:26 +0100 Subject: [PATCH 2/3] Formatted the output texts to follow a more modern style --- 79_Slalom/python/slalom.py | 114 ++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/79_Slalom/python/slalom.py b/79_Slalom/python/slalom.py index aab08e68..44ce0fde 100644 --- a/79_Slalom/python/slalom.py +++ b/79_Slalom/python/slalom.py @@ -1,7 +1,7 @@ from random import random -print("SLALOM".rjust(39)) -print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n".rjust(57)) +print("Slalom".rjust(39)) +print("Creative Computing Morristown, New Jersey\n\n\n".rjust(57)) medals = { "gold": 0, @@ -19,64 +19,64 @@ def ask_int(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--") + 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 OLYMPIC GIANT SLALOM. YOU ARE") - print(" THE AMERICAN TEAM'S ONLY HOPE OF A GOLD MEDAL.\n") - print(" 0 -- TYPE THIS IS 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--") + 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("Gate Max") print(" # M.P.H.") print("----------") for i in range(0, gates): print(f" {i + 1} {max_speeds[i]}") - cmd = ask("COMMAND--") + cmd = ask("Command--") else: - cmd = ask(f"\"{cmd}\" IS AN ILLEGAL COMMAND--RETRY") + cmd = ask(f"\"{cmd}\" is an illegal command--Retry") def run(): global medals - print("THE STARTER COUNTS DOWN...5...4...3...2...1...GO!") + print("The starter counts down...5...4...3...2...1...Go!") time = 0 speed = int(random() * (18 - 9) + 9) - print("YOU'RE OFF") + 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.") + print(f"\nHere comes gate #{i + 1}:") + print(f" {int(speed)} M.P.H.") old_speed = speed - opt = ask_int("OPTION") + opt = ask_int("Option") while opt < 1 or opt > 8: if(opt == 0): - print(f"YOU'VE TAKEN {int(time)}SECONDS.") + print(f"You've taken {int(time)} seconds.") else: - print("WHAT?") - opt = ask_int("OPTION") + print("What?") + opt = ask_int("Option") if opt == 8: - print("***CHEAT") + print("***Cheat") if random() < .7: - print("AN OFFICIAL CAUGHT YOU!") - print(f"YOU TOOK {int(time + random())}SECONDS.") + print("An official caught you!") + print(f"You took {int(time + random())} seconds.") return else: - print("YOU MADE IT!") + print("You made it!") time += 1.5 else: match opt: @@ -97,69 +97,69 @@ def run(): case 7: speed -= int(random() * (10 - 5) + 5) - print(f" {int(speed)}M.P.H.") + 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") + 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 NAXIMUM SPEED AND MADE IT!") + print("You went over the maximum speed and made it!") if speed > max_speeds[i] - 1: - print("CLOSE ONE!") + print("Close one!") if speed < 7: - print("LET'S BE REALISTIC, OK? LET'S GO BACK AND TRY AGAIN...") + 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.") + print(f"\nYou took {int(time + random())} seconds.") avg = time / gates if avg < 1.5 - (lvl * .1): - print("YOU WON A GOLD MEDAL!") + print("Yout won a gold medal!") medals["gold"] += 1 elif avg < 2.9 - (lvl * .1): - print("YOU WON A SILVER MEDAL!") + print("You won a silver medal!") medals["silver"] += 1 elif avg < 4.4 - (lvl * .01): - print("YOU WON A BRONZE MEDAL!") + print("You won a bronze medal!") medals["bronze"] += 1 while True: - gates = ask_int("HOW MANY GATES DOES THIS COURSE HAVE (1 TO 25)") + gates = ask_int("How many gates does this course have (1 to 25)") if gates < 1: - print("TRY AGAIN,") + print("Try again,") else: if(gates > 25): - print("25 IS THE LIMIT.") + print("25 is the limit.") break pre_run() while True: - lvl = ask_int("RATE YOURSELF AS A SKIER, (1=WORST, 3=BEST)") + lvl = ask_int("Rate yourself as a skier, (1=Worst, 3=Best)") if lvl < 1 or lvl > 3: - print("THE BOUNDS ARE 1-3.") + print("The bounds are 1-3.") else: break while True: run() while True: - answer = ask("DO YOU WANT TO PLAY AGAIN?") + answer = ask("Do you want to play again?") if answer == "YES" or answer == "NO": break else: - print("PLEASE TYPE 'YES' OR 'NO'") + print("Please type \"YES\" or \"NO\"") if answer == "NO": break -print("THANKS FOR THE RACE") +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']}") From 10af3318d66d7314c0b49023447f0970b716a140 Mon Sep 17 00:00:00 2001 From: Gabry Date: Thu, 24 Feb 2022 19:35:40 +0100 Subject: [PATCH 3/3] Formatted the output texts to follow a more modern style --- 79_Slalom/python/slalom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/79_Slalom/python/slalom.py b/79_Slalom/python/slalom.py index 44ce0fde..a764e84a 100644 --- a/79_Slalom/python/slalom.py +++ b/79_Slalom/python/slalom.py @@ -76,7 +76,7 @@ def run(): print(f"You took {int(time + random())} seconds.") return else: - print("You made it!") + print("You made it!") time += 1.5 else: match opt: