Hey guys, I made a class that I am using to save numbers for a larger Ping Pong stat tracker program I am making. I am having problems with it however and can't figure out how to solve it. The class is a basic save to file program. It takes the numbers from the pre-made file (all four numbers start at 0.0, each on a new line) and applies them to a certain variable . However, if I write to the file twice without changing a certain variable (self.__aa, self.__bb, self.__cc or self.__dd) then it creates a new line below the number in the file. I am not sure why this is, I think it may have something to do with the fact that the part of the program which is suppose to clear the new line is in the __init__ part of the class.

The program has been revised a bunch and isn't written very well. I'll show you what is giving me trouble below:

The file looks like:

0.0
0.0
0.0
0.0

The first 0 is for self.__aa, the second for self.__bb, etc.

Basically, if you do:

John = Player("John","Doe")
John.updateWins() #self.__aa
John.updatePointsWon(1)#self.__cc
John.updatePointsLost(1)#self.__dd
John.saveAll()

then:

John.updateWins() #self.__aa
John.updatePointsWon(1)#self.__bb
John.updatePointsLost(1)#self.__cc
John.saveAll()

the file will look like this:

2.0
0.0

2.0
2.0

This then creates a problem with reading the numbers. Something is up with the new line, not sure if i'm not deleting it right or what but if someone could give me some advice that'd be great.

class Player:
    
    def __init__(self, first, last):
        self.__first = first
        self.__last = last
        self.__savefile = open(first+".py", 'r')
        self.__aa = self.__savefile.readline()
        self.__aa = float(self.__aa[:-1])
        self.__bb = self.__savefile.readline()
        self.__bb = float(self.__bb[:-1])
        self.__cc = self.__savefile.readline()
        self.__cc = float(self.__cc[:-1])
        self.__dd = self.__savefile.readline()
        self.__dd = float(self.__dd[:-1])
        self.__wins = self.__aa
        self.__losses = self.__bb
        self.__games = self.__aa + self.__bb
        self.__pointswon = self.__cc
        self.__pointslost = self.__dd

    def getWins(self):
        return self.__aa

    def updateWins(self):
        self.__aa = float(self.__aa) + 1.0

    def getLosses(self):
        return self.__bb

    def updateLosses(self):
        self.__bb = float(self.__bb) + 1.0

    def getGames(self):
        return self.__aa + self.__bb

    def getPointsWon(self):
        return self.__cc

    def updatePointsWon(self,x):
        self.__cc = float(self.__cc) + x

    def getPointsLost(self):
        return self.__dd

    def updatePointsLost(self, x):
        self.__dd = float(self.__dd) + x

    def getWL(self):
        try:
            return self.__aa / self.__bb
        except ZeroDivisionError:
            return "100%"
            
    def getPointRatio(self):
        try:
            return self.__cc / self.__dd
        except ZeroDivisionError:
            return "100%"

    def getWinPercent(self):
        return self.__aa / (self.__aa + self.__bb) 
        

    def saveAll(self):
        outfile = open(self.__first+".py",'w')
        self.__aa = str(self.__aa)
        self.__aa = self.__aa + "\n"
        self.__bb = str(self.__bb)
        self.__bb = self.__bb + "\n"
        self.__cc = str(self.__cc)
        self.__cc = self.__cc + "\n"
        self.__dd = str(self.__dd)
        self.__dd = self.__dd + "\n"
        outfile.write(self.__aa)
        outfile.write(self.__bb)
        outfile.write(self.__cc)
        outfile.write(self.__dd)
        outfile.close()

Err, oops, in my example of what's wrong I mean to make it:

Basically, if you do:

John = Player("John","Doe")
John.updateWins() #self.__aa
John.updatePointsWon(1)#self.__cc
John.updatePointsLost(1)#self.__dd
John.saveAll()

then:

John.updateWins() #self.__aa
John.updatePointsWon(1)#self.__cc
John.updatePointsLost(1)#self.__dd
John.saveAll()

the file will look like this:

2.0
0.0

2.0
2.0

The point being, that is you do not update something twice in a row it adds a new line below.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.