"Name is not defined" when running a class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: "Name is not defined" when running a class (/thread-39172.html) |
"Name is not defined" when running a class - lil_e - Jan-12-2023 Hi. I'm a beginner trying to figure out why my code won't work. I'm pretty sure it's a minor detail, but I tried several time, but cannot find out why I'm trying to make a program that runs a survey by fetching methods from another module. Appreciate your help. Thanks. Module with class: class AnonymousSurvey(): """Collect anonymous answers to a survey question.""" def __init__(self, question): """Store a question, and prepare to store responses.""" self.question = question self.responses = [] def show_question(self): """Show the survey question.""" print(question) def store_response(self, new_response): """Store a single response to the survey.""" self.responses.append(new_response) def show_results(self): """Show all the responses that have been given.""" print("Survey results:") for response in responses: print('- ' + response)My program: from survey import AnonymousSurvey # Define a question, and make a survey. question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) # Show the question, and store responses to the question. my_survey.show_question() print("Enter 'q' at any time to quit.\n") while True: response = input("Language: ") if response == 'q': break my_survey.store_response(response) # Show the survey results. print("\nThank you to everyone who participated in the survey!") my_survey.show_results()Error traceback:
RE: "Name is not defined" when running a class - Yoriz - Jan-12-2023 Need to add self. in front of question in the method show_question
RE: "Name is not defined" when running a class - lil_e - Jan-12-2023 Do you mean like this? Maybe I misunderstood because I get the same traceback as last time. def show_question(self): """Show the survey question.""" print(self.question)
RE: "Name is not defined" when running a class - Yoriz - Jan-12-2023 Yes that is now correct also self. is missing from responses in the method show_results but that would not cause the error shown.What is the full error traceback? Did you make sure you saved survey after making the change
RE: "Name is not defined" when running a class - lil_e - Jan-12-2023 Have now edited the method show_results as welldef show_results(self): """Show all the responses that have been given.""" print("Survey results:") for response in self.responses: print('- ' + response)Full error traceback is still the same:
I'm sorry. I see that I messed up and mixed the file names when running the code. It's working now. Thank you! RE: "Name is not defined" when running a class - Yoriz - Jan-12-2023 If you have made the change, the error now does not match the code print(self.question)does not match As stated above did you make sure you saved survey after making the changes?
RE: "Name is not defined" when running a class - lil_e - Jan-12-2023 Everything seems to be working now. The code looks like this after saving Thanks again! # survey.py - page 223 class AnonymousSurvey(): """Collect anonymous answers to a survey question.""" def __init__(self, question): """Store a question, and prepare to store responses.""" self.question = question self.responses = [] def show_question(self): """Show the survey question.""" print(self.question) def store_response(self, new_response): """Store a single response to the survey.""" self.responses.append(new_response) def show_results(self): """Show all the responses that have been given.""" print("Survey results:") for response in self.responses: print('- ' + response) |