0

This is part of my code for a simple music quiz. However, file.readline() returns an empty space whenever I run it. There is a text file called storage.txt with the data in it. I have tried using file.seek(), file.read(), and file.readlines() but none so far have allowed me to print a specific line.

points = 0
question_number = 0

file = open("storage.txt","r")
while True: 
 question_number = question_number + 1
 keyword = file.readline(question_number)
 print("Who made this song?")
 answer = input(keyword)
 if answer == file.readline(question_number+1):
  print("You got it right, 1 point for you!")
  points += 1
 else:
  print("You lost with",points,"points.")
  break

file.close()
print("All done.")
big T
  • 3
  • 1
  • Can you check the readline definition once? https://www.w3schools.com/python/ref_file_readline.asp – techrhl Sep 23 '21 at 11:34

1 Answers1

1

The usage of file.readline() here is wrong. Check out what parameters the function should get. Maybe this could help. readline() reads the next line of the file, and not the line specified in the parameter. The parameter is actually size, or how many bytes to read of the line. If you want to read a specific line, I'd suggest doing something like this -

keyword = file.readlines()[question_number]

you might want to refer to this for how to use the readlines() function.

Anyways, I suggest you name your file variable something else, as file is a saved word. Call it something like my_file, or song_titles_file, or any other meaningful name.

alonkh2
  • 533
  • 2
  • 11