Python Forum

Full Version: Self taught , (creating a quiz) syntax error on my array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im self teaching python , trying to create a quiz but Im having a error on line 17 , im importing from from this code
Quote:Question.py
class Question:
	def__init__(self, prompt, answer):
		self.prompt = prompt
		self.answer = answer
for file
Quote:building a multiple choice quiz.py
from Question import Question

question_promts = [
	"What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n",
	"What color are Bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n" ,
	"What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n"
]

questions = [
	Question(question_prompts[0],"a" ),
	Question(question_prompts[1],"c" ),
	Question(question_prompts[2],"b" ),
]

def run_test(questions):
	score = 0
	for question in questions
		answer = input(question.prompt)
		if answer == question.answer:
			score += 1
	print("You got " + str(score) + "/" + str(len(questions)) + "correct")
	
	run_test(questions)
However it keeps telling me that I have a syntax error on line 17 when I run the program.
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pyroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile.mainpyfile)
File "/data/user/0/ru.iiec.pyroid2/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read().   __main__.__dict__)
File "<string>", line 17
for question in questions
                        ^
SyntaxError: invalid syntax

[Program finished]
I looks like a colon is missing at the end of the line...
I tried colon and I still receive the same syntax error only with a colon typed in
(Jan-09-2020, 05:52 PM)DarkAlchemyXEX Wrote: [ -> ]I tried colon and I still receive the same syntax error only with a colon typed in

You need the colon. I ran it and you don't get the same error with it.
I deleted both files and decided to re-do the whole program , the good news is that Process finished with exit code 0 however nothing prints in the the terminal so the user cannot input naswers to the questions . This is what I have now.
Quote:app.py
from Question import Question

question_prompts = [
    "What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n" ,
    "What color are Bananas?\n(a) teal\n(b) Magenta\n(c) Yellow\n\n" ,
    "What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n"
]

question = [
    Question(question_prompts[0], "a"),
    Question(question_prompts[1], "c"),
    Question(question_prompts[2], "b"),
]

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

run_test(questions)
Quote:Question.py
class Question:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer
Process finished with exit code 0
I noticed the error was fixed when my first attempt
class Question:
    def__init__(self, prompt, answer):
and second attempt
    def __init__(self, prompt, answer):
        self.prompt = prompt
I forgot to put a space between def and __init which was breaking the program . Again thank you all for helping I am learning this on my own and appreciate the input from everyone
On line 23 questions is not defined. Is line 9 supposed to be questions instead of question?
I still get the same end result after the change ichabod801
You're also missing a colon at the end of line 19. It would really help if you would post the full text of the error messages you are getting.
C:\Users\Admin\PycharmProjects\Giraffe\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Giraffe/Question.py

Process finished with exit code 0
im not getting any prompts to answer the question
When I run your code with the fixes I have mentioned, it works.