Question: this code draws a bear made of circles # by reading data from a file to get the parameters for the circle function. Will you

this code draws a bear made of circles
# by reading data from a file to get the parameters for the circle function.
Will you please help me find the error in my code?
# move the pen without leaving a line
def relocate(turt, x, y):
turt.penup()
turt.setx(x)
turt.sety(y)
turt.setheading(90)
turt.pendown()
# draw a circle at the specified position
def circle(turt, x, y, radius, color):
SHIFT_X =40
SHIFT_Y =30
SCALE =5
x = x - SHIFT_X
y = y - SHIFT_Y
x = x * SCALE
y = y * SCALE
radius = radius * SCALE
relocate(turt, x, y)
turt.color(color, color)
turt.begin_fill()
turt.circle(radius)
turt.end_fill()
# draw crosshairs
def crosshairs(turt, n):
relocate(turt,0- n/2,0)
turt.setheading(0)
turt.forward(n)
relocate(turt,0,0- n/2)
turt.setheading(90)
turt.forward(n)
def main():
import turtle
wn = turtle.Screen()
wn.bgcolor("gray88")
t = turtle.Turtle()
t.width(2)
t.speed(7)
t.hideturtle()
crosshairs(t,400)
try:
with open("circles.txt","r") as file:
for line in file:
data = line.strip().split()
x, y, radius, color = map(float, data)
circle(t, x, y, radius, color)
except FileNotFoundError:
print("Error: File 'circles.txt' not found.")
except ValueError:
print("Error: Invalid data format in 'circles.txt'.")
try:
print("Close the window when done viewing.")
turtle.done()
except:
print("Done.")
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!