I am trying to write some code that prints some shapes in the turtle module. Here is my code:
#imports
`import random`
#The functions that are used later.
shape = turtle.Pen()
def spiraling_shapes():
for spiral in range(1,2):
shape.forward(20)
shape.left(shape_sides)
def rotating_shapes():
for rotating in range(1,2):
shape.left(20)
for shape_image in range(1,2):
shape.forward(50)
shape.left(shape_sides)
#First loop. This will generate a random number
for shape in range (1,6):
number_of_sides = random.randint(3,8)
shape_sides = 360 / number_of_sides
#This will check if it is even or odd and run the correct function accordingly
if number_of_sides % 2 == 0:
spiraling_shapes()
else:
rotating_shapes()
''' It keeps running shape.forward(20) AttributeError: 'int' object has no attribute 'forward'
You need to be careful depending on global variables because it is easy to accidentally overwrite or modify them, which leads to difficult bugs. This is why you'll see people avoid that or name things in all caps for constants.
Here you use shape
shape = turtle.Pen()
Later, however, you reassign it:
for shape in range (1,6):
So now shape is an integer, not a Pen and causes an error you call the Pen.forward()
method.
Try renaming your one of the variables for a quick fix.