Python quiz - Where the user can add his questions

Category: python (3 Views)

I'm making a quiz in Python where a user can add himself the questions and answers and specify the correct one. I already did this

# Do you want to add another question version
from Question import Question

question_prompt = []
answer_list = []
def add_Question():
            question_insert = f"{question} \n {a} \n {b} \n {c} \n {d}\n\n"
            listing = tuple(zip(question_prompt, answer_list))
            questions = [Question(question_prompt[i],answer_list[i]) for i in range(0, len(listing))]
        else : 

In a function called def insert_question_prompt() : I'll ask the user if he wants to insert another question and repeat the add_Question function.

The run_test function is to test the quiz. Play and access it

def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
    print("Hey You got " + str(score) + "out of " + str(len(questions)) + "questions correct")

But I am unable to access question_prompt and answer_list in the run_test function even though they are not empty. Basically, I'm blocked at level where the user can test the quiz ie Play and have the score.

Thanks for your help

🔴 No definitive solution yet

📌 Solution 1

questions is a local variable inside add_Question(): an outer function cannot have access to it unless questions is set as global. Also you're not showing were do you call run_test().

I think you should rethink the whole program in a simpler way: having all these functions taking in Question objects and working together, suggests you may group them inside a Questions class with a _questions attribute and these functions as methods.

Also, let your functions do one thing and one only. The sooner you learn this, the better.