From 8fdbf0470ac5b4efd49da0517dd09c94f702714e Mon Sep 17 00:00:00 2001 From: Darren Jones Date: Sat, 27 Feb 2021 12:27:26 +0000 Subject: [PATCH] Added python version of MATHDICE --- 61 Math Dice/python/mathdice.py | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 61 Math Dice/python/mathdice.py diff --git a/61 Math Dice/python/mathdice.py b/61 Math Dice/python/mathdice.py new file mode 100644 index 00000000..ca84555c --- /dev/null +++ b/61 Math Dice/python/mathdice.py @@ -0,0 +1,79 @@ +from random import randint +print("Math Dice") +print("https://github.com/coding-horror/basic-computer-games") +print() +print("""This program generates images of two dice. +When two dice and an equals sign followed by a question +mark have been printed, type your answer, and hit the ENTER +key. +To conclude the program, type 0. +""") + +def print_dice(n): + + def print_0(): + print("| |") + def print_2(): + print("| * * |") + + print(" ----- ") + + if n in [4,5,6]: + print_2() + elif n in [2,3]: + print("| * |") + else: + print_0() + + if n in [1,3,5]: + print("| * |") + elif n in [2,4]: + print_0() + else: + print_2() + + if n in [4,5,6]: + print_2() + elif n in [2,3]: + print("| * |") + else: + print_0() + + print(" ----- ") + +def main(): + + while True: + d1 = randint(1,6) + d2 = randint(1,6) + guess = 13 + + print_dice(d1) + print(" +") + print_dice(d2) + print(" =") + + tries = 0 + while guess != (d1 + d2) and tries < 2: + if tries == 1: + print("No, count the spots and give another answer.") + try: + guess = int(input()) + except ValueError: + print("That's not a number!") + if guess == 0: + exit() + tries += 1 + + if guess != (d1 + d2): + print(f"No, the answer is {d1 + d2}!") + else: + print("Correct!") + + + print("The dice roll again....") + + + +if __name__ == "__main__": + main() \ No newline at end of file