0

I am making my first game and want to create a score board within a .txt file, however when I try and print the score board it doesn't work.

with open("Scores.txt", "r") as scores:
        for i in range(len(score.readlines())):
          print(score.readlines(i + 1))

Instead of printing each line of the .txt file as I expected it to instead it just prints []

The contents of the .txt file are:

NAME: AGE: GENDER: SCORE:

I know it's only one line but it should still work shouldn't it?

*Note there are spaces between each word in the .txt file, though Stack Overflow formatting doesn't allow me to show that.

MrDDog
  • 7
  • 6

2 Answers2

1

Assign the result of score.readlines() to a variable. Then you can loop through it and index it.

with open("Scores.txt", "r") as scores:
    scorelines = scores.readlines()

for line in scorelines:
    print(line)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

.readlines() reads everything until it reaches the end of the file. Calling it repeatedly will return [] as the file seeker is already at the end.

Try iterating over the file like so:

with open("Scores.txt", "r") as scores:
    for line in scores:
        print(line.rstrip())
Bharel
  • 23,672
  • 5
  • 40
  • 80