Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Jasmine Lewis
Jasmine Lewis
377 Points

TypeError: Question() takes no arguments ...Stuck on how to resolve the 'self' argument

I am brand new to Python and trying to build my first multiple choice question and am getting the following error after writing my 'Question_prompts = ' code: TypeError: Question() takes no arguments

and I'm not sure how to fix it.

My code looks like this:

Creating a Multiple Choice Quiz in Python

from questions 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) Pink\n(b) Red\n(c) Blue\n\n" ]

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

test

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

run_test(questions)

I am getting the error in the 'question =[' section where it is highlighting:

Question(Question_prompts[0], "a"),
Question(Question_prompts[1], "c"),
Question(Question_prompts[2], "b"),

Please help.

Steven Parker
Steven Parker
229,644 Points

"Question" is apparently defined in a different module not shown here (questions.py).
Also, when posting code, be sure to use Markdown formatting.

2 Answers

Jasmine Lewis
Jasmine Lewis
377 Points

Steven Parker Thank you! My second page is a simple questions.py :

class Question: def init(self, prompt, answer): self.prompt = prompt self.answer = answer

Steven Parker
Steven Parker
229,644 Points

Please edit your code (in both places) and enclose it in Markdown so it is displayed properly!

Steven Parker
Steven Parker
229,644 Points

Without Markdown formatting, it's impossible to try the code or check it for proper indentation. Plus, some of your characters are being converted into display instructions (such as titles with larger fonts).

But I can see one potential issue already: using the same name for a global variable, a parameter, and a loop variable ("question") is likely to cause problems.

Jasmine Lewis
Jasmine Lewis
377 Points

Yesterday was my first day coding and my first day using Python, so I am not sure what Markdown means but I will google it to figure out the formatting and re post the error, thank you.

Steven Parker
Steven Parker
229,644 Points

The blue words in my original comment are a link that will take you to an instructional video. :wink:
And clicking the bold words Markdown Cheatsheet below the answer section :arrow_heading_down: will bring up a reminder list.

Jasmine Lewis
Jasmine Lewis
377 Points

Creating a Multiple Choice Quiz in Python

# I am brand new to Python.
# I have two .py files: 'MCQ.py' and 'questions.py'.
# I am getting the following error after writing my, 'Question_prompts = ' code inside of 'MCQ.py':

TypeError: Question() takes no arguments

# My code on 'MCQ.py' is:

Creating a Multiple Choice Quiz in Python

from questions 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) Pink\n(b) Red\n(c) Blue\n\n"

]

question = [

Question(Question_prompts[0], "a"),

Question(Question_prompts[1], "c"),

Question(Question_prompts[2], "b"),

]

# My code on 'questions.py' is:

class Question:

definit(self,prompt,answer):

self.prompt = prompt

self.answer = answer

# Python IDE kicks out the following error message:

Traceback (most recent call last):

File "C:/Users/JPS/PycharmProjects/Project/MCQ.py", line 13, in <module>

Question(Question_prompts[0], "a"),

TypeError: Question() takes no arguments

Please assist if you can, thank you.
Steven Parker
Steven Parker
229,644 Points

It looks like you may be using the Markdown symbols for blockquote instead of for code. Code starts with a line containing only 3 accent symbols and the name of the language (like :point_right: ```python ). Your code follows and at the end place another line with just the 3 accents. It will look like this:

# this is Python in Markdown
for something in whatnot:
    myval = something.val  # notice indenting is preserved!

Also, you don't need to repeat the code, you can edit the code already in the question to add the formatting marks. Click the little rectangle with the 3 dots in it to reveal the "Edit Question" or "Edit Comment" options.