[Python] Is it possible for a list to contain full 'methods', and use random.choice to choose...

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a simple random walk using turtle graphics, and in my attempt to create a function that turns the turtle in a random direction, the turtle would turn only 180 or 0 degrees, resulting in a back and forth motion.

    def random_direction(obj):
    directions = [obj.left(0) , obj.left(90) , obj.left(180) , obj.left(270)]
    random.choice(directions)


    On the other hand, the following returned a proper random walk:

    def random_direction(obj):
    directions = [0 , 90 , 180 , 270]
    obj.left(random.choice(directions))

    Continue reading...

Compartilhe esta Página