0

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?

Johnny
  • 211
  • 3
  • 15
  • 1
    If you print `swimmerid` you'll see that its values are strings, not integers. That's why the integer `1` in not in the list. Perhaps you meant `swimmerid.append(int(lists[0].strip()))`. – FMc Aug 16 '20 at 01:13
  • @FMc changed that and got the same error – Johnny Aug 16 '20 at 01:15
  • 1
    @Johnny There are two problems: (1) You're searching for an integer in a list that contains only strings, and (2) Even if you change the list to contain integers, you will still get the error if the particular integer you're searching for is not in the list. – Tom Karzes Aug 16 '20 at 01:17
  • @TomKarzes I don't understand - why would making 1 an integer not work? And for 2, yes, that's fine, but since the integer in question is in the list, why does it not show up? – Johnny Aug 16 '20 at 01:21
  • Try sticking a `print()` statement inside your loop: you'll see it never prints. Maybe you should read about how the `a+` file mode works: https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function In other words, you are not reading anything from the file. – FMc Aug 16 '20 at 01:21
  • 1
    @Johnny The list contains only strings. Therefore it contains no integers. Try this: `1 in ["1"]`. The result is `False`, since the integer `1` is not present in the list, which contains the string `"1"`. I'm talking about the posted code. I don't know what changes you've made since then, but that would be a new question. If your list now contains integers, then close this question and post a new one with the new problem. – Tom Karzes Aug 16 '20 at 01:24
  • @TomKarzes I changed 1 into "test" and tried searching for test. still didn't work. Not sure how I can fix this – Johnny Aug 16 '20 at 01:38
  • @TomKarzes Sorry for all the confusion, I edited my question for clarity. I hope it makes more sense now with my attempt at explaining further. – Johnny Aug 16 '20 at 01:50
  • @TomKarzes As I responded to that comment, I tried using that but got the same exact error. – Johnny Aug 16 '20 at 01:55
  • 1
    What is the *exact* value of `swimmerid`, and what is the *exact* value of `x`? One more time, I don't know what code you're running because the original post is no longer relevant. It would be much better to close this question, and open a new one with *precise* information. I could diagnose this problem in about 2 seconds if I knew what code you were running and what values you're using, but you have still not communicated that information. What is `swimmerid`, and what is `x`? – Tom Karzes Aug 16 '20 at 01:59
  • `swimmerid` is the integer 1, since I used the code from the first comment, and `x` is the integer 1 as well – Johnny Aug 16 '20 at 02:01
  • 1
    Ok, so `swimmnerid` is no longer a list, but is now an integer? Then you have your answer: Integers do not support `find`. You need a list (or a similar type) to use `index`. Try it yourself: `1.index(1)` gives an error. Are you sure `swimmerid` is an integer? Are you sure it isn't the list `[1]`? *Be precise*. – Tom Karzes Aug 16 '20 at 02:02
  • 1
    Just add a debugging statement: `print(swimmerid)`. Then you'll see exactly what `swimmerid` is. Then just paste it here, *exactly* as it appears. – Tom Karzes Aug 16 '20 at 02:06
  • Just shows up as [], a blank list, placed the debug statement after the input for x – Johnny Aug 16 '20 at 02:08
  • 1
    Ok, so there's your answer: You have an empty list, and `1` isn't in it. You're doing `[].index(1)`, and getting an error, just as you'd expect, since `1` is not in the list. – Tom Karzes Aug 16 '20 at 02:10
  • How would I get the the 1 into the list though? But without directly putting 1, obviously – Johnny Aug 16 '20 at 02:12
  • 1
    Oh, your `open` mode is wrong. You're opening the file for appending and updating, which positions you to the end. Change `"a+"` to `"r"`. That should fix your input problems. – Tom Karzes Aug 16 '20 at 02:14
  • The `"r"` changed worked perfectly, but fails after I enter the new point value. With ```Traceback (most recent call last): File "main.py", line 32, in update() File "main.py", line 29, in update files.write(f"{changes}") io.UnsupportedOperation: not writable``` – Johnny Aug 16 '20 at 02:16
  • 1
    So you want to read, then rewrite the existing input file? You're better off reading it, then closing it, then opening it for write access and writing the new contents. Or even better, use a *different* file for the output, if you can. If you want to rewrite the entire file, use `"w"` for the second open mode. If you want to append to the end, use `"a"`. – Tom Karzes Aug 16 '20 at 02:20
  • The `"w"` for the second open mode worked! Thank you so much, my code works great now. Sorry for all the trouble, but I cannot thank you enough. God bless. – Johnny Aug 16 '20 at 02:23

0 Answers0