Skip to content

Instantly share code, notes, and snippets.

@ronsims2
Last active July 6, 2020 20:00
Show Gist options
  • Select an option

  • Save ronsims2/a2398260302b79d56a85 to your computer and use it in GitHub Desktop.

Select an option

Save ronsims2/a2398260302b79d56a85 to your computer and use it in GitHub Desktop.
Boogie is a command line math program for kids, great for the raspberryPi
#Boogie is a command line math program for kids, great for the raspberryPi
import random as r
import math as m
import re
class boogie:
def __init__(self, mathtype = 'ADDITION', level = 'EASY', offset = 0, sets = 10):
self.mathsign = None
self.mathtype = mathtype
self.welcomemsg = 'Welcome to Boogie Math! Looks like you will be doing '
self.score = 0
self.start = 0
self.end = 0
self.current = None
self.answers = []
self.level = level.upper()
self.setlevel(self.level, offset)
self.setmathtype(mathtype)
self.welcomemsg += level + ' ' + self.mathtype + '.'
self.sets = self.generatesets(sets, self.level)
self.numsets = len(self.sets)
self.welcome()
def welcome(self, message = ''):
if message == '':
message = self.welcomemsg
print(message)
# @property {integer} qty - The number of number pairs to generate.
def generatesets(self, qty = 10, level = 'EASY'):
numsets = []
for i in range(qty):
numsets.append(self.getrandomnums())
return numsets
def getrandomnums(self):
randnums = []
num1 = r.randint(self.start, self.end)
num2 = r.randint(self.start, self.end)
if num1 >= num2:
randnums.append(num1)
randnums.append(num2)
else:
randnums.append(num2)
randnums.append(num1)
return randnums
def setlevel(self, level = 'EASY', offset = 0):
level = level.upper()
if level == 'SUPEREASY':
self.start = 0 + offset
self.end = 4
elif level == 'MEDIUM':
self.start = 0 + offset
self.end = 50
elif level == 'HARD':
self.start = 0 + offset
self.end = 100
elif level == 'SUPERHARD':
self.start = 0 + offset
self.end = 500
else:
self.start = 0 + offset
self.end = 10
def checkanswer(self, set, answer):
isright = False
correctanswer = None
if answer.strip() == '':
answer = '99999999999999999999'
try:
numanswer = int(answer)
except Exception as e:
e = None #silently handle if a string is passed in
numanswer = '99999999999999999999'
if self.mathtype.find('SUB') == 0:
correctanswer = set[0] - set[1]
if correctanswer == numanswer:
isright = True
elif self.mathtype.find('MUL') == 0:
correctanswer = set[0] * set[1]
if correctanswer == numanswer:
isright = True
elif self.mathtype.find('DIV') == 0:
if set[1] == 0:#Handles precursor to zero division error
sol = 0
solrem = 0
else:
sol = m.floor(set[0] / set[1])
solrem = set[0] % set[1]
totalsol = str(sol)
if solrem > 0:
totalsol += ' ' + str(solrem)
correctanswer = totalsol
if totalsol == answer:
isright = True
#Addition handled by default
else:
correctanswer = set[0] + set[1]
if correctanswer == numanswer:
isright = True
return (isright, correctanswer)
def play(self):
self.current = 0 #This indicates game state (started)
while self.current < self.numsets:
set = self.sets[self.current]
answer = self.askquestion(set)
result = self.checkanswer(set, answer)
if result[0]:
self.score += 1
print("Correct!")
else:
print("Sorry, the answer is: " + str(result[1]))
self.current += 1
self.answers.append(answer)
score = m.floor((self.score/self.numsets) * 100)
print('Thank you for playing your got ' + str(score) + '% right. ')
return score
def askquestion(self, set):
phrase = "What is " + str(set[0]) + ' ' + self.mathsign + ' ' + str(set[1]) + ' ?'
answer = input(phrase)
return answer
def setmathtype(self, mathtype = 'ADDITION'):
mathtype = mathtype.upper()
if mathtype.find('SUB') == 0:
self.mathtype = 'SUBTRACTION'
self.mathsign = '-'
elif self.mathtype.find('MUL') == 0:
self.mathtype = 'MULTIPLICATION'
self.mathsign = 'x'
elif self.mathtype.find('DIV') == 0:
self.mathtype = 'DIVISION'
self.mathsign = '/'
#Addition is the default
else:
self.mathtype = 'ADDITION'
self.mathsign = '+'
@staticmethod
def replay():
understood = False
while understood == False:
answer = input('Would you like to play again?')
if (answer.strip().upper() == 'YES'):
answer = True
understood = True
elif (answer.strip().upper() == 'NO'):
answer = False
understood = True
else:
print('Please type yes or no.')
return answer
@staticmethod
def askmathtype():
understood = False
mathtype = ''
while understood == False:
print('What kind of math would you like to do?')
mathtype = input('You can type things like: Addition, Subtraction, Multiplication or Division.')
mathtype = mathtype.strip().upper()
if (mathtype.find("ADD") == 0
or mathtype.find("SUB") == 0
or mathtype.find("MUL") == 0
or mathtype.find("DIV") == 0):
understood = True
else:
print('I did not quite understand.')
return mathtype
@staticmethod
def asklevel():
understood = False
level = ''
while understood == False:
print('What level do you want to play?')
level = input('You can type things like: SUPEREASY, EASY, MEDIUM, HARD, SUPERHARD.')
level = level.strip().upper()
if (level == 'SUPEREASY'
or level == 'EASY'
or level == 'MEDIUM'
or level == 'HARD'
or level == 'SUPERHARD'):
understood = True
else:
print('Huh?, I did not quite understand.')
return level
@staticmethod
def asksets():
understood = False
sets = 0
while understood == False:
sets = input('How many problems do you want to do?')
sets = sets.strip()
regex = r'^[0-9]+$'
arenums = re.match(regex, sets)
if arenums != None:
sets = int(sets)
if sets > 0:
understood = True
else:
print('You can think of a bigger number than that...')
else:
print("That doesn't look like a proper number...")
return sets
#Sample usage
replay = True
while replay:
mathtype = boogie.askmathtype()
level = boogie.asklevel()
sets = boogie.asksets()
bm = boogie(mathtype = mathtype, level = level, sets = sets)
bm.play()
replay = boogie.replay()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment