I am trying to create a random triangle in python turtle with a for loop. I figured it out how to complete the task without a for loop, but I was instructed to use a for loop. I am going to add my code that isn’t working. I need explanations and examples if that is possible. I what to understand . range is (-r,r) and the starts at the origin. Thank you:
import turtle
import random turtle.penup() turtle.color('red')
def median(r):
L = [] for i in range (3):
L.append(randint(-r,r),randint(-r,r))
forward(r)
return L
turtle.pendown()
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Just an update on an the question,
It seems like you’re trying to create a function median() that generates three random points within a range (-r, r) and have the turtle draw lines between these points, thereby creating a random triangle.
Your current approach has several issues. Here is a step-by-step fix for your function, along with explanations:
randint from the random module. Also, turtle needs to be imported as a standalone module, not as a function.import turtle
import random
randint() function, which generates a random integer, directly inside L.append(). However, you are missing random. before randint(). Moreover, you are trying to append two elements at once, which is not possible with .append(). To append two values as a pair (representing coordinates), you should use a tuple.Instead of:
L.append(randint(-r,r),randint(-r,r))
Use:
L.append((random.randint(-r,r), random.randint(-r,r)))
forward() is a turtle method and you are trying to use it without reference to the turtle object. It’s unclear what you’re trying to achieve with this function call in this context. Maybe you are trying to move the turtle forward by a distance of r. If that’s the case, it should be turtle.forward(r).
Your function median() is defined to return L, which is a list of three random points. However, it’s unclear what you’re doing with this return value. If you want to draw a triangle using these points, you should include the drawing commands inside your function or in another part of your code.
Here’s a corrected and completed version of your code that creates a random triangle using a for loop:
import turtle
import random
turtle.penup()
turtle.color('red')
def create_random_triangle(r):
points = []
for i in range(3):
points.append((random.randint(-r, r), random.randint(-r, r)))
# Now let's draw the triangle
for point in points:
turtle.goto(point)
turtle.pendown()
# Closing the triangle by going back to the first point
turtle.goto(points[0])
turtle.penup()
# Let's create a triangle with range (-100,100)
create_random_triangle(100)
turtle.done()
This script generates three random points within a range (-r, r), moves the turtle to these points in order, and draws lines between them, creating a random triangle.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.