I am trying to update the contents of the file swimmerpoints.txt
. My current code is as follows:
def update():
swimmerid = []
name = []
points = []
files = open('swimmerpoints.txt', 'a+')
for info in files:
info = info.strip()
lists = info.split(',')
swimmerid.append(lists[0].strip())
name.append(lists[1].strip())
points.append(lists[2].strip())
x = int(input("Swimmer ID to update points: "))
index = swimmerid.index(x)
print(name[index] + "'s", "point total is:", points[index])
changes = int(input("Enter the new point value: "))
files.write(f"{changes}")
files.close()
update()
The contents of swimmerpoints.txt
are this list: 1, JOHNNY, 492
. When I get the prompt to enter the Swimmer ID, which in this case is 1, I get this error:
Traceback (most recent call last):
File "main.py", line 29, in <module>
update()
File "main.py", line 24, in update
index = swimmerid.index(x)
ValueError: 1 is not in list
1 is in the spot of swimmerid
JOHNNY is in the spot of name
492 is in the spot of points
What is the proper way to read my file and match with the proper ID?