这个可以用来制作问卷调查,需要自己再将里面一些东西给改一下,看懂需要一定的基础
import unittest
class AnonymousSurvey():
"""收集匿名调查问卷的答案"""
def __init__(self, question):
"""存储一个问题,并为存储答案做准备"""
self.question = question
self.responses = []
def show_question(self):
"""显示调查问卷"""
print(question)
def store_response(self, new_response):
"""存储单份调查答卷"""
self.responses.append(new_response)
def show_results(self):
"""显示收集到的所有答卷"""
print("Survey results:")
for response in responses:
print('- ' + response)
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
#显示问题并存储答案
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
responses = input("Language: ")
if responses == 'q':
break
else:
my_survey.store_response(responses)
# 显示调查结果
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()
#这个就是开始检测的
class TestAnonmyousSurvey(unittest.TestCase):
def test_store_single_response(self):
"""测试单个答案会被妥善地存储"""
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
#my_survey = AnonymousSurvey()
my_survey.store_response('English')
self.assertIn('English', my_survey.responses)
unittest.main()
|