\n \u65b9\u6cd5<\/strong><\/p> <\/td> \n \u7528\u9014<\/strong><\/p> <\/td> \n <\/tr> \n \n assertEqual(a, b)<\/p> <\/td> \n | \u6838\u5b9ea == b<\/p> <\/td> \n <\/tr> \n | \n assertNotEqual(a, b)<\/p> <\/td> \n | \u6838\u5b9ea != b<\/p> <\/td> \n <\/tr> \n | \n assertTrue(x)<\/p> <\/td> \n | \u6838\u5b9ex\u4e3aTrue<\/p> <\/td> \n <\/tr> \n | \n assertFalse(x)<\/p> <\/td> \n | \u6838\u5b9ex\u4e3aFalse<\/p> <\/td> \n <\/tr> \n | \n asseertIn(item, list)<\/p> <\/td> \n | \u6838\u5b9eitem\u5728list\u4e2d<\/p> <\/td> \n <\/tr> \n | \n assertNotIn(item, list)<\/p> <\/td> \n | \u6838\u5b9eitem\u4e0d\u5728list\u4e2d<\/p> <\/td> \n <\/tr> \n <\/tbody> \n <\/table> \n 2.\u4e00\u4e2a\u8981\u6d4b\u8bd5\u7684\u7c7b<\/h4> \n \u3000\u3000\u9996\u5148\u7f16\u5199\u4e00\u4e2a\u7c7b\uff1a<\/p> \n \n survey.py<\/span><\/pre> \n \n class<\/span> AnonymousSurvey:\r\n <\/span>"""<\/span>\u6536\u96c6\u533f\u540d\u8c03\u67e5\u95ee\u5377\u7684\u7b54\u6848<\/span>"""<\/span>\r\n\r\n def<\/span> __init__<\/span>(self, question):\r\n <\/span>"""<\/span>\u5b58\u50a8\u4e00\u4e2a\u95ee\u9898\uff0c\u5e76\u4e3a\u5b58\u50a8\u7b54\u6848\u505a\u51c6\u5907<\/span>"""<\/span>\r\n self.question <\/span>= question\r\n self.responses <\/span>= []\r\n\r\n <\/span>def<\/span> show_quetion(self):\r\n <\/span>"""<\/span>\u663e\u793a\u8c03\u67e5\u95ee\u5377<\/span>"""<\/span>\r\n print<\/span>(self.question)\r\n\r\n <\/span>def<\/span> store_response(self, new_response):\r\n <\/span>"""<\/span>\u5b58\u50a8\u5355\u4efd\u8c03\u67e5\u7b54\u6848<\/span>"""<\/span>\r\n self.responses.append(new_response)\r\n\r\n <\/span>def<\/span> show_result(self):\r\n <\/span>"""<\/span>\u663e\u793a\u6536\u96c6\u5230\u7684\u6240\u6709\u7b54\u5377<\/span>"""<\/span>\r\n print<\/span>("<\/span>Survey result:<\/span>"<\/span>)\r\n <\/span>for<\/span> response in<\/span> self.responses:\r\n <\/span>print<\/span>('<\/span>- <\/span>'<\/span> + response)<\/pre> \n <\/div> \n \u3000\u3000\u4e3a\u8bc1\u660eAnonymousSurvey\u7c7b\u53ef\u4ee5\u6b63\u786e\u7684\u5de5\u4f5c\uff0c\u7f16\u5199\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u7a0b\u5e8f\uff1a<\/p> \n \n language_survey.py<\/span><\/pre> \n \n from<\/span> survey import<\/span> AnonymousSurvey\r\n\r\n<\/span>#<\/span> \u5b9a\u4e49\u4e00\u4e2a\u95ee\u9898\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u8c03\u67e5\u7684AnonymousSurvey\u5bf9\u8c61<\/span>\r\nquestion = "<\/span>What language did you first learn to speak?<\/span>"<\/span>\r\nmy_survey <\/span>= AnonymousSurvey(question)\r\n\r\n<\/span>#<\/span> \u663e\u793a\u5e76\u5b58\u50a8\u95ee\u9898\u7684\u7b54\u6848<\/span>\r\nmy_survey.show_quetion()\r\n<\/span>print<\/span>("<\/span>Enter 'q' at any time to quit.\\n<\/span>"<\/span>)\r\n<\/span>while<\/span> True:\r\n response <\/span>= input("<\/span>Language: <\/span>"<\/span>)\r\n <\/span>if<\/span> response == '<\/span>q<\/span>'<\/span>:\r\n <\/span>break<\/span>\r\n my_survey.store_response(response)\r\n\r\n<\/span>#<\/span> \u663e\u793a\u8c03\u67e5\u7ed3\u679c<\/span>\r\nprint<\/span>("<\/span>\\nThank you to everyone who participated in survey!<\/span>"<\/span>)\r\nmy_survey.show_result()<\/span><\/pre> \n <\/div> \n \u3000\u3000\u8fd0\u884c\u7ed3\u679c\uff1a<\/p> \n \n What language did you first learn to speak?\r\nEnter <\/span>'<\/span>q<\/span>'<\/span> at any time to quit.\r\n\r\nLanguage: English\r\nLanguage: Spanish\r\nLanguage: English\r\nLanguage: Mandarin\r\nLanguage: q\r\n\r\nThank you to everyone who participated <\/span>in<\/span> survey!\r\nSurvey result:\r\n<\/span>- English\r\n<\/span>- Spanish\r\n<\/span>- English\r\n<\/span>- Mandarin<\/pre> \n <\/div> \n 3.\u6d4b\u8bd5AnonymousSurvey\u7c7b<\/h4> \n \u3000\u3000\u5bf9AnonymousSurvey\u7c7b\u884c\u4e3a\u7684\u4e00\u4e2a\u65b9\u9762\u8fdb\u884c\u9a8c\u8bc1\uff1a\u5982\u679c\u7528\u6237\u9762\u5bf9\u8c03\u67e5\u95ee\u9898\u65f6\u53ea\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b54\u6848\uff0c\u8fd9\u4e2a\u7b54\u6848\u4e5f\u80fd\u88ab\u59a5\u5584\u5730\u5b58\u50a8\u3002\u4f7f\u7528\u65b9\u6cd5assertIn()\u6765\u6838\u5b9e\u5b83\u5305\u542b\u5728\u7b54\u6848\u5217\u8868\u4e2d\uff1a<\/p> \n \n test_survey.py<\/span><\/pre> \n \n import<\/span> unittest\r\n<\/span>from<\/span> survey import<\/span> AnonymousSurvey\r\n\r\n\r\n<\/span>class<\/span> TestAnonymousSurvey(unittest.TestCase):\r\n <\/span>"""<\/span>\u9488\u5bf9AnonymousSurvey\u7c7b\u7684\u6d4b\u8bd5<\/span>"""<\/span>\r\n\r\n def<\/span> test_store_single_response(self):\r\n <\/span>"""<\/span>\u6d4b\u8bd5\u5355\u4e2a\u7b54\u6848\u4f1a\u88ab\u59a5\u5584\u5730\u5b58\u50a8<\/span>"""<\/span>\r\n question <\/span>= "<\/span>What language did you first learn to speak?<\/span>"<\/span>\r\n my_survey <\/span>= AnonymousSurvey(question)\r\n my_survey.store_response(<\/span>'<\/span>English<\/span>'<\/span>)\r\n\r\n self.assertIn(<\/span>'<\/span>English<\/span>'<\/span>, my_survey.responses)\r\n\r\n\r\n<\/span>if<\/span> __name__<\/span> == "<\/span>__main__<\/span>"<\/span>:\r\n unittest.main()<\/span><\/pre> \n <\/div> \n \u3000\u3000\u8fd0\u884ctest_survey.py\u65f6\uff0c\u6d4b\u8bd5\u901a\u8fc7\u4e86\uff1a<\/p> \n \n Ran 1 test in<\/span> 0.010s\r\n\r\nOK<\/span><\/pre> \n <\/div> \n \u3000\u3000\u53ea\u80fd\u6536\u96c6\u4e00\u4e2a\u7b54\u6848\u7684\u8c03\u67e5\u7528\u9014\u4e0d\u5927\u3002\u4e0b\u9762\u6838\u5b9e\u7528\u6237\u63d0\u4f9b\u4e09\u4e2a\u7b54\u6848\u65f6\uff0c\u5b83\u4eec\u4e5f\u5c06\u88ab\u59a5\u5584\u5730\u5b58\u50a8\u3002\u4e3a\u6b64\uff0c\u5728AnonymousSurvey\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\uff1a<\/p> \n \n import<\/span> unittest\r\n<\/span>from<\/span> survey import<\/span> AnonymousSurvey\r\n\r\n\r\n<\/span>class<\/span> TestAnonymousSurvey(unittest.TestCase):\r\n <\/span>"""<\/span>\u9488\u5bf9AnonymousSurvey\u7c7b\u7684\u6d4b\u8bd5<\/span>"""<\/span>\r\n\r\n def<\/span> test_store_single_response(self):\r\n ...\r\n\r\n <\/span>def<\/span> test_store_three_response(self):\r\n <\/span>"""<\/span>\u6d4b\u8bd5\u4e09\u4e2a\u7b54\u6848\u4f1a\u88ab\u59a5\u5584\u5730\u5b58\u50a8<\/span>"""<\/span>\r\n question <\/span>= "<\/span>What language did you first learn to speak?<\/span>"<\/span>\r\n my_survey <\/span>= AnonymousSurvey(question)\r\n responses <\/span>= ['<\/span>English<\/span>'<\/span>, '<\/span>Spanish<\/span>'<\/span>, '<\/span>Mandarin<\/span>'<\/span>]\r\n <\/span>for<\/span> response in<\/span> responses:\r\n my_survey.store_response(response)\r\n\r\n <\/span>for<\/span> response in<\/span> responses:\r\n self.assertIn(response, my_survey.responses)\r\n\r\n\r\n<\/span>if<\/span> __name__<\/span> == "<\/span>__main__<\/span>"<\/span>:\r\n unittest.main() <\/span><\/pre> \n <\/div> \n \u3000\u3000\u518d\u6b21\u8fd0\u884ctest_survey.py\u65f6\uff0c\u4e24\u4e2a\u6d4b\u8bd5\u90fd\u901a\u8fc7\u4e86\uff1a<\/p> \n \n Ran 2 te","orderid":"0","title":"\u6d4b\u8bd5\u7c7b\u2014\u2014python\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5(\u4e00)","smalltitle":"","mid":"0","fname":"Python","special_id":"0","bak_id":"0","info":"0","hits":"118","pages":"2","comments":"0","posttime":"2019-07-15 22:09:53","list":"1563199793","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u6d4b\u8bd5<\/A> python<\/A> \u7f16\u7a0b<\/A> \u5165\u95e8<\/A> \u5b9e\u8df5<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"47.106.78.186","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u6d4b\u8bd5\u7c7b\u2014\u2014python\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5","lastview":"1703299702","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"} | | |