[Tutor] Cricket predictor
David Holland
davholla2002 at yahoo.co.uk
Sat Oct 16 17:37:32 CEST 2004
Here is the code for the whole programme
#predicts the result of a limited over cricket match
based on current score
#aim to modify it so that everytime it is run it
looks at the last time and
#takes in account the changes
from Tkinter import *
class Cricket:
def numberofballs(y):
"""Convert overs to number of balls so far"""
y = float(y)
b = int(y)
numberofballsinthisover = (y-b)*10
numberofballsbefore = b*6
totalnumberofballs = numberofballsinthisover +
numberofballsbefore
return totalnumberofballs
def scoreperball(x,y):
"""Runs per ball"""
#x is the score
x = float(x)
y = float(x)
runsperball = x/y
return runsperball
def ifnotout(y,z):
#y score per ball
#z number of overs in game
z = z * 6
a = y * z
a = int(a)
return a
def ifallout(x,w):
#predicts score if they will not last z overs
#x is the score per ball
#w is number of balls to get them out
b = x * w
b = int(b)
return b
def willsurvive(w,y,z, wold, yold):
#will they survive to z overs
#w number of wickets so far
#y how many balls so far
w = float(w)
y = float(y)
#how many balls to get a wicket
wlost = y/w
#numberofballs to get them allout if this the
first time we look at all 10 wickets
#the second just the number of wickets that
were running that time
allout = ((10 - wold) * wlost) + yold
numballsingame = z * 6
if allout < numballsingame:
alloutearly = allout
else:
alloutearly = 0
return alloutearly
#create a function to run all functions
def runfuns(self, w,y,x,z, win, originalscore,
orignoovers, wold, yold):
"""This is to get all functions to run"""
numberofovers = orignoovers
y = numberofballs(y)
runsperball = scoreperball(x,y)
#see if they will survive z overs
alloutearly = willsurvive(w,y,z, wold, yold)
if alloutearly == 0:
a = ifnotout(runsperball,z)
resulttext = "Predicted score is", a +
originalscore
else:
b = ifallout(runsperball,alloutearly)
resulttext = "Predicted score is all out
for ", b + originalscore
#create a number of number of wickets now + 5
numberofoversnexttime = numberofovers +5
resulttext = resulttext + "Please wait until",
numberofoversnexttime, "overs have been played before
using this again"
return resulttext
print "Line 85"
def savecurrentstate(x,y,w):
listcurrentstate = [x,y,w]
#save x score, y number of overs, w number of
wickets
pickle_file = open("cricket1.dat","w")
cPickle.dump(listcurrentstate, pickle_file)
pickle_file.close()
def openlasttime():
"""Get the state of play last time"""
try:
pickle_file = open("cricket1.dat", "r")
#print "line 86 okay"
laststateofplay =
cPickle.load(pickle_file)
#print "line 88 okay"
except:
print "I am sorry but I could not open
this file so not using the old values"
laststateofplay = [0,0,0]
xold = laststateofplay[0]
#print "line 90 okay "
yold = laststateofplay[1]
#print "line 92 okay"
wold = laststateofplay[2]
#print "line 94 okay"
return xold, yold, wold
class CricketGUI(Frame):
"""GUI to enter the info for the cricket class"""
def __init__(self, master):
"""Initiliaze Frame."""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
#create runs label
self.runs_lbl = Label(self, text = "Enter the
number of runs")
self.runs_lbl.grid(row = 0, column = 0,
columnspan = 2, sticky = W)
#create overs label
self.overs_lbl = Label(self, text = "Enter the
number of overs")
self.overs_lbl.grid(row = 1, column = 0,
columnspan = 2, sticky = W)
#create wickets label
self.wickets_lbl = Label(self, text = "Enter
the number of wickets")
self.wickets_lbl.grid(row = 2, column = 0,
columnspan = 2, sticky = W)
#create results label
self.result_lbl = Label(self, text = "The
result is")
self.result_lbl.grid(row = 5, column = 0,
columnspan = 2, sticky = W)
#create entry to put in number of runs
self.runs_ent = Entry(self)
self.runs_ent.grid(row=0, column = 1,
columnspan = 2, sticky = W)
#create entry to put in number of overs
self.overs_ent = Entry(self)
self.overs_ent.grid(row=1, column = 1,
columnspan = 2, sticky = W)
#create entry to put in number of wickets
self.wickets_ent = Entry(self)
self.wickets_ent.grid(row=2, column = 1,
columnspan = 2, sticky = W)
#create checkbutton to see if he they have
done it before for this game
self.yes_no = BooleanVar()
Checkbutton(self, text ="Have you used this
before for this game, click if yes otherwise leave
blank",
variable = self.yes_no).grid(row =
3, column = 0, sticky = W)
#need to create a submit button
Button(self, text = "Click for result",
command = self.cricket_getinfo).grid(row = 4, column =
0, columnspan = 4)
#show results
self.results_txt = Text(self, width = 50,
height = 10, wrap = WORD)
self.results_txt.grid(row = 5, column = 0,
columnspan = 4)
def cricket_getinfo(self):
"""Get values from the GUI and submit for
calculation"""
print "test"
runs = self.runs_ent.get()
wickets = self.wickets_ent.get()
overs = self.overs_ent.get()
if self.yes_no.get():
used_before = 'Y'
else:
used_before = 'N'
Cricketobj = Cricket()
#create code to call the calculations
z = 50
win = 10
if self.yes_no == 'N':
win = 10
originalscore = 0
wold = 0
yold = 0
results =
Cricketobj.runfuns(wickets,overs,runs,z, win,
originalscore, orignovoers, wold, yold)
print results
elif self.yes_no == 'Y':
#get the data from last time
xold, yold, wold = openlasttime()
#now we are calculating using the more up to
date run rate
orignovoers = y
runs = int(runs)
xold = int(xold)
x = x - xold
overs = int(overs) - int(yold)
wickets = int(wickets) - int(wold)
wold = int(wold)
win = 10
z = 50
results =
Cricketobj.runfuns(wickets,overs,runs,z, win,
originalscore, orignovoers, wold, yold)#(w,y,x,z,win,
xold, orignovoers, wold, yold)
#the line below is causing it crash
#results = 1
self.results_txt.insert(0.0,results)
Cricketobj.savecurrentstate(runs,overs,wickets)
#main
import cPickle, shelve
from Tkinter import *
root = Tk()
root.title("Cricket Results")
app = CricketGUI(root)
root.mainloop()
--- tutor-request at python.org wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web,
> visit
> http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it
> is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
> 1. Re: Find out if a number is even or not (Alan
> Gauld)
> 2. Re: Find out if a number is even or not (Liam
> Clarke)
>
>
>
----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 16 Oct 2004 08:47:12 +0100
> From: "Alan Gauld" <alan.gauld at freenet.co.uk>
> Subject: Re: [Tutor] Find out if a number is even or
> not
> To: <cyresse at gmail.com>, <cynos at safe-mail.net>,
> <tutor at python.org>
> Message-ID: <00a601c4b354$5979b630$d79b8851 at xp>
> Content-Type: text/plain; charset="iso-8859-1"
>
>
> > I don't quite understand the syntax of
> >
> > return x % 2 and 'Even' or 'Odd'...
>
> This is fully exlained in my tutorial on the
> Functional Programming
> topic, but the short answer is:
>
> x % 2 and 'Even' or 'Odd'...
>
> Can be written:
>
> ((x%2) and 'Even') or 'Odd')
>
> This is a boolean expression.
>
> Python works out the first term, x%2 and of it is
> true looks at
> the second term in the AND expression, 'Even'. Since
> 'Even' is true
> (only empty strings are considered false) the whole
> AND expression
> is True and because it is True Python doesn't need
> to evaluate the
> second part of the OR expression so it returns the
> True part of the
> AND expression.
>
> And here is the trick. Python doesn't return an
> actual boolean value
> (True or False), instead it returns the actual value
> it last tested,
> in this case the string 'Even'.
>
> If x%2 is 0 and thus False in boolean terms the
> whole AND expression
> must be false. So Python now evaluates the second
> part of the OR
> expression, 'Odd'. Again this string is True
> boolean-wise, so Python
> returns the last True value, namely the string
> 'Odd'.
>
> So the actual return value from the overall
> expression is 'Even'
> if x%2 is true and 'Odd' if x%2 is False.
>
> This is not intuitively obvious but the tutorial
> page gives several
> more examples to illustrate how it works. In this
> case I thing the
> functional form works quite well because it kind of
> makes sense
> (to me anyway!) to say the function returns 'Even'
> or 'Odd'
>
> > I'm having trouble understanding conditionals,
> like... if x: So if
> x
> > is... what exactly? Is it checking for a non-null
> or non-zero value?
> > Or if not x:....
>
> if x:
>
> is siply shorthand for saying
>
> if x == True:
>
> And the logical equivalent to True depends on the
> data type.
> Normally it is whatever comes closest in meaning to
> not
> being 'empty'. Thus for numbers it means not being
> zero,
> for sequences it really is not being empty, for
> files it
> means the file is open and readable - ie we aren't
> at the end.
>
> There is a page in the Python documentation that
> decribes
> for each type the boolean equivalents. I think its
> in the
> reference manual.
>
> HTH,
>
> Alan G.
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 16 Oct 2004 21:47:49 +1300
> From: Liam Clarke <cyresse at gmail.com>
> Subject: Re: [Tutor] Find out if a number is even or
> not
> To: Alan Gauld <alan.gauld at freenet.co.uk>
> Cc: tutor at python.org, cynos at safe-mail.net
> Message-ID:
> <f2ff2d04101601473ce1cb41 at mail.gmail.com>
> Content-Type: text/plain; charset=US-ASCII
>
> Ah sorry, I haven't got to the functional
> programming bit yet. <embarrassed>
>
> So, that would mean 'if not x' is shorthand for 'if
> x is False' or 'if x == 0'
>
> so, any expression, say, if a - 10: if a - 10
> doesn't equal zero,
> then it's true.
>
> and the x % 2 and 'Even' or 'Odd' thing is nifty..
>
> That shines a ray of light on it for me,
>
> Thanks.
>
>
> On Sat, 16 Oct 2004 08:47:12 +0100, Alan Gauld
> <alan.gauld at freenet.co.uk> wrote:
> >
> > > I don't quite understand the syntax of
> > >
> > > return x % 2 and 'Even' or 'Odd'...
> >
> > This is fully exlained in my tutorial on the
> Functional Programming
> > topic, but the short answer is:
> >
> > x % 2 and 'Even' or 'Odd'...
> >
> > Can be written:
> >
> > ((x%2) and 'Even') or 'Odd')
> >
> > This is a boolean expression.
> >
> > Python works out the first term, x%2 and of it is
> true looks at
> > the second term in the AND expression, 'Even'.
> Since 'Even' is true
> > (only empty strings are considered false) the
> whole AND expression
> > is True and because it is True Python doesn't need
> to evaluate the
> > second part of the OR expression so it returns the
> True part of the
> > AND expression.
> >
> > And here is the trick. Python doesn't return an
> actual boolean value
> > (True or False), instead it returns the actual
> value it last tested,
> > in this case the string 'Even'.
> >
> > If x%2 is 0 and thus False in boolean terms the
> whole AND expression
> > must be false. So Python now evaluates the second
> part
=== message truncated ===
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
More information about the Tutor
mailing list