0

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.

  • Your error includes ``, which indicates you're working in a notebook. Notebooks can be good for a variety of purposes, but only if you understand how they work. The mix of code and text cells is nice, but it also complicates the execution flow. For example, if you have a couple cells that define `store_response()`, you might not be using the version of the function that you think you are. Similarly, you don't show the code where you defined `my_survey`. If you defined that in a couple cells, you may not be using the object you think you are. – japhyr Dec 30 '22 at 16:42
  • I took the code you showed and pasted it into a single *.py* file. I added two lines to define a question and a survey, called `my_survey`. When I ran your code, it complained that `self.responses` wasn't defined. The `__init__()` method you showed defines `self.response`, where it should be `self.responses`. Fixing that makes the program work, although the use of `time` instead of a quit flag complicates execution as well. The fact that you aren't showing the `self.responses` error indicates that you have a couple versions of `AnonymousSurvey` in your notebook. – japhyr Dec 30 '22 at 16:45
  • I would suggest working in *.py* files instead of notebooks. Or, if you want to stay in the notebook world, make a new notebook for each exercise until you're more confident in not creating cells that conflict with each other's execution. – japhyr Dec 30 '22 at 16:47

0 Answers0