0

I am coding a quiz and I am trying to read line 2 from this file, reading the first line works but the second one gives me an error "IndexError: list index out of range" Here is the part of the code that gives me errors:

def business():
    difficulty = input("Easy, Medium or Hard? ")
    if difficulty == "easy":
        score = 0
        questions = open("buseasyquestions.txt","r")
        answers = open("buseasyanswers.txt","r")
        question1 = questions.readlines(0)[0].split("1")[0]
        answer1 = answers.readlines()[0]
        print(answer1 + "2) revenue + costs")
        gues1 = int(input(question1 + " "))
        if gues1 != 1:
            print("Wrong, next question...")
            if gues1 == 1:
                score += 1
                print("Correct! Next question...")

        question2 = questions.readlines(1)[1].split("2")[0]

Oh and this is what the txt file says:

What is the formula for PROFIT?1
test2
khelwood
  • 55,782
  • 14
  • 81
  • 108
MiniMiloe
  • 77
  • 1
  • 5
  • Which line is the error being thrown on? You say "trying to read line 2" but you have only posted 1 line of text. – Jim Wright Jan 18 '18 at 09:04
  • "What is the formula for PROFIT?1 test2" is meant to be two lines, the second line is the "test2" – MiniMiloe Jan 18 '18 at 09:05
  • I see someone edited the question, so I can see that now :) – Jim Wright Jan 18 '18 at 09:05
  • Why are you calling `questions.readlines()` twice? After the 1st call you've read all the lines, so there's nothing left for the 2nd call to read. – PM 2Ring Jan 18 '18 at 09:07
  • why are you passing an argument to `questions.readlines(0)`? That is *how many bytes to read before stopping. If you read `0` bytes, you create `0` lines, so the list will have length 0, so indexing into it with anything will throw an index error... EDIT actually, i've never used this and had to check, but actually, `0` will read *all lines*. – juanpa.arrivillaga Jan 18 '18 at 09:09
  • The first call only reads the first line, thats all it prints anyway – MiniMiloe Jan 18 '18 at 09:09
  • Because of the arguments you are passing to `readlines`, in any event, it woudl fail on the second call *anyway*, but both of those are issues – juanpa.arrivillaga Jan 18 '18 at 09:11
  • Please see [the `readlines` docs](https://docs.python.org/3/library/io.html#io.IOBase.readlines). The arg to `readlines` is a size hint, with the size measured in bytes or characters (depending on which mode the file was opened in). It is **not** the number of lines you want to read. – PM 2Ring Jan 18 '18 at 09:13
  • That code doesn't print the result of the 1st call. But anyway, `question1 = questions.readlines(0)[0].split("1")[0]` reads all the lines into a list, grabs the first line from that list, and then calls `.split` on that line. – PM 2Ring Jan 18 '18 at 09:15
  • I changed the question2 line to "question2 = question1[1]" but it just prints "h" – MiniMiloe Jan 18 '18 at 09:27
  • Just create a list of all the questions, and a list of all the answers, eg `all_questions = questions.readlines()`. And from then on access those lists to get the lines you want. – PM 2Ring Jan 18 '18 at 09:45
  • That worked,thank you – MiniMiloe Jan 18 '18 at 10:48

0 Answers0