Python Forum
"not defined" error in function referencing a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"not defined" error in function referencing a class
#1
Question.py
class Question:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer
scratch.py
from Question import Question


question_prompts = [
    "What does nauta mean?\n(a) man\n(b) sailor\n(c) poet\n(d) farmer",
    "What does poeta mean?\n(a) man\n(b) sailor\n(c) poet\n(d) farmer",
    "What does agricola mean?\n(a) man\n(b) sailor\n(c) poet\n(d) farmer"
]

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

def run_test(questions):
    score = 0
    for each_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)
I'm getting an error saying that "question.prompt" and "question.answer" are not defined, but I'm using the exact same code from this video tutorial, and it's not working for me.
Reply


Messages In This Thread
"not defined" error in function referencing a class - by Exsul - Mar-27-2019, 11:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 927 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 719 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,564 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  "Name is not defined" when running a class lil_e 6 4,516 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Getting NameError for a function that is defined JonWayn 2 1,202 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 3,138 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,422 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,783 May-25-2022, 07:22 PM
Last Post: Gribouillis
  TimeOut a function in a class ? Armandito 1 1,767 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,678 Apr-21-2022, 12:33 PM
Last Post: KatManDEW

Forum Jump:

User Panel Messages

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