Jan-12-2023, 11:10 PM
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:
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:Error:Traceback (most recent call last):
File "C:/Users/Lenovo/Documents/Python/Examples_book/Page_223_language_survey.py", line 8, in <module>
my_survey.show_question()
File "C:\Users/Lenovo/Documents/Python/Examples_book\survey.py", line 13, in show_question
print(question)
NameError: name 'question' is not defined
