strings can't be print from text file

so i'm doing a quiz python program and it seems there is some problem with my program. at first time if you want to start the quiz, the quiz will start and print out the question like a normal quiz does and when you answer the right question i will give you points. however after the quiz end and you wanted to do the quiz again, that's where the problem starts, when i want to start the quiz, non of the question are printed out

while True:
    print('1. Take test, 2. Add Question, 3. Modify, 4. Delete, 5. Exit')
    n=input('Choice: ')
    counter=0
    lines=q.readlines()
    liness=p.read()
    key=liness.split('\n')
    while n not in ('1','2','3','4','5'):
        print('Invalid Choice')
        n=input('Choice: ')

    if n=='1':            
        score=0
        counter=0
        n=0
        nb=0

        while True:
            linez=lines[n:n+5]
            for line in linez:
                print(line)
            b=CheckAnswer()
            if b==key[nb]:
                score=score+1
                print('Nice')
            n=n+6
            nb=nb+1
            counter=counter+1
            print('Current Score: ',score)
            if counter>=len(key):
                break

can anyone help me to solve this problem ?

Upvotes: 0

Views: 84

Answers (1)

Vajk Hermecz
Vajk Hermecz

Reputation: 5722

After you've executed these lines:

lines=q.readlines()
liness=p.read()

The file pointers p and q are pointing to the end of the file. The following calls to readlines and read will start at the end of the file, and thus, will return an empty answer...

You can use q.tell() to check your actual position in the file, and q.seek(0) will reset the pointer to the beginning of the file...

In this particular case, adding the following lines after you read from the files will solve the problem:

q.seek(0)
p.seek(0)

Upvotes: 1

Related Questions