我正在尝试更新文件swimmerpoints.txt
的内容。我当前的代码如下:
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()
swimmerpoints.txt
的内容如下:1,JOHNNY,492
。当我提示输入游泳者ID(在本例中为1)时,出现此错误:
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位于swimmerid
JOHNNY在name
的位置
492位于points
读取文件并与正确的ID匹配的正确方法是什么?