[Solved] Using Python to make a quiz from a text file

[ad_1]

First of all, use .strip() to remove the newline characters. The answers comparison might not always work because of them.

To ask every even row you can use the modulo (%) operator to get the remainder of the current line index after dividing it by two.

If the answer is correct we add 1 to the corrent_answer_count variable that we print in the end.

questions = open(r"textfile.txt", "r")
content = [line.strip() for line in questions.readlines()]
questions.close()

corrent_answer_count = 0
for i in range((len(content))):
    if i % 2 == 0:
        print(content[i])
        answer = input("What's the answer: ")
        if answer == content[i + 1]:
            print("Correct")
            corrent_answer_count += 1
        else:
            print("Incorrect")

print("Correct answer count: " + str(corrent_answer_count))

[ad_2]

solved Using Python to make a quiz from a text file