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
#2
each time the for loop loops it is using each_question so you need to change the following question's to each_question

def run_test(questions):
    score = 0
    for each_question in questions:
        answer = input(each_question.prompt)
        if answer == each_question.answer:
            score += 1
    print("You got " + str(score) + "/" + str(len(questions)) + " correct.")
In the video
for question in questions:
was used
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.")
Reply
#3
(Mar-27-2019, 11:58 PM)Yoriz Wrote: each time the for loop loops it is using each_question so you need to change the following question's to each_question

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

Oh, I'm an idiot. Thank you so much!
Reply


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