-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
832208f
commit dd9879d
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#to show that the class works, here is a program | ||
from survey import AnonymousSurvey | ||
|
||
question = "What language did you first learn to speak? " | ||
my_survey = AnonymousSurvey(question) | ||
|
||
my_survey.show_question() | ||
print("Press enter to quit.\n") | ||
while True: | ||
response = input("Language: ") | ||
if response == '': | ||
break | ||
my_survey.store_response(response) | ||
|
||
print("\nThank you for participating!") | ||
my_survey.show_results() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#testing classes | ||
#here is a class that helps administer anonymous serveys | ||
class AnonymousSurvey: | ||
|
||
def __init__(self, question): | ||
self.question = question | ||
self.responses = [] | ||
|
||
def show_question(self): | ||
print(self.question) | ||
|
||
def store_response(self, new_response): | ||
self.responses.append(new_response) | ||
|
||
def show_results(self): | ||
print("Survey results:") | ||
for response in self.responses: | ||
print(f"- {response}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import unittest | ||
from survey import AnonymousSurvey | ||
|
||
class TestAnonymousSurvey(unittest.TestCase): | ||
|
||
def test_store_singele_response(self): | ||
question = "What language did you first learn?" | ||
my_survey = AnonymousSurvey(question) | ||
my_survey.store_response('English') | ||
self.assertIn('English', my_survey.responses) | ||
|
||
def test_store_three_responses(self): | ||
question = "What language did you first learn?" | ||
my_survey = AnonymousSurvey(question) | ||
responses = ['English', 'Spanish', 'Python'] | ||
for response in responses: | ||
my_survey.store_response(response) | ||
|
||
for response in responses: | ||
self.assertIn(response, my_survey.responses) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
||
#continue on page 297 |