我正在尝试编写一个名为randomTurtle()的函数:
然后对于每个重复的序列): 生成1到50之间的随机步数(包括1和50)。
将乌龟向前移动该数量。
选择0到1之间的随机浮点数。如果该值小于0.5,则将乌龟向右转。如果该值大于或等于0.5,则将乌龟向左转
答案 0 :(得分:2)
from random import random
moves = randint(0, 101)
for i in xrange(moves):
steps = randint(1, 51)
moveTurtle(direction, steps)
spin = random()
if spin < 0.5:
direction = turn_right(direction)
else:
direction = turn_left(direction)
def turn_left(direction):
#returns your new direction if you start facing 'direction' and turn left
def turn_right(direction):
#returns your new direction if you start facing 'direction' and turn right
def moveTurtle(direction, steps)
#moves turtle in 'direction' by 'steps'