-1
points = "temp"
a = "temp"
f = "temp"

def pointincrementer():
  global points
  points = 0
  for line in f:
    for word in a:
      if word in line:
        scorelen = int(len(user+","))
        scoreval = line[0:scorelen]
        isolatedscore = line.replace(scoreval,'')
        if "," in  line:
          scorestr = isolatedscore.replace(",","")
          score = int(scorestr)
          points = score + 1
          print(points)

def score2():
  f = open('test.txt','r')
  a = [user]
  lst = []
  for line in f:
    for word in a:
      if word in line:
        pointincrementer()
        print(points)
        point = str(points)
        winning = (user+","+point+","+"\n")
        line = line.replace(line,winning)
    lst.append(line)
  f.close()
  f = open('test.txt','w')
  for line in lst:
    f.write(line)
  f.close()
  print("Points updated")


user = input("Enter username: ") #change so user = winners userid
with open('test.txt') as myfile:
  if user in myfile.read():
    score2()
  else:
    f = open('test.txt','r')
    f2 = f.read()
    f3 = (f2+"\n"+user)
    f.close()
    f = open('test.txt','w')
    f.write(f3)
    f.close()
    score2()

This is paired with test.txt, which looks like this:

one,1,
two,5,
three,4,
four,94,

When this code is run, it it will ask the user their name (as expected) and then will print 0 (when it should instead print the user's score) and then Points updated. Anybody know how to sort this out?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
Xtreme
  • 1
  • 1
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – CrazyChucky Dec 31 '21 at 00:49

2 Answers2

3

There are many problems with your code. You should not be using global variables like that. Each function should be passed what it needs, do its computing, and return values for the caller to handle. You should not be reading the file multiple times. And you can't write the file while you still have it open with the with statement.

Here, I read the file at the beginning into a Python dictionary. The code just updates the dictionary, then writes it back out at the end. This makes for a simpler and more maintainable structure.

def readdata(fn):
    data = {}
    for row in open(fn):
        info = row.strip().split(',')
        data[info[0]] =  int(info[1])
    return data

def writedata(fn,data):
    f = open(fn,'w')
    for k,v in data.items():
        print( f"{k},{v}", file=f )

def pointincrementer(data,user):
    return data[user] + 1

def score2(data, user):
    points = pointincrementer(data, user)
    print(points)
    data[user] = points
    print("Points updated")

user = input("Enter username: ")

data = readdata( 'test.txt' )
if user not in data:
    data[user] = 0

score2(data, user)
writedata( 'test.txt', data )
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • I appreciate this effort, my coding skills as you saw are all over the place. Although this didnt add new users, i can easily add that. Thankyou for your help man :) – Xtreme Dec 31 '21 at 17:36
  • Yes, my code absolutely DOES add new users. That's the `if user not in data:` section. – Tim Roberts Dec 31 '21 at 23:52
  • when i added it to the whole code, it didnt. Thats just an observation and i already edited it so it would work. Thanks again – Xtreme Jan 01 '22 at 21:57
1

The f in pointincrementer() refers to the "temp" string declared on the third line. The f in score2() refers to the file handle declared immediately below the function header. To get around this, you can pass the file handle into pointincrementer():

def pointincrementer(file_handle):
  global points
  points = 0
  for line in file_handle:
    for word in a:
      if word in line:
        scorelen = int(len(user+","))
        scoreval = line[0:scorelen]
        isolatedscore = line.replace(scoreval,'')
        if "," in  line:
          scorestr = isolatedscore.replace(",","")
          score = int(scorestr)
          points = score + 1
          print(points)

def score2():
  file_handle = open('test.txt','r')
  a = [user]
  lst = []
  for line in f:
    print(line)
    for word in a:
      if word in line:
        pointincrementer(file_handle)
        print(points)
        point = str(points)
        winning = (user+","+point+","+"\n")
        line = line.replace(line,winning)
    lst.append(line)
  f.close()
  f = open('test.txt','w')
  for line in lst:
    f.write(line)
  f.close()
  print("Points updated")

This leads to a parsing error. However, as you haven't described what each function is supposed to do, this is the limit to which I can help. (The code is also extremely difficult to read -- the lack of readability in this code snippet is likely what caused this issue.)

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33