Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run this code
f = open("save.txt", "r")
xp_levelup_save=f.readlines(3)
xp_levelup_save=[int(i.replace("\n", "")) for i in xp_levelup_save][2]
f.close()

I get the error
List Index out of range

If someone could tell me why and give me a fix thanks

What I have tried:

I have tried changing the file it finds and the line it finds the first line is fine
(readlines 1 and xp_levelup ... xp_levelup_save][0]
and
(read lines 2 and xp_levelup ... xp_levelup_save][1]
but when it gets to line 3 (read line 3 and [2])
it comes up with the error
Posted
Updated 8-Dec-17 11:38am

1 solution

The hint value is the number of bytes as described at 16.2. io — Core tools for working with streams — Python 3.3.7 documentation[^].
 
Share this answer
 
Comments
MattyB-Raps 8-Dec-17 17:09pm    
I'm not sure I understand that could you explain it in a little more detail. If you have time
Richard MacCutchan 9-Dec-17 4:19am    
> readlines(hint=-1)
> Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

In your case it means it will stop reading after three characters.

But I am not sure exactly what you are trying to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900