From 0645ab1e30b458fb3e6881a980b5680a267a90f6 Mon Sep 17 00:00:00 2001 From: Dave LeCompte Date: Sat, 27 Feb 2021 18:53:06 -0800 Subject: [PATCH 1/2] Port KINEMA to Python Pulled "g", the gravitational constant, out as a constant, rewrote the equations to be a little clearer, closer to how a high school student would recognize them from first semester Physics class. --- 52 Kinema/python/kinema.py | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 52 Kinema/python/kinema.py diff --git a/52 Kinema/python/kinema.py b/52 Kinema/python/kinema.py new file mode 100644 index 00000000..55d20aa1 --- /dev/null +++ b/52 Kinema/python/kinema.py @@ -0,0 +1,82 @@ +""" +KINEMA + +A kinematics physics quiz. + +Ported by Dave LeCompte +""" + +import random + +# We approximate gravity from 9.8 meters/second squared to 10, which +# is only off by about 2%. 10 is also a lot easier for people to use +# for mental math. + +g = 10 + + +def print_with_tab(spaces_count, msg): + if spaces_count > 0: + spaces = " " * spaces_count + else: + spaces = "" + print(spaces + msg) + + +def do_quiz(): + print() + print() + num_questions_correct = 0 + + # pick random initial velocity + v0 = random.randint(5, 40) + print(f"A BALL IS THROWN UPWARDS AT {v0} METERS PER SECOND.") + print() + + answer = v0 ** 2 / (2 * g) + num_questions_correct += ask_player("HOW HIGH WILL IT GO (IN METERS)?", answer) + + answer = 2 * v0 / g + num_questions_correct += ask_player( + "HOW LONG UNTIL IT RETURNS (IN SECONDS)?", answer + ) + + t = 1 + random.randint(0, 2 * v0) // g + answer = v0 - g * t + num_questions_correct += ask_player( + f"WHAT WILL ITS VELOCITY BE AFTER {t} SECONDS?", answer + ) + + print() + print(f"{num_questions_correct} right out of 3.") + if num_questions_correct >= 2: + print(" NOT BAD.") + + +def ask_player(question, answer): + print(question) + player_answer = float(input()) + if abs((player_answer - answer) / answer) < 0.15: + print("CLOSE ENOUGH.") + score = 1 + else: + print("NOT EVEN CLOSE....") + score = 0 + print(f"CORRECT ANSWER IS {answer}") + print() + return score + + +def main(): + print_with_tab(33, "KINEMA") + print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print() + print() + print() + + while True: + do_quiz() + + +if __name__ == "__main__": + main() From e73c7c2bbf5db6253a787fe63917803d4d60d0d4 Mon Sep 17 00:00:00 2001 From: Dave LeCompte Date: Sat, 27 Feb 2021 18:55:29 -0800 Subject: [PATCH 2/2] Kinema tweak Moved the accuracy window constant to the top of the file with a comment. --- 52 Kinema/python/kinema.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/52 Kinema/python/kinema.py b/52 Kinema/python/kinema.py index 55d20aa1..e5529d68 100644 --- a/52 Kinema/python/kinema.py +++ b/52 Kinema/python/kinema.py @@ -14,6 +14,11 @@ import random g = 10 +# We only expect the student to get within this percentage of the +# correct answer. This isn't rocket science. + +EXPECTED_ACCURACY_PERCENT = 15 + def print_with_tab(spaces_count, msg): if spaces_count > 0: @@ -56,7 +61,9 @@ def do_quiz(): def ask_player(question, answer): print(question) player_answer = float(input()) - if abs((player_answer - answer) / answer) < 0.15: + + accuracy_frac = EXPECTED_ACCURACY_PERCENT / 100.0 + if abs((player_answer - answer) / answer) < accuracy_frac: print("CLOSE ENOUGH.") score = 1 else: