Highscore

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MaxLindquist
    New Member
    • Oct 2006
    • 24

    #16
    Thank you.... now it works prefectly. I inserted some extra stuff:

    Score, Name, Times Played, Total time played.

    I have now only one last question:

    When i print this out, how do I make this look good? Now it prints out this:

    (Total Score, Name, Times Played, Median Value, Total time played (sec))
    (300, 'John')
    (200, 'Jane')
    (106, 'Bajs', 1, 106, 9)
    (102, 'Hej', 1, 102, 9)
    (83, 'test', 2, 41, 48)
    (68.70051319954 6208, 'Testny', 1, 68, 14.555932021867 418)
    (66, 'Runk', 1, 66, 15)
    (58, 'test', 1, 58, 16)
    (53, 'Test4', 1, 53, 18)


    Code:
    But i would like it to print out this way:
    
    (Total Score, Name,    Times Played, Median Value, Total time played (sec))
    (300             'John'      2                    142                 114)
    (200,            'Jane'      1                    200                 17)
    (106,            'Bajs',     1,                   106,                 9)
    (102,            'Hej',       1,                   102,                 9)
    (83,              'test',      2,                   41,                  48)
    (68.              'Testny',  1,                   68,                  14)
    (66,              'Runk',    1,                   66,                  15)
    (58,              'test',      1,                   58,                  16)
    (53,              'Test4',    1,                   53,                  18)
    (I did'nt know how to wright it this way here, so i used the CODE-thing. I hope you understand what i mean)

    This is the code i have:

    Code:
    def highscore(totalscore, name, timesplayed, median, totaltime):
    
        scores = open("scores", "r")
        som blir en lista
        hiscores = pickle.load(scores)
        scores.close()
        
    
        hiscores.append((totalscore, name, timesplayed, median, totaltime))
    
        hiscores.sort    
    
        scores=open("scores", "w")
        pickle.dump(hiscores[-10:], scores)
        scores.close()
    
    
    def highscore2():
        scores = open("scores", "r")
        oldscores = pickle.load(scores)
        scores.close()
    
        print "(Total Score, Name,    Times Played, Median Value, Total time played (sec))"
    
        for i in range(9, 0, -1):
            print oldscores[i]
    Thanks

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #17
      Originally posted by MaxLindquist
      Thank you.... now it works prefectly. I inserted some extra stuff:

      Score, Name, Times Played, Total time played.

      I have now only one last question:

      When i print this out, how do I make this look good? Now it prints out this:

      (Total Score, Name, Times Played, Median Value, Total time played (sec))
      (300, 'John')
      (200, 'Jane')
      (106, 'Bajs', 1, 106, 9)
      (102, 'Hej', 1, 102, 9)
      (83, 'test', 2, 41, 48)
      (68.70051319954 6208, 'Testny', 1, 68, 14.555932021867 418)
      (66, 'Runk', 1, 66, 15)
      (58, 'test', 1, 58, 16)
      (53, 'Test4', 1, 53, 18)


      Code:
      But i would like it to print out this way:
      
      (Total Score, Name,    Times Played, Median Value, Total time played (sec))
      (300             'John'      2                    142                 114)
      (200,            'Jane'      1                    200                 17)
      (106,            'Bajs',     1,                   106,                 9)
      (102,            'Hej',       1,                   102,                 9)
      (83,              'test',      2,                   41,                  48)
      (68.              'Testny',  1,                   68,                  14)
      (66,              'Runk',    1,                   66,                  15)
      (58,              'test',      1,                   58,                  16)
      (53,              'Test4',    1,                   53,                  18)
      (I did'nt know how to wright it this way here, so i used the CODE-thing. I hope you understand what i mean)

      This is the code i have:

      Code:
      def highscore(totalscore, name, timesplayed, median, totaltime):
      
          scores = open("scores", "r")
          som blir en lista
          hiscores = pickle.load(scores)
          scores.close()
          
      
          hiscores.append((totalscore, name, timesplayed, median, totaltime))
      
          hiscores.sort    
      
          scores=open("scores", "w")
          pickle.dump(hiscores[-10:], scores)
          scores.close()
      
      
      def highscore2():
          scores = open("scores", "r")
          oldscores = pickle.load(scores)
          scores.close()
      
          print "(Total Score, Name,    Times Played, Median Value, Total time played (sec))"
      
          for i in range(9, 0, -1):
              print oldscores[i]

      Thanks
      How 'bout this:

      Code:
      column_width = 18
      
      def columnize(word, width):
          nSpaces = width - len(word)
          if nSpaces < 0:
              nSpaces = 0
          return word + (" " * nSpaces)
      
      def highscore2():
          scores = open("scores", "r")
          oldscores = pickle.load(scores)
          scores.close()
      
          header = ('Total Score', 'Name', 'Times Played', 'Median Value', 'Total time played (sec)')
      
          for title in header:
              print columnize(title, column_width),
          print
          for i in range(9, 0, -1):
              for col_value in oldscores[i]:
                  print columnize(col_value, column_width),
              print

      Comment

      • MaxLindquist
        New Member
        • Oct 2006
        • 24

        #18
        Originally posted by bartonc
        How 'bout this:

        Code:
        column_width = 18
        
        def columnize(word, width):
            nSpaces = width - len(word)
            if nSpaces < 0:
                nSpaces = 0
            return word + (" " * nSpaces)
        
        def highscore2():
            scores = open("scores", "r")
            oldscores = pickle.load(scores)
            scores.close()
        
            header = ('Total Score', 'Name', 'Times Played', 'Median Value', 'Total time played (sec)')
        
            for title in header:
                print columnize(title, column_width),
            print
            for i in range(9, 0, -1):
                for col_value in oldscores[i]:
                    print columnize(col_value, column_width),
                print
        It exactly what i had in mind... this doesn't work on my cpu though. Maybe col_value has to be defined or something?

        Comment

        • MaxLindquist
          New Member
          • Oct 2006
          • 24

          #19
          This is the Errormessage:

          Code:
          >>> 
          Total Score        Name               Times Played       Median Value       Total time played (sec)
          
          Traceback (most recent call last):
            File "D:\Python24\Test.py", line 25, in -toplevel-
              highscore2()
            File "D:\Python24\Test.py", line 22, in highscore2
              print columnize(col_value, column_width),
            File "D:\Python24\Test.py", line 5, in columnize
              nSpaces = width - len(word)
          TypeError: len() of unsized object

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #20
            Originally posted by MaxLindquist
            This is the Errormessage:

            Code:
            >>> 
            Total Score        Name               Times Played       Median Value       Total time played (sec)
            
            Traceback (most recent call last):
              File "D:\Python24\Test.py", line 25, in -toplevel-
                highscore2()
              File "D:\Python24\Test.py", line 22, in highscore2
                print columnize(col_value, column_width),
              File "D:\Python24\Test.py", line 5, in columnize
                nSpaces = width - len(word)
            TypeError: len() of unsized object
            len(int) returns an error.
            This works:
            Code:
            def columnize(word, width):
                nSpaces = width - len(word)
                if nSpaces < 0:
                    nSpaces = 0
                return word + (" " * nSpaces)
            
            def highscore2():
                column_width = 18
                """
                scores = open("scores", "r")
                oldscores = pickle.load(scores)
                scores.close()
                """    
            
                oldscores = [(300, 'John', 1, 300, 12), (200, 'Jane', 200, 3, 4), (106, 'Bajs', 1, 106, 9), (102, 'Hej', 1, 102, 9), (83, 'test', 2, 41, 48), \
                              (68.700513199546208, 'Testny', 1, 68, 14.555932021867418), (66, 'Runk', 1, 66, 15), (58, 'test', 1, 58, 16), \
                              (53, 'Test4', 1, 53, 18)]
            
            
                header = ('Total Score', 'Name', 'Times Played', 'Median Value', 'Total time played (sec)')
            
                for title in header:
                    print columnize(title, column_width),
                print
                    
                for item in oldscores:
                    for col_value in item:
                        print columnize(str(col_value), column_width),
                    print            
            
            if __name__ == '__main__':
                try:
                    highscore2()
                finally:
                    del highscore2
                    del columnize
            Code:
            Total Score        Name               Times Played       Median Value       Total time played (sec)
            300                John               1                  300                12                
            200                Jane               200                3                  4                 
            106                Bajs               1                  106                9                 
            102                Hej                1                  102                9                 
            83                 test               2                  41                 48                
            68.7005131995      Testny             1                  68                 14.5559320219     
            66                 Runk               1                  66                 15                
            58                 test               1                  58                 16                
            53                 Test4              1                  53                 18

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #21
              Originally posted by MaxLindquist
              This is the Errormessage:

              Code:
              >>> 
              Total Score        Name               Times Played       Median Value       Total time played (sec)
              
              Traceback (most recent call last):
                File "D:\Python24\Test.py", line 25, in -toplevel-
                  highscore2()
                File "D:\Python24\Test.py", line 22, in highscore2
                  print columnize(col_value, column_width),
                File "D:\Python24\Test.py", line 5, in columnize
                  nSpaces = width - len(word)
              TypeError: len() of unsized object
              My bad. Thanks BV.

              Comment

              • MaxLindquist
                New Member
                • Oct 2006
                • 24

                #22
                Originally posted by bvdet
                len(int) returns an error.
                This works:
                Code:
                def columnize(word, width):
                    nSpaces = width - len(word)
                    if nSpaces < 0:
                        nSpaces = 0
                    return word + (" " * nSpaces)
                
                def highscore2():
                    column_width = 18
                    """
                    scores = open("scores", "r")
                    oldscores = pickle.load(scores)
                    scores.close()
                    """    
                
                    oldscores = [(300, 'John', 1, 300, 12), (200, 'Jane', 200, 3, 4), (106, 'Bajs', 1, 106, 9), (102, 'Hej', 1, 102, 9), (83, 'test', 2, 41, 48), \
                                  (68.700513199546208, 'Testny', 1, 68, 14.555932021867418), (66, 'Runk', 1, 66, 15), (58, 'test', 1, 58, 16), \
                                  (53, 'Test4', 1, 53, 18)]
                
                
                    header = ('Total Score', 'Name', 'Times Played', 'Median Value', 'Total time played (sec)')
                
                    for title in header:
                        print columnize(title, column_width),
                    print
                        
                    for item in oldscores:
                        for col_value in item:
                            print columnize(str(col_value), column_width),
                        print            
                
                if __name__ == '__main__':
                    try:
                        highscore2()
                    finally:
                        del highscore2
                        del columnize
                Code:
                Total Score        Name               Times Played       Median Value       Total time played (sec)
                300                John               1                  300                12                
                200                Jane               200                3                  4                 
                106                Bajs               1                  106                9                 
                102                Hej                1                  102                9                 
                83                 test               2                  41                 48                
                68.7005131995      Testny             1                  68                 14.5559320219     
                66                 Runk               1                  66                 15                
                58                 test               1                  58                 16                
                53                 Test4              1                  53                 18

                But can i add new players to the highscore now.
                Beacause now the highscore is part of the code:
                Code:
                    oldscores = [(300, 'John', 1, 300, 12), (200, 'Jane', 200, 3, 4), (106, 'Bajs', 1, 106, 9), (102, 'Hej', 1, 102, 9), (83, 'test', 2, 41, 48), \
                                  (68.700513199546208, 'Testny', 1, 68, 14.555932021867418), (66, 'Runk', 1, 66, 15), (58, 'test', 1, 58, 16), \
                                  (53, 'Test4', 1, 53, 18)]

                Comment

                • MaxLindquist
                  New Member
                  • Oct 2006
                  • 24

                  #23
                  Okey... i get it now... thank you very much for your help! I truly mean it!

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #24
                    Originally posted by MaxLindquist
                    Okey... i get it now... thank you very much for your help! I truly mean it!
                    You're welcome, Max.

                    Comment

                    • MaxLindquist
                      New Member
                      • Oct 2006
                      • 24

                      #25
                      i Have one last question:

                      What does this part do?
                      Code:
                      if __name__ == '__main__':
                          try:
                              highscore2()
                          finally:
                              del highscore2
                              del columnize
                      what is "del"?

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #26
                        Originally posted by MaxLindquist
                        i Have one last question:

                        What does this part do?
                        Code:
                        if __name__ == '__main__':
                            try:
                                highscore2()
                            finally:
                                del highscore2
                                del columnize
                        what is "del"?
                        It means delete from memory. I never worry about this. Python takes care of cleaning up memory.

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #27
                          Originally posted by bartonc
                          It means delete from memory. I never worry about this. Python takes care of cleaning up memory.
                          It's a habit I have developed which is not necessary. This works fine:
                          Code:
                          if __name__ == '__main__':
                                 highscore2()

                          Comment

                          • MaxLindquist
                            New Member
                            • Oct 2006
                            • 24

                            #28
                            Originally posted by bvdet
                            It's a habit I have developed which is not necessary. This works fine:
                            Code:
                            if __name__ == '__main__':
                                   highscore2()
                            But what does this mean then?

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #29
                              Originally posted by MaxLindquist
                              But what does this mean then?
                              The code I posted could be considered a module. To test the module, you need some means of calling the function 'highscore2'.
                              Quote Python Essential Reference - "Each module defines a variable '__name__' that contains the module name. Programs specified on the command line or entered interactively run inside the '__main__' module."
                              In our case, the function 'highscore' is only executed if run as a script, but not if imported as a module.

                              Comment

                              • bartonc
                                Recognized Expert Expert
                                • Sep 2006
                                • 6478

                                #30
                                Originally posted by MaxLindquist
                                But what does this mean then?
                                Originally posted by bvdet
                                It's a habit I have developed which is not necessary. This works fine:
                                Code:
                                if __name__ == '__main__':
                                       highscore2()
                                I call this "standard module test code".

                                Comment

                                Working...