1

i have been trying to do a theory test in python, and i want the code to take a random question from the file that i created and ask the user for the answer.

i made two files, one contains the questions and the other one is for the answers.

value = 0

q1 = open('questions.txt','r')
q1_question = q1.readlines()[0]
print(q1_question)

a1 = open('answers.txt','r')
a1_answer = a1.readlines()[0]
answer1 = input("Enter your answer: ").lower()

if answer1 != a1_answer:
    value = value + 1

the value is for the wrong answers, now, this is just the first question, my goal is that the user get a random question and would need to type the correct answer, and in the end the user will be shown how much questions he got wrong.

answers.text: b

questions.text: What should a driver do after parking his vehicle?| A) Turn on all the winkers. B) Ensure that the vehicle remains immobile and cannot move. C) Place a warning triangle. Wearing a seat belt as required:| A) Keeps the driver in an upright sitting position. B) Reduces the extent of injury during an accident. C) Keeps the driver alert. Is it permitted to re- fuel a vehicle while its motor is running?| A) Absolutely not. B) Yes, in case of self-fueling. C) Yes, when the vehicle stands next to a fire extinguishing station. It is prohibited to stop, park or stand a vehicle on or before a pedestrian crossing, within a distance of:| A) 20 meters from the crossing. B) 12 meters from the crossing. C) 24 meters from the crossing. Which driver related ability is impaired during fog?| A) Vision. B) Vehicle acceleration. C) Steering. Is it permitted to drive passengers for hire in a private passenger car?| A) Only during a public transportation strike. B) No. C) Only in inter-urban rides.

Skies
  • 21
  • 6
  • Please post your questions.txt, answers.txt and expected output. – user1717828 Mar 11 '21 at 20:51
  • If your files are very large, you may have problems with `readlines()` running out of memory. In that case maybe you can adapt https://stackoverflow.com/q/232237/5987 to Python. – Mark Ransom Mar 11 '21 at 21:29

2 Answers2

0

Assuming the questions and answers are separated by a newline, you can use this

import random
number_of_questions = 100 #an example, set it to the actual value
i = random.randrange(0, number_of_questions)

question = q1.readlines()[i]
answer = a1.readlines()[i]

You should be able to take it from here.

QWERTYL
  • 1,355
  • 1
  • 7
  • 11
0

It's difficult to answer this without an example of your questions.txt or answers.txt but based on what you've posted, if the two mentioned files are guaranteed to have the same number of lines and every line is a question/answer - the following has been updated to not repeat questions:

import random

def unasked_question_number(q_dic, upper_limit):
    qn = random.randint(1, upper_limit) - 1
    while (q_dic.get(qn) is not None):
        qn = random.randint(1, upper_limit) - 1
    q_dic[qn] = True
    return qn
    
value = 0
questions_asked = {}

q_file = open('questions.txt','r')
questions = q_file.readlines()
a_file = open('answers.txt','r')
answers = a_file.readlines()
q_file.close()
a_file.close()

number_of_questions = len(questions)
question_number = unasked_question_number(questions_asked, number_of_questions)

print(questions[question_number])
answer = input("Enter your answer: ").lower()

if answer != answers[question_number]:
    value = value + 1
jfakour
  • 51
  • 2