Jump to content

survey exercise with Python

DominHoes
Go to solution Solved by RadiatingLight,
Just now, RadiatingLight said:

should be 


print(self.question)

because you are accessing a class variable

 

also your docstring for show_results is wrong.

this also applies to show_responses, you need to access self.responses

I'm trying to take an anonymous survey of peeps first language.

 

here is the survey

Spoiler

class AnonymousSurvey():
    """Collect anonymous answers to a survery question."""
    def __init__(self, question):
        """Store a question, and prepare to store responses."""
        self.question = question
        self.responses = []
        
    def show_question(self):
        """Show the survery 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):
        """Store a single response to the survey"""
        print("Survey results:")
        for response in responses:
            print('-' + response)

 

and here is the language_survey

Spoiler

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")
while True:
	response = input("Language: ")
	if response == 'q':
		break
	my_survey.store_response(response)

#show the results
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()

 

my problem is here in the language_survey: the error said question isnt defined.

Traceback (most recent call last):
  File "language_survey.py", line 9, in <module>
    my_survey.show_question()
  File "G:\Python\python_work\files_and_exceptions\survey.py", line 13, in show_question
    print(question)
NameError: name 'question' is not defined

 

Any help would be appreciated, thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

should be 

print(self.question)

because you are accessing a class variable

 

also your docstring for show_results is wrong.

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, RadiatingLight said:

should be 


print(self.question)

because you are accessing a class variable

 

also your docstring for show_results is wrong.

this also applies to show_responses, you need to access self.responses

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

This may be a concept you learn later in coding, but in general it's somewhat clunky to have a class print directly to the console (the whole point of a class is that you want to use it in multiple places, and you might not want to print in all of those cases). Instead, it's better practice to use a return statement, and print outside of the function.

I've made some changes and corrected some syntax errors. Try this on for size:

if you have any questions and/or want to know how to do something in python, HMU.

 

class AnonymousSurvey():
    """Collect anonymous answers to a survery question."""
    def __init__(self, question):
        """Store a question, and prepare to store responses."""
        self.question = question
        self.responses = []
    
    def store_response(self, new_response):
        """Store a single response to the survey"""
        self.responses.append(new_response)
        
    def get_results_printout(self):
        """retuabrns a list of all results"""
        result_string = "Survey Results:"
        for response in self.responses:
            result_string += "\n-" + response  # The \n creates a new line (adds a new line character)
        return result_string

#This should go in a seperate file and you should 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
print(my_survey.question) #you don't need a function to get the question, you can access it directly 
print("Enter 'q' at any time to quit")
while True:
	response = input("?") #It's a bad idea to have "language" here because it's not applicable if you decide to change the question later
	if response.lower() == 'q':
		break
	my_survey.store_response(response)

#show the results
print("\nThank you to everyone who participated in the survey!")
print(my_survey.get_results_printout())

 

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, RadiatingLight said:

This may be a concept you learn later in coding, but in general it's somewhat clunky to have a class print directly to the console (the whole point of a class is that you want to use it in multiple places, and you might not want to print in all of those cases). Instead, it's better practice to use a return statement, and print outside of the function.

I've made some changes and corrected some syntax errors. Try this on for size:

if you have any questions and/or want to know how to do something in python, HMU.

 

ah yes, the questions may change, the function stays. Gotcha

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×