Search code examples
pythonpython-turtle

I can't figure out why my circles are being placed in a diagonal line


I am currently working on a piece of python turtle code that requires me to randomly place circles within my square. For some reason, they are always placed in a diagonal line and do not get drawn randomly.

import random
import turtle

for i in range(15):
    # draws 15 circles and then stops
    random_position = random.randint(-80,90)
    
    # allows the computer to pick any number between -80 and 90 as an x or y co-ordinate
    pit_x = random_position 
    pit_y = random_position 
    pit_radius = 7
      
    # radius of circle
    turtle.penup()
    turtle.setposition(pit_x, pit_y-pit_radius)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(pit_radius)
    turtle.end_fill()
    turtle.hideturtle()
     
    # the circle is now a black hole

This is how it appears:

result

How can I fix this?


Solution

  • Maybe use

    pit_x = random.randint(-80,90)
    pit_y = random.randint(-80,90)
    

    Or if you want a function (I think you wanted to do random_pos for each one) Just

    def randomPitNumber():
      return random.randint(-80, 90)
    

    Then

        pit_x = randomPitNumber()
        pit_y = randomPitNumber()
    

    And remove random_position Variable.

    Tip: To help correct answers that some people answer, press the check mark beside it, so you mark it for other people as the right answer.