diff --git a/base/language_survey.py b/base/language_survey.py index 326ede30e696e74bb7e7fbc384bb8b4b8da9426c..cdf2e30ff11e1bf343fe0f21911757ac9a86e51f 100644 --- a/base/language_survey.py +++ b/base/language_survey.py @@ -8,10 +8,10 @@ my_survey = AnonymousSurvey(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) + response = input("language: ") + if response == 'q': + break + my_survey.store_response(response) # 显示调查结果 print("\nThank you to everyone who participated in the survey!") diff --git a/base/test_survey.py b/base/test_survey.py index bef589b2d9c4cc3dfc30c3f12e25e898d3e90d5d..9d8be3ccf262d57adb6006daaa541d489cc8d63a 100644 --- a/base/test_survey.py +++ b/base/test_survey.py @@ -2,44 +2,44 @@ import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): - """针对AnonymousSurvey的测试""" - - # def test_store_single_response(self): - # """测试单个答案会被妥善的存储""" - # question = "What language did you first learn to speak?" - # 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 to speak?" - # my_survey = AnonymousSurvey(question) - # responses = ['English', 'Spanish', 'Mandarin'] - # for response in responses: - # my_survey.store_response(response) - - # for response in responses: - # self.assertIn(response, my_survey.responses) - - def setUp(self): - """创建一个调查对象和一组答案, 供使用的测试方法使用""" - question = "What language did you first learn to speak?" - self.my_survey = AnonymousSurvey(question) - self.responses = ['English', 'Spanish', 'Mandarin'] - - def test_store_single_response(self): - """测试单个答案会被妥善的存储""" - self.my_survey.store_response(self.responses[0]) - self.assertIn(self.responses[0], self.my_survey.responses) - - def test_store_three_responses(self): - """测试三个答案会被妥善地存储""" - for response in self.responses: - self.my_survey.store_response(response) - - for response in self.responses: - self.assertIn(response, self.my_survey.responses) + """针对AnonymousSurvey的测试""" + + # def test_store_single_response(self): + # """测试单个答案会被妥善的存储""" + # question = "What language did you first learn to speak?" + # 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 to speak?" +# my_survey = AnonymousSurvey(question) +# responses = ['English', 'Spanish', 'Mandarin'] +# for response in responses: +# my_survey.store_response(response) +# +# for response in responses: +# self.assertIn(response, my_survey.responses) + +def setUp(self): + """创建一个调查对象和一组答案, 供使用的测试方法使用""" + question = "What language did you first learn to speak?" + self.my_survey = AnonymousSurvey(question) + self.responses = ['English', 'Spanish', 'Mandarin'] + +def test_store_single_response(self): + """测试单个答案会被妥善的存储""" + self.my_survey.store_response(self.responses[0]) + self.assertIn(self.responses[0], self.my_survey.responses) + +def test_store_three_responses(self): + """测试三个答案会被妥善地存储""" + for response in self.responses: + self.my_survey.store_response(response) + + for response in self.responses: + self.assertIn(response, self.my_survey.responses) unittest.main() \ No newline at end of file diff --git a/matplotlib/random_walk.py b/matplotlib/random_walk.py index 02de3cdc4e6370842739b4ee454a1bb119580990..70312a3cf52eb789dd442632db897c39c71e055e 100644 --- a/matplotlib/random_walk.py +++ b/matplotlib/random_walk.py @@ -2,40 +2,40 @@ from random import choice class RandomWalk: - """一个生成随机漫步数据的类""" - - def __init__(self, num_points=5000): - """初始化随机漫步的属性""" - self.num_points = num_points - - # 所有随机漫步都始于(0,0) - self.x_values = [0] - self.y_values = [0] - - def fill_walk(self): - """计算随机漫步包含的所有点""" - - # 不断漫步,知道列表达到指定的长度 - while len(self.x_values) < self.num_points: - # choice() 随机选择列表中的一个数 - # 决定前进方向以及沿这个方向前进的距离 - # x 的方向 - x_direction = choice([1, -1]) - # x 的距离 - x_distance = choice([0, 1, 2, 3, 4]) - x_step = x_direction * x_distance - - y_direction = choice([1, -1]) - y_distance = choice([0, 1, 2, 3, 4]) - y_step = y_direction * y_distance - - # 拒绝原地踏步 - if x_step == 0 and y_step == 0: - continue - - # 计算下一个点的x和y值 - next_x = self.x_values[-1] + x_step - next_y = self.y_values[-1] + y_step - - self.x_values.append(next_x) - self.y_values.append(next_y) + """一个生成随机漫步数据的类""" + + def __init__(self, num_points=5000): + """初始化随机漫步的属性""" + self.num_points = num_points + + # 所有随机漫步都始于(0,0) + self.x_values = [0] + self.y_values = [0] + + def fill_walk(self): + """计算随机漫步包含的所有点""" + + # 不断漫步,知道列表达到指定的长度 + while len(self.x_values) < self.num_points: + # choice() 随机选择列表中的一个数 + # 决定前进方向以及沿这个方向前进的距离 + # x 的方向 + x_direction = choice([1, -1]) + # x 的距离 + x_distance = choice([0, 1, 2, 3, 4]) + x_step = x_direction * x_distance + + y_direction = choice([1, -1]) + y_distance = choice([0, 1, 2, 3, 4]) + y_step = y_direction * y_distance + + # 拒绝原地踏步 + if x_step == 0 and y_step == 0: + continue + + # 计算下一个点的x和y值 + next_x = self.x_values[-1] + x_step + next_y = self.y_values[-1] + y_step + + self.x_values.append(next_x) + self.y_values.append(next_y) diff --git a/matplotlib/rw_visual.py b/matplotlib/rw_visual.py index 2c90787b0f3fa9aa46ebc958245d3ad7cfea20ee..0ccb9c16d36a6a99d8dfece2f178a862fae3c419 100644 --- a/matplotlib/rw_visual.py +++ b/matplotlib/rw_visual.py @@ -4,27 +4,27 @@ from random_walk import RandomWalk # 只要程序处于活动状态,就不断地模拟随机漫步 while True: - # 创建一个RandomWalk实例,并将其包含的点都绘制出来 - rw = RandomWalk(50000) - rw.fill_walk() + # 创建一个RandomWalk实例,并将其包含的点都绘制出来 + rw = RandomWalk(50000) + rw.fill_walk() - # 设置绘图窗口的尺寸 - plt.figure(dpi=256, figsize=(10, 6)) + # 设置绘图窗口的尺寸 + plt.figure(dpi=256, figsize=(10, 6)) - point_numbers = list(range(rw.num_points)) - # 给点着色看出漫步的轨迹 - plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=1) + point_numbers = list(range(rw.num_points)) + # 给点着色看出漫步的轨迹 + plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=1) - # 突出起点和终点 - plt.scatter(0, 0, c='green', edgecolor='none', s=100) - plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=100) + # 突出起点和终点 + plt.scatter(0, 0, c='green', edgecolor='none', s=100) + plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=100) - # 隐藏坐标轴 - plt.axes().get_xaxis().set_visible(False) - plt.axes().get_yaxis().set_visible(False) + # 隐藏坐标轴 + plt.axes().get_xaxis().set_visible(False) + plt.axes().get_yaxis().set_visible(False) - plt.show() + plt.show() - keep_running = input("是否再模拟一次随机漫步?(y/n):") - if keep_running == 'n': - break + keep_running = input("是否再模拟一次随机漫步?(y/n):") + if keep_running == 'n': + break