Hi I am learning python and there is these two code snippets but I keep getting an error when I try to run the program.

# survey.py

class AnonymousSurvey():
    # Collect anonymous answers to a survey question.

    def __init__(self, question):
        # Store a question, and prepare to 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 for the survey
        self.responses.append(new_response)

    def show_results(self):
        # Show all responses that have been given
        print("Survey results: ")
        for response in responses:
            print('- ' + response)

# language_survey.py

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()
This is the error message I get:
Traceback (most recent call last):
  File "../Documents/Python Code/language_survey.py", line 8, in <module>
    my_survey.show_question()
  File "../Documents/Python Code/survey.py", line 11, in show_question
    print(question)
NameError: name 'question' is not defined

Process finished with exit code 1

Thanks alot for the help!!!

Recommended Answers

All 2 Replies

line 13 should be print(self.question)
and similar mistake in the line 22

Thanks Alot AndrisP the error was also present in the book.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.