Skip to content

Commit 7580163

Browse files
committed
Chapter End
1 parent 9e0854b commit 7580163

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from survey import AnonymousSurvey
2+
3+
question = "What language did you first learn to speak ? "
4+
my_survey = AnonymousSurvey(question)
5+
6+
my_survey.show_question()
7+
print("Enter 'q' to quit it.")
8+
9+
while True:
10+
response = input("Language: ")
11+
if response == 'q':
12+
break
13+
my_survey.store_response(response)
14+
15+
print("Thank you for participating in the survey ")
16+
my_survey.show_results()

11. Testing Your Code/survey.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ class AnonymousSurvey:
44
def __init__(self, question):
55
""" Store a qiestion and prepare to store the response """
66
self.question = question
7-
self.response = []
8-
7+
self.responses = []
8+
99
def show_question(self):
1010
""" to show the question of the survey """
1111
print(self.question)
1212

13+
def store_response(self, new_response):
14+
""" storing the responses from the input """
15+
self.responses.append(new_response)
1316

14-
17+
def show_results(self):
18+
""" Showing the result """
19+
print("Survey results: ")
20+
for response in self.responses:
21+
print(f"- {response}")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from survey import AnonymousSurvey
3+
4+
5+
class TestAnonymousSurvey(unittest.TestCase):
6+
"""Tests for the class Anonymous Survey"""
7+
8+
def setUp(self):
9+
""" set up survey and responses for further uses """
10+
question = "What language did you first learn to speak ?"
11+
self.my_survey = AnonymousSurvey(question)
12+
self.responses = ['English', 'Spanish', 'Mandarin']
13+
14+
def test_store_single_responses(self):
15+
""" Single response testing """
16+
self.my_survey.store_response(self.responses[0])
17+
self.assertIn(self.responses[0], self.my_survey.responses)
18+
19+
def test_store_triple_responses(self):
20+
""" Triple response testing """
21+
for response in self.responses:
22+
self.my_survey.store_response(response)
23+
self.assertIn(response, self.my_survey.responses)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

0 commit comments

Comments
 (0)