Python Forum
Self taught , (creating a quiz) syntax error on my array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Self taught , (creating a quiz) syntax error on my array
#1
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]
Reply
#2
I looks like a colon is missing at the end of the line...
Reply
#3
I tried colon and I still receive the same syntax error only with a colon typed in
Reply
#4
(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.
Reply
#5
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
Reply
#6
On line 23 questions is not defined. Is line 9 supposed to be questions instead of question?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I still get the same end result after the change ichabod801
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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
Reply
#10
When I run your code with the fixes I have mentioned, it works.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,301 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 1,991 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 1,997 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,964 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,403 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,116 Feb-10-2020, 01:57 AM
Last Post: sbabu
  syntax error: can't assign to operator liam 3 4,028 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,421 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  creating functions which modify numpy array GSGSGKGK 0 1,581 Dec-15-2019, 07:04 PM
Last Post: GSGSGKGK
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,066 Oct-19-2019, 01:40 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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