* Jason <[EMAIL PROTECTED]> [2005-09-19 16:28]: > I've restructured my code with the assistance of George and Mike which > is now as follows... > > import random > > class HiScores: > def __init__(self,hiScores): > self.hiScores=[(entry[:5],entry[5:]) for entry in hiScores]
With your redefined hiScores, the above is wrong. I think it should just be: self.hiScores=[entry for entry in hiScores] Your original code used the slicing to pull the score and name out of a single string. Since you've split the string into its two parts, you don't need the indexing anymore. > def showScores(self): > for name,score in self.hiScores: > print "%s - %s" % name,score The error you cite below is due to trying to zfill an integer, not a string. I'm not going to modify your code, but I would use integers all over for the scores, and only turn to a string (and do the zfill) when outputting it. def showScores(self): for name,score in self.hiScores: score = str(score).zfill(5) #untested print "%s - %s" % name,score > def main(): > > hiScores=[('10000','Alpha'),('07500','Beta'),('05000','Gamma'),('02500','Delta'),('00000','Epsilon')] > This looks like an indentation error to me. Is hiScores indented in your version? > a=HiScores(hiScores) > print "Original Scores\n---------------" > a.showScores() > > while 1: > newScore=random.randint(0,10000) As I said, I would use int's throughout, but to use it as-is, change the above line to: newScore=str(random.randint(0,10000)) > if newScore.zfill(5) > a.lastScore(): > print "Congratulations, you scored %d " % newScore > name=raw_input("Please enter your name :") > a.addScore(newScore,name) > a.showScores() > > if __name__=="__main__": > main() <snip> > 1) The most important is that when run, the program crashes with > > AttributeError: 'int' object has no attribute 'zfill' > > I've read as many websites as I can about zfill and I can't see why on > earth it's failing. I think I explained this above. You're trying to call a string method on an integer. > > 2) The output of the predefined hiscores is now... > > 10000 - Alpha () > 07500 - Beta () > 05000 - Gamma () > 02500 - Delta () > 00000 - Epsilon () Are you sure it's not: ('10000', 'Alpha') - () etc. ? I think this is a result of your still doing indexing to separate the score and the name, even though you've already separated the name and score into tuples in the predefined list: >>> hiScores=[('10000','Alpha'),('07500','Beta'),('05000','Gamma'),('02500','Delta'),('00000','Epsilon')] >>> s=hiScores[0] >>> s ('10000', 'Alpha') >>> s[:5] ('10000', 'Alpha') >>> s[5:] () >>> p = (s[:5],s[5:]) >>> p (('10000', 'Alpha'), ()) >>> print "%s - %s" % p ('10000', 'Alpha') - () >>> > Why are there the pairing parenthesis there? George very kindly showed > me another way which was to have... > > def showScores(self): > for entry in self.hiScores: > print entry[0:5]," - ",entry[5:] > > But using that method output the entire list in it's full format (sorry > if that's not the correct terminology). But give me a small plus mark > for changing code and not simply copying George :) > > 3) The hardest thing to 'understand' is the line... > self.hiScores=[(entry[:5],entry[5:]) for entry in hiScores] This is now slicing into the tuple for each entry, instead of into the string, so your results are unexpected. slicing past the end of a tuple returns the empty tuple (which I think is the '()' you're getting in your output. > TIA HTH- John -- http://mail.python.org/mailman/listinfo/python-list