Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MCQ logic
#1
Hi all,

I am writing a code that read MCQ from a file like this :

N1. question 1
A. answer A
B. answer B
C. answer C
D. answer D

So I create a list that store the question number :
def load_exam_questions_num(filepath):
    # get the questions number
    question_num = 0
    with open(filepath, "r") as file:
        for line in file.readlines():
            if line.startswith('NO.'):
                st.list_q_number.append(line[3:5])
                question_num += 1
then I store the questions in another list :
def load_exam_questions(filepath):
    # get the questions
    question = ""
    with open(filepath, "r") as file:
        for line in file.readlines():
            if line.startswith('A.'):
                st.list_q_desc.append(question.rstrip())
            elif line.startswith('NO.'):
                question = ""
                question += line.strip() + '\n'
            else:
                question += line.strip() + '\n'
and I create a list of list to store the different possible answers :
st.exam_questions = [[0 for j in range(7)] for i in range(len(st.list_q_number))]
then I want to insert on it the answers but I get an error :
def load_exam_answers(filepath):
    answer = ""
    question = 0
    print(st.exam_questions)
    with open(filepath, "r") as file:
        for line in file.readlines():
            if line.startswith('Answer:'):
                st.exam_questions[question][question] = answer.rstrip()
                question += 1
                print(question)
            elif line.startswith('A.	'):
                answer = ""
                answer += line.strip() + '\n'
            else:
                answer += line.strip() + '\n'
Can anyone tell me if my logic is OK ? Undecided
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020