How am I doing?
Mike Meyer
mwm at mired.org
Mon Sep 19 00:24:30 EDT 2005
Jason <jason at jasonmhirst.co.uk> writes:
> Please don't laugh, this is my FIRST Python script where I haven't
> looked at the manual for help...
>
> import string
> import random
>
> class hiScores:
> hiScores=['10000Alpha','07500Beta','05000Gamma','02500Delta','00000Epsilon']
>
> def showScores(self):
> for entry in self.hiScores:
> print entry[0:5]," - ",entry[5:]
>
> def addScore(self,score,name):
> newScore=string.zfill(score,5)
> self.hiScores.append(newScore+name)
> self.hiScores.sort(reverse=True)
>
> if len(self.hiScores)==6:
> del self.hiScores[-1]
>
> a=hiScores()
> print "Original Scores\n---------------"
> a.showScores()
>
> while 1:
> newScore=random.randint(0,10000)
> if string.zfill(newScore,5)>a.hiScores[4][0:5]:
> print "Congratulations, you scored %d " % newScore
> name=raw_input("Please enter your name :")
> a.addScore(newScore,name)
> a.showScores()
> continue
>
> Anything I could have done differently or any "bad-habits" you think I
> have which could lead to ultimate doom I really appreciate to know.
George already covered a lot of things. I wanted to make a general comment:
The standard idiom is to put all the executable code for a script in a
function, (say "main"), then invoke that function iff your code is run
as a script:
def main():
a = hiScores()
...
if __name__ == "__main__":
main()
That way your program can be used as a module by other applications,
allowing your classes/functions/etc. to be reused by other
applications without having to copy them out of your program.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list