Python Forum
MCQ logic - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: MCQ logic (/thread-19180.html)



MCQ logic - kferhat - Jun-16-2019

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