0

after starting this part of the code the code throws an error

after starting this part of the code "if os.path.getsize("loginon.txt") != 0: win1=0 lose1=0 win2=0 lose2=0 win3=0 lose3=0 f = open("loginon.txt", "r",encoding="utf-8") f1 = open("logs.txt", encoding="utf-8") nameuser=f.read() for i2 in f1: i=i2.split(" ") if i[0]==nameuser: win1 = int(i[2]) lose1 = int(i[3]) win2 = int(i[4]) #line150 error lose2 = int(i[5]) win3 = int(i[6]) lose3 = int(I[7]) f.close() f1.close() ", the code throws an error: Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 150, in <module> IndexError: list index out of range File Contents logs.txt: лось 728 0 0 0 0 0 0 котлета 555 0 0 0 0 0 0 й 123 1 0 0 0 0 0 тест 123 0 0 0 0 0 0

12th Oct 2023, 3:15 PM
Flasher
Flasher - avatar
4 Réponses
+ 6
Did you read the error message? The error is referenced to lines 31, 30 and close on line 150, which we don‘t see, do we? Second important part of the message is the last row: „list index out of range“.
12th Oct 2023, 3:29 PM
JaScript
JaScript - avatar
+ 3
Flasher could you explain a bit more what was happend?
13th Oct 2023, 12:00 PM
JaScript
JaScript - avatar
0
It seems like the issue is related to the way you're processing the lines from 'logs.txt'. Specifically, it appears that some lines do not contain enough elements after splitting by spaces. To handle this, you should add a condition to check whether 'i' contains enough elements before trying to access specific indices. You can also add some print statements for debugging purposes. Here's a modified section of your code: if os.path.getsize("loginon.txt") != 0: # ... (previous code remains the same) for i2 in f1: i = i2.split(" ") if len(i) >= 8 and i[0] == nameuser: win1 = int(i[2]) lose1 = int(i[3]) win2 = int(i[4]) lose2 = int(i[5]) win3 = int(i[6]) lose3 = int(i[7]) else: print(f"Invalid format in line: {i2}") f.close() f1.close()
13th Oct 2023, 1:05 AM
Lutreze Hue Jacinto
Lutreze Hue Jacinto - avatar
0
thanks, but I have already solved this problem in another way
13th Oct 2023, 10:52 AM
Flasher
Flasher - avatar