1

Im getting is error in python for some reason i don't understand how these are outside of the index when there are 7 elements to the file attack = user_stats.readlines()[2] IndexError: list index out of range

this is the code:

with open(username + '.txt', 'r') as user_stats:
    level = user_stats.readlines()[1]
    attack = user_stats.readlines()[2]
    health = user_stats.readlines()[3]
    max_health = user_stats.readlines()[4]
    exp = user_stats.readlines()[5]
    money = user_stats.readlines()[6]

this is the txt file:

username
1
1
25
25
0
0

3 Answers 3

2

The first call to readlines() reads the full file and reaches the end of the file. Each subsequent call returns an empty string because you've already reached the end of the file.

There is no need to call readlines() multiple times.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response, so how do i set each variable equal to the contents of that specific line then?
Read lines once and store that in a variable. Read from that variable as you intended initially afterwards.
0

well ive figured it out, idk that its the best way but it works

with open(username + '.txt', 'r') as user_stats:
    level = user_stats.readlines()[1]
with open(username + '.txt', 'r') as user_stats:
    attack = user_stats.readlines()[2]
with open(username + '.txt', 'r') as user_stats:
    health = user_stats.readlines()[3]
with open(username + '.txt', 'r') as user_stats:
    max_health = user_stats.readlines()[4]
with open(username + '.txt', 'r') as user_stats:
    exp = user_stats.readlines()[5]
with open(username + '.txt', 'r') as user_stats:
    money = user_stats.readlines()[6]

2 Comments

A better way would be to assing the result of user_stats.readlines() to a variable, and then use that instead of opening and reading the file again and again: contents = user_stats.readlines() and then level = contents[1], attack = contents[2], etc.
This opens the file, reads it in full, assign partial result to a variable, close the file and again over 7 times. The better approach is to open the file, read it in full once and store that in a variable, then extract from it 7 times such that the open-read-close operations are only performed once.
0

Firstly, as person above said the code should look something like that

with open('username.txt', 'r') as user_stats:
    lines = user_stats.readlines()
    level = lines[1]
    attack = lines[2]
    health = lines[3]
    max_health = lines[4]
    exp = lines[5]
    money = lines[6]
    print(username, level, attack, health, max_health, exp, money) #print to check if everything is right

Secondly, such things as stats should be stored inside objects but from what i can remember there are some limitations to python constructors, so i cannot really help much here. more about constructors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.