0

I'm currently working on a simple little application that keeps track of wins and losses for any sort of game. I'm storing the win/loss count in separate text files as single numbers. What I want the program to be able to do is look into the specified text file, and simply add 1 to the existing number. For example, if the entire text file is simply "0" and I input "win" into the application, it will perform 0+1 and change the text file permanently to the result. Here's what I've got so far:

ld = open("data/lossdata.txt", "r+")
wd = open("data/windata.txt", "r+")
hlp = open("data/help.txt", "r+")
losread = ld.read()
winread = wd.read()
helpread = hlp.read()
to_write = []

print("Welcome to Track Lad. For help, input \"help\"\nCurrent Win Count: "+winread+"\nCurrent Loss Count: "+losread)
inp = input("Input: ")

if inp == "w" or inp == "win":
    for line in wd:
        num = int(line) + 1
        to_write.append(num)
        wd.reload()
    wd.seek(0)
    for num in to_write:
        wd.write(str(num)+'\n')
    wd.close()
    print("New Win Count: "+winread+"\nLoss Count: "+losread)
    input("")
elif inp == "l" or inp == "loss":
    ld.write("1")
    print("New Loss Count: "+losread+"\nWin Count: "+winread)
    input("")
elif inp == "help":
    print(helpread)
    input("")
else:
    print("Invalid input, try again.")
    input("")

Everything I've done so far is in the first if statement. I'm not getting any error when I run the code, even when I input "w", but the number in the text file doesn't change. Thanks in advance for any help, and I'll stay on the page to answer any questions that may help you figure out what's wrong.

vaultah
  • 44,105
  • 12
  • 114
  • 143
MalyG
  • 301
  • 1
  • 6
  • 15

2 Answers2

1

I would recommend using a single file database like SQLIte instead of separated text files. You also can register all wins and losses (with timestamp, if you needed later).

import sqlite3

db = sqlite3.connect('winloss.db')
cursor = db.cursor()
cursor.execute('''
    CREATE TABLE IF NOT EXISTS winloss (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        t TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        value TEXT
    );
''')
db.commit()


def add_value(db, value):
    cursor = db.cursor()
    cursor.execute("INSERT INTO winloss(value) VALUES(?)", (value, ))
    db.commit()


def add_win(db):
    add_value(db, "win")


def add_loss(db):
    add_value(db, "loss")


def count_value(db, value):
    cursor = db.cursor()
    cursor.execute("SELECT COUNT(*) FROM winloss where value=?", (value, ))
    return cursor.fetchone()[0]


def count_win(db):
    return count_value(db, "win")


def count_loss(db):
    return count_value(db, "loss")


if __name__ == '__main__':
    print "now"
    print "win:", count_win(db)
    print "loss:", count_loss(db)
    print "adding values"
    add_win(db)
    add_win(db)
    add_loss(db)
    print "win:", count_win(db)
    print "loss:", count_loss(db)

And it is easier to read and understand

Juan Fco. Roco
  • 1,608
  • 14
  • 11
0

As you seem to have very small data in your lossdata.txt and windata.txt, I would preferably do the following :

  • read the entire file content and store the data into variables (open the file in readmode, read data, then close the file),
  • overwrite the entire file content with the new value (open the file in writemode, write data, then close the file)

If you need to update a particular line of your file, know that it's better to make a copy of your input file and create the updated file as a new file. This is exactly what fileinput.input(inplace=1) does as noted in this SO answer.

So, try to do something like :

import fileinput

def process(line):
    return int(line) + 1

for line in fileinput.input(inplace=1):
    print process(line)
Community
  • 1
  • 1
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40