0

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!!!

2
Contributors
2
Replies
17
Views
3 Months
Discussion Span
Last Post by _kr4t0s_
2

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

Edited by AndrisP

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.