Struggling with basics

Peter peter at commonlawgov.org
Sun Sep 25 17:29:54 EDT 2005


Peter wrote:

> Peter wrote:
>
>> Jason wrote:
>>
>>  
>>
>>> A week ago I posted a simple little hi-score routine that I was 
>>> using to learn Python.
>>>
>>> I've only just managed to examine the code, and the responses that 
>>> people gave, and I'm now seriously struggling to understand why 
>>> things aren't working correctly.
>>>
>>> At present my code is as follows...
>>>
>>> import random
>>> import bisect
>>>
>>> class HiScores:
>>>    def __init__(self,hiScores):
>>>        self.hiScores=[entry for entry in hiScores]
>>>
>>>    def showScores(self):
>>>        for score,name in self.hiScores:
>>>            score=str(score).zfill(5)
>>>            print "%s - %s" % name,score
>>>
>>>
>>>    def addScore(self,score,name):
>>>        score.zfill(5)
>>>        bisect.insort(self.hiScores,(score,name))
>>>        if len(self.hiScores)==6:
>>>            self.hiScores.pop()
>>>
>>>    def lastScore(self):
>>>        return self.hiScores[-1][0]
>>>
>>> def main():
>>>
>>> hiScores=[('10000','Alpha'),('07500','Beta'),('05000','Gamma'),('02500','Delta'),('00000','Epsilon')] 
>>>
>>>    a=HiScores(hiScores)
>>>    print "Original Scores\n---------------"
>>>    a.showScores()
>>>
>>>    while 1:
>>>        newScore=str(random.randint(0,10000))
>>>        if newScore  > a.lastScore():
>>>            print "Congratulations, you scored %s " % newScore
>>>            name=raw_input("Please enter your name :")
>>>            a.addScore(newScore,name)
>>>            a.showScores()
>>>
>>> if __name__=="__main__":
>>>    main()
>>>
>>>
>>> My first problem (lack of understanding of course) is that if I run 
>>> the above, I get an error saying:
>>>
>>>    print "%s - %s" % name,score
>>> TypeError: not enough arguments for format string
>>>
>>>
>>> Now I understand what it's saying, but I don't understand why.
>>>
>>> If I change the code to read:
>>>
>>> print "%s - %n" % name, score (thinking of course that ah-ha, score 
>>> is numeric) then I get the same error.
>>>
>>> The only way for the program to run is to simply have
>>>
>>> print name,score (or print score,name)
>>>
>>>
>>>   
>>
>> This is because 'print' is accepting 'score' as a seperate argument, 
>> not the formatting, as you want it to.
>> Try 'print "%s - %s" % (name, score)'
>>  
>>
>>  
>>
>>> The final part that's simply not working correctly is that the 
>>> entire program isn't sorting the data.
>>>
>>> If I run the program and get a score of, say, 6789, then when I add 
>>> my name, nothing is entered.  I have changed the clause that deletes 
>>> (pops) the last array if the array count is 6 and seen what figures 
>>> are being entered into the array.
>>>
>>> Sure enough they are going in the array, and they are being sorted, 
>>> but they are only being sorted AFTER the 00000 of the initial array 
>>> creation.
>>>
>>> I'm pretty sure it's to do with comparing a string against an 
>>> integer but can't for the life of me see where to force the 
>>> comparrison to check against two integers.
>>>
>>>
>>>
>>>   
>>
>> Humm. This is a harder problem. I will copy this text into JEdit to 
>> highlight the text and see if i cannot find the problem.
>>
>>  
>>
> Correction: I will fix my apearently semi-broaken Python installation 
> wich gives me undefined reference errors from math.h and then see if i 
> cannot fix the problem. -.-
>
Well, My Python2.4 may have a broken math module, but my Python2.3 only 
has a broken cPickle. *Kicks computer*.

Try changing pop() to pop(0) and it should work.
string.pop by default removes the last element appended to the list, you 
want the first, which is the oldest.

>>> Apologies for going over old ground and if I'm not understanding, 
>>> I'm getting there honest ;)
>>>
>>>
>>>
>>>   
>>
>>
>> HTH,
>> Peter
>>
>>  
>>
> Peter
>
>
>
HTH,
Peter



More information about the Python-list mailing list