随机漫步 没搞懂为什么报错
随机漫步 没搞懂为什么报错 from random import choiceclass RandomWalk():
def _init_(self,num_points=5000):
self.num_points=num_points
self.x_values=
self.y_values=
def fill_walk(self):
while len(self.x_values)<self.num_points:
x_direction=choice()
x_distance=choice()
x_step=x_direction*x_distance
y_direction=choice()
y_diostance=choice()
y_step=y_direction*y_distance
if x_step==0 and y_step==0:
continue
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):
这里错了,注意python里面所有的魔法方法都是两个下划线'__'
正确代码如下
from random import choice
class RandomWalk():
def __init__(self,num_points=5000):
self.num_points=num_points
self.x_values=
self.y_values=
def fill_walk(self):
while len(self.x_values)<self.num_points:
x_direction=choice()
x_distance=choice()
x_step=x_direction*x_distance
y_direction=choice()
y_diostance=choice()
y_step=y_direction*y_distance
if x_step==0 and y_step==0:
continue
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) ============= RESTART: /Users/lsy/Documents/pythonlsy/rw_visual.py =============
Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/rw_visual.py", line 4, in <module>
rw.fill_walk()
File "/Users/lsy/Documents/pythonlsy/random_walk.py", line 15, in fill_walk
y_step=y_direction*y_distance
NameError: name 'y_distance' is not defined
>>> 还是不对呀
小甲鱼的铁粉 发表于 2021-1-29 23:59
这里错了,注意python里面所有的魔法方法都是两个下划线'__'
正确代码如下
还是不对呀 已解决
页:
[1]