Doubt in the book Python on a Crash Course

Asked

Viewed 74 times

1

I’m studying the book Python on a Crash Course And everything was going great until now. After testing the codes on pages 324 and 325 (which are a module and a program that uses it), I was surprised that nothing works.

What to do to fix this code and make it work?

Here is the module survey.py:

class AnonymousSurvey():

    def __init__(self, question): #Aqui Esta em vermelho(problema)
        #Armazena uma pergunta e se prepara para armazenar asrespostas.
        self.question = question
        self.responses = []

    def show_question(self):
        #Mostra a pergunta da pesquisa.
        print(question) #Aqui também esta (aparente problema)

    def store_response(self, new_response):
        #Armazena uma única resposta da pesquisa.
        self.responses.append(new_response)

    def show_results(self):
        #Mostra todas as respostas dadas.
        print("Survey results:")
        for response in responses:
            print('- ' + response)

And here’s the little program that uses the module above:

from Modulo_Survey import AnonymousSurvey
# Define uma pergunta e cria uma pesquisa
question = 'What language did you first learn to speak?'
my_survey = AnonymousSurvey(question)


# Mostra a pergunta e armazena as respostas à pergunta
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)


# Exibe os resultados da pesquisa
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()
  • What should I do to make it work??

  • 1

    I recommend studying a little more about object orientation, what’s wrong with your code are just object orientation concepts, as you said is a study material

1 answer

4


On the fifth and sixth line they declare self.question and self.responses. However, when referencing them later, on lines 10 and 19, they use question and responses. Just exchange them for self.question and self.responses. In addition, the my_survey.store_response(response) has to be indented. Here’s the corrected code:

Filing cabinet survey.py:

class AnonymousSurvey():

    def __init__(self, question):
        """Store a question, and prepare to store responses."""
        self.question = question
        self.responses = []

    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('- ' + response)

Filing cabinet 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()
  • 2

    and the my_survey.store_response(response) certainly would also be to be within the while but I suspect that the AP was careless in trying to reproduce the code

  • 1

    @Isac My book doesn’t have that mistake of while. Could have been in the formatting when posting the question.

  • Thank you very much !!!!! : ) :0 My God in heaven , I would never find that mistake. Thank you.

  • 1

    @Namelessman These errors are key object-oriented programming details that I recommend you to review before continuing through these book exercises.

  • 1

    @Namelessman Stay tuned for what @Isac said. That my_survey.store_response(response) has to be indented inside the code. I will edit the answer with all fixed, both files.

  • Thanks Rafael, I read after only. I set here, it worked.

  • 1

    I corrected anyway. The comments are in English because it was my version of the book.

  • The book in English Is Not Wrong ??

  • 1

    It is. I corrected it.

  • 3

    Just to stay here too, sometimes mistakes have books, happens because we are human. Many of them even end up building an Errata precisely so that a future reader can realize where they are and quickly correct or know what is not right.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.