level 4
贴吧用户_0JMK2AA
楼主
虽然显示属性错误,但是类中明明有这个属性,有点不知为何了,百度过了没有找到答案。下面贴代码。
import matplotlib.pyplot as plt
from random import choice
class RandomWalk:
def _init_(self, points):
self.xvalue = [0]
self.yvalue = [0]
self.points = points
def walk(self):
x_direction = choice([1, -1])
y_direction = choice([1, -1])
x_distance = choice(range(5))
y_distance = choice(range(5))
next_x = x_distance * x_direction
next_y = y_distance * y_direction
self.xvalue.append(next_x)
self.yvalue.append(next_y)
rw = RandomWalk()
rw.points = 100
for i in range(rw.points):
rw.walk()
plt.scatter(rw.xvalue, rw.yvalue, s=15)
plt.show()
2018年09月09日 02点09分
1
import matplotlib.pyplot as plt
from random import choice
class RandomWalk:
def _init_(self, points):
self.xvalue = [0]
self.yvalue = [0]
self.points = points
def walk(self):
x_direction = choice([1, -1])
y_direction = choice([1, -1])
x_distance = choice(range(5))
y_distance = choice(range(5))
next_x = x_distance * x_direction
next_y = y_distance * y_direction
self.xvalue.append(next_x)
self.yvalue.append(next_y)
rw = RandomWalk()
rw.points = 100
for i in range(rw.points):
rw.walk()
plt.scatter(rw.xvalue, rw.yvalue, s=15)
plt.show()