Skip to content

Instantly share code, notes, and snippets.

@juicedM3
Created November 10, 2021 02:27
Show Gist options
  • Save juicedM3/1811e98e173f85c95b9a144430de6cff to your computer and use it in GitHub Desktop.
Save juicedM3/1811e98e173f85c95b9a144430de6cff to your computer and use it in GitHub Desktop.
Taking my Ruby version and writing it in Python.
#!/usr/bin/env python
from random import choice
#
# Originally written in Ruby as a math learning tool for my 6 year old son. It
# is called 'DiceMath' because I wanted to add the visual for dice to help him
# count. I never got that far with adding images but I kept the name since I
# still think of it as rolling dice to select 2 random numbers.
#
# He's currently working on his multiplication up to 12. They work on 2 numbers
# at a time. So 4 or 8 times 1 through 12.
#
# Written for Python 2.7.16 because that's MacOS default.
#
class DiceMath(object):
# First number, second number and answer.
# Or think of it as dice1, dice2.
d1, d2, answer = int(), int(), int()
# String char of operator that will be performed.
operator = '+'
# This is the high end of the range of numbers we will be working with.
max = int()
def __init__(self, m=6):
self.max = m
self.roll()
def roll(self):
# self.d1 = choice(range(1, self.max+1))
# ar = [3,6]
ar = [4, 8]
self.d1 = choice(ar)
self.d2 = choice(range(1, self.max+1))
self.perform_arithmetic()
def perform_arithmetic(self):
raise "Must be implemented in child class."
def equation(self):
return "{0} {1} {2} = ".format(self.d1, self.operator, self.d2)
def is_answer_correct(self, ans):
return self.answer == ans
# Factory method to create correct object.
@classmethod
def f(self, operation, m):
return {
"subtract": DiceMathSubtract(m),
"multiply": DiceMathMultiply(m),
}.get(operation, DiceMathAdd(m))
class DiceMathAdd(DiceMath):
def __init__(self, m):
self.operator = "+"
super(DiceMathAdd, self).__init__(m)
def perform_arithmetic(self):
self.answer = self.d1 + self.d2
class DiceMathMultiply(DiceMath):
def __init__(self, m):
self.operator = "*"
super(DiceMathMultiply, self).__init__(m)
def perform_arithmetic(self):
self.answer = self.d1 * self.d2
class DiceMathSubtract(DiceMath):
def __init__(self, m):
self.operator = "-"
super(DiceMathSubtract, self).__init__(m)
def perform_arithmetic(self):
self.__switch_if_larger()
self.answer = self.d1 - self.d2
# No negative numbers.
def __switch_if_larger(self):
if self.d2 > self.d1:
tmp = self.d1
self.d1 = self.d2
self.d2 = tmp
# Just hitting return w/o any input exits the script.
def ask(d):
try:
user_answer = input("{}".format(d.equation())) # .rstrip()
return user_answer
except SyntaxError:
print("Exiting.")
exit(0)
dice = DiceMath.f('multiply', 12)
# dice = DiceMath.f("subtract",20)
# dice = DiceMath.f("addition", 10)
while True:
user_answer = ask(dice)
if dice.is_answer_correct(user_answer):
print("Correct!")
dice.roll()
else:
print("Incorrect.")
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment