I thought I’d follow up on my post about using a “Deal or No Deal”-type game to teach math. If I were starting off with beginners to Python, I probably wouldn’t use so much functional syntax, but I thought I better show some code.

# "Deal or no deal"-isomorphic game
import math
values = [0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000]
class Possibilities(object):
def __init__(self, possibilities=values):
self.possibilities=possibilities
def E(self, fcn):
return sum(list(map(fcn, self.possibilities)))/len(self.possibilities)
def mean(self):
return self.E(lambda x: x)
def standard_deviation(self):
return math.sqrt(self.E(lambda x: x*x)-self.mean()**2)
def population_deviation(self):
n=len(self.possibilities)
return self.standard_deviation()*math.sqrt(n/(n-1))
def beat(self, val):
n=len(self.possibilities)
m=len(list(filter(lambda x: x>=val, self.possibilities)))
return m/n
def remove(self, n):
self.possibilities.remove(n)
def __contains__(self, n):
return n in self.possibilities
def __str__(self):
return 'The '+str(len(self.possibilities))+' possibilities are: '+str(self.possibilities)+'\n mean='+str(self.mean())+' ; SD='+str(self.standard_deviation())+'\n P(beating mean)='+str(self.beat(self.mean()))
if __name__=='__main__':
x=Possibilities()
while(len(x.possibilities)>1):
print('')
print(x)
inp = input('What value was revealed? ')
float_val = float(inp)
if float_val in x:
x.remove(float_val)
else:
print(float_val, 'is not a possible value.')
print('You win', x.possibilities[0])