2

I'm trying to create a quiz, and there is a Syntax error in the questions array for the 2nd element. I've tried appending each object to the array through a `for loop but I need each question to have a correct answer.

The Questions class is in a diffrent file:

class Questions:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

Here is the main file:

from Questions import Questions
questionsPrompt = ["What does CPU stand for?\n(a) central procesing unit\n(b)controlled purification\n(c)computer unit",
    "What is an advantage of a compiler?\n(a)slow to run each time\n(b)compiling takes a long time\n(c)easy to implement",
    "The Operating System is a :\n(a)system software\n(b)application software\n(c)utility software"]

questions = [
    Questions(questionsPrompt[0], "a")
    Questions(questionsPrompt[1], "b")
    Questions(questionsPrompt[2], "a")
]

def runQuiz(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
    return score

runQuiz(questions)
2
  • 1
    List elements have to be separated by commas. Commented Aug 22, 2018 at 17:09
  • Then do it using a loop and a list of correct answers or a dict Commented Aug 22, 2018 at 17:12

2 Answers 2

2

As Aran-Fey commented, list items must be separated by commas. This is also true for other collections as dictionaries, sets, etc.

questions = [
    Questions(questionsPrompt[0], "a"),
    Questions(questionsPrompt[1], "b"),
    Questions(questionsPrompt[2], "a")
]
Sign up to request clarification or add additional context in comments.

Comments

1

As Aran-Fey pointed out, your syntax is incorrect.

questions = [
    Questions(questionsPrompt[0], "a"),
    Questions(questionsPrompt[1], "b"),
    Questions(questionsPrompt[2], "a")
]

Also, another small point, what you are creating is a list, not an array. There are both semantic and implementation differences, and since Python has both, it's an important distinction.

2 Comments

such a silly mistake i made. what is the diffrence between an array and list?
A very short summary can be found on the python docs website at docs.python.org/3/library/array.html "This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained." Whereas, lists can contain any object, and have a different set of methods. look here: docs.python.org/3/library/stdtypes.html?highlight=lists#list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.