Python Forum
Having issues getting a multiple choice test program to work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having issues getting a multiple choice test program to work
#1
I have been working on a program that I saw on a tutorial that creates Multiple choice test.

I created a class named Question.
Code for Question:
class Question:
    def _init_(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer
The rest of my code is another file
Rest of Code:

from Question import Question
question_prompts = [
    "What kind of vessel is the Japanese Soryu Class?\n(a) Submarine\n(b) Frigate\n(c) Destroyer\n(d) Cruiser\n\n",
    "What is the primary anti-ship weapon of the US Navy?\n(a) SM2\n(b) SM6\n(c) Sea_Sparrow\n(d) Harpoon\n\n",
    "Name of the new destroyer class in the Chinese PLA Navy?\n(a) Type_45\n(b) Type_52C\n(c) Type_55\n(d) Type_23\n\n"
]

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

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(question)) + "correct ")

run_test(questions)
When I run the code the following error is created
Error:
Traceback (most recent call last): File "C:/Users/johne/PycharmProjects/untitled/Multiple choice quiz.py", line 9, in <module> Question(question_prompts[0], "a"), TypeError: Question() takes no arguments
I am unsure of what the issue is with the arguments. Any help would be greatly appreciated
Reply
#2
The problem is in your Question() class init method. It should have two trailing underscores on each side, not one:
# your init
def _init_(self, prompt, answer):

# correct init
def __init__(self, prompt, answer):
And next time when you post on the forums, please use Python code tags and error tags. Help is available here.
Reply
#3
Hey guys thanks for the suggestions! You helped me fix my code. I will definitely read up on this stuff.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,432 Sep-10-2023, 10:27 PM
Last Post: janeik
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 745 Aug-09-2023, 05:51 PM
Last Post: Calab
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 955 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Can't get a new program to work jpotter0 2 985 Nov-28-2022, 03:40 AM
Last Post: jpotter0
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,205 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  getting an import statement to work in my program barryjo 1 1,655 Dec-06-2021, 04:28 PM
Last Post: snippsat
  random.choice HELP samuelbachorik 4 2,261 Aug-18-2021, 03:24 PM
Last Post: naughtyCat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,114 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to work with multiple files and tkinter? philipbergwerf 3 4,595 Aug-31-2020, 12:03 PM
Last Post: ndc85430
  ???: if "{choice}" in self._command BaiYouLing4 3 2,016 Aug-23-2020, 05:40 AM
Last Post: BaiYouLing4

Forum Jump:

User Panel Messages

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