Struggling with basics



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)


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.

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

.



Relevant Pages

  • Re: Rich Comparisons Gotcha
    ... Note how the presence of c in the list makes it behave wrong for 3 as ... When I'm comparing ... Numeric developers and they have been part of Python ... == op return an object that wraps the array of bools (instead of the array ...
    (comp.lang.python)
  • Re: SOLVED: How do I scope a variable if the variable name contains a variable?
    ... Either I have it right but am expressing it poorly, ... so I wonder if my understanding is all wrong. ... table so you can't get access to them via symrefs. ... DF> You suggested using a hash INSTEAD of an array, ...
    (comp.lang.perl.misc)
  • Need help understanding an Array push
    ... complex script that generates some reports based on some text input ... Need help understanding what this section of Perl code does: ... new arrangement of the data pulled out of the other array. ...
    (comp.lang.perl.misc)
  • RE: Array of datarows
    ... Hi Brian, ... I would like to confirm my understanding of your issue. ... your description, I understand that when binding to an array of DataRows, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Formula logic explaination
    ... In the Countif help it shows Countif, you ... I'm not understanding how the array returned by the ...
    (microsoft.public.excel.programming)