I am trying to implement a random walk in python. This is the error I get. I feel my implementation is wrong or at least not the best. Can someone have a look at it. Keep in mind I am a beginner in python and this is how I think someone would code something, so I can be totally off.
in randomWalk(stepSize, stepNumber)
37 for _ in range(stepNumber):
38 r = randint(1,4)
---> 39 x,y = movement[r]
40 xList.append(x)
41 yList.append(y)
TypeError: 'function' object is not iterable
This is my code
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import random as rnd
matplotlib.rcParams.update({'font.size': 20})
x = 0.
y = 0.
xList = []
yList = []
def goRight(stepSize, y):
direction = np.cos(0)
x = stepSize*direction
return [x,y]
def goUp(stepSize, x):
direction = np.cos(90)
y = stepSize*direction
return [x,y]
def goLeft(stepSize, y):
direction = np.cos(180)
x = stepSize*direction
return [x,y]
def goDown(stepSize, x):
direction = np.cos(270)
y = stepSize*direction
return [x,y]
def randomWalk(stepSize, stepNumber):
movement = {1: goRight,
2: goUp,
3: goLeft,
4: goDown}
for _ in range(stepNumber):
r = randint(1,4)
x,y = movement[r]
xList.append(x)
yList.append(y)
plt.ioff()
plot(x, y)
plt.show()
randomWalk(1.,4)
Aucun commentaire:
Enregistrer un commentaire