I relatively new to the world of Python, and over the past few months I have been going over the exercises in Python Crash Course. I am stuck on the building of a class that takes answers for an anonymous survey. One of the methods of the class is store_response(self, response), which takes the response on the survey and appends it to a list of responses. However, when I create a variable 'response' and add it as the argument for the function, it gives me the error "store_response() takes 1 positional argument but 2 were given. I really don't understand why that is the case if clearly have only given the function 1 argument (response) and not two. For more context, this is the "A class to Test" example of the 2nd edition Python Crash Course page 217. Thanks!
#Consider a class that helps administer anonymous surveys
class AnonymousSurvey:
"""Collects anonymous answers to survey questions"""
def __init__(self, question):
"""Store a question, and prepare to store responses."""
self.question = question
self.response = [] #An extra attribute that takes in the response and places it as a string in a list.
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(f" -{response}")
# I want the survey to run for about a minute
import time
my_survey.show_question()
counter = 0
time_duration = 60
start_time = time.time()
while time.time() < start_time + time_duration:
counter += 1
response = input("Language: ")
my_survey.store_response(response)
#Show the results of your survey
print("\nThank you to everyone who participated in the survey.")
my_survey.show_results()
TypeError Traceback (most recent call last)
<ipython-input-29-2f6d440992b8> in <module>
22 counter += 1
23 response = input("Language: ")
---> 24 my_survey.store_response(response)
25
26 #Show the results of your survey
TypeError: store_response() takes 1 positional argument but 2 were given
I have looked back to make sure all my variables are defined and to make sure the code here matches that of the book, but I have not been able to find my mistake.