105k views
0 votes
I am coding a turtle race in python I have to Use a single call to forward for each turtle, using a random number as the distance to move

User NorseGaud
by
4.5k points

2 Answers

5 votes

Answer:

if you mean to set a speed. Im not sure. however, move turtle.forward(random number) i can do

import turtle

import random

random_int = random.randint(20, 1000)

win = turtle.Screen()

turtle_1 = turtle.Turtle()

turtle_1.forward(random_int)

win.mainloop()

User Harsimran Singh
by
5.9k points
2 votes

import turtle,random

s = turtle.getscreen()

t1 = turtle.Turtle()

t2 = turtle.Turtle()

t3 = turtle.Turtle()

t2.penup()

t2.goto(1,100)

t3.penup()

t3.goto(1,200)

while True:

t1.forward(random.randint(1,10))

t2.forward(random.randint(1,10))

t3.forward(random.randint(1,10))

if t1.xcor() >= 180:

print("Turtle 1 won the race!")

quit()

elif t2.xcor()>= 180:

print("Turtle 2 won the race!")

quit()

elif t3.xcor() >= 180:

print("Turtle 3 won the race")

quit()

I hope this helps!

User Nucandrei
by
5.3k points