Search code examples
pythonattributeerrorpython-turtle

AttributeError: 'function' object has no attribute 'penup'


I have written the following code:

import turtle
from turtle import penup

a = turtle.Turtle(visible=False)
a.speed(0)
b = turtle.Turtle(visible=False)
b.speed(0)
e = turtle.Turtle(visible=False)
e.speed(0)
f = turtle.Turtle(visible=False)
f.speed(0)
pedlightone = turtle.Turtle(visible=False)
pedlightone.speed(0)
pedlighttwo = turtle.Turtle(visible=False)
pedlighttwo.speed(0)
horizontalsplit = turtle.Turtle()
horizontalsplit.speed(0)
verticlesplit = turtle.Turtle()
verticlesplit.speed(0)

def a_movement():
  a.penup()
  a.goto(-475,25)
  a.pendown()
  
  a.goto(-25,25)
  a.left(90) 
  a.forward(400)
  a.backward(400)

def b_movement():
  b.penup()
  b.goto(-475,-25)
  b.pendown()

  b.goto(-25,-25)
  b.right(90)
  b.forward(400)
  b.backward(400)

def e_movement():
  e.penup()
  e.goto(475,25)
  e.pendown()

  e.goto(25,25)
  e.left(90)
  e.forward(400)
  e.backward(400)

def f_movement():
  f.penup()
  f.goto(475,-25)
  f.pendown()

  f.goto(25,-25)
  f.right(90)
  f.forward(400)
  f.backward(400)

def horizontal_split():
  horizontalsplit.penup()
  horizontalsplit.goto(-475,0)
  horizontalsplit.pendown()
  
  horizontalsplit.goto(475,0)

def verticle_split():
  verticlesplit.penup()
  verticlesplit.goto(0,400)
  verticlesplit.pendown()

  verticlesplit.goto(0,-400)

def pedlightone():
  pedlightone.penup()
  pedlightone.goto(-25,25)
  pedlightone.pendown()
  pedlightone.right(45)
  for i in range(8):
    pedlightone.forward(5)
    pedlightone.penup()
    pedlightone.forward(5)
    pedlightone.pendown()

def pedlighttwo():
  pedlighttwo.penup()
  pedlighttwo.goto(25,25)
  pedlightone.pendown()


  pedlighttwo.left(45)
  for i in range(8):
    pedlighttwo.forward(5)
    pedlighttwo.penup()
    pedlighttwo.forward(5)

    pedlighttwo.pendown

def roaddiagram():
  a_movement()
  b_movement()
  e_movement()
  f_movement()
  horizontal_split()
  verticle_split()
  pedlightone()
  pedlighttwo()

roaddiagram()

When I run this, it draws a cross with a line in between, which is what I want it to do.

However, I get an attribute error that says that 'function' has no attribute 'penup' The exact error is: AttributeError: 'function' object has no attribute 'penup'

Please could someone help me out and point out where I went wrong.

This had happened before but it resolved itself without me needing to do anything.

Thank you


Solution

  • I don't usually advocate using fewer functions, but in this case everything you've done is really just initialization, and this avoids the name conflicts:

    import turtle
    from turtle import penup
    
    a = turtle.Turtle(visible=False)
    a.speed(0)
    b = turtle.Turtle(visible=False)
    b.speed(0)
    e = turtle.Turtle(visible=False)
    e.speed(0)
    f = turtle.Turtle(visible=False)
    f.speed(0)
    pedlightone = turtle.Turtle(visible=False)
    pedlightone.speed(0)
    pedlighttwo = turtle.Turtle(visible=False)
    pedlighttwo.speed(0)
    horizontalsplit = turtle.Turtle()
    horizontalsplit.speed(0)
    verticlesplit = turtle.Turtle()
    verticlesplit.speed(0)
    
    def roaddiagram():
      a.penup()
      a.goto(-475,25)
      a.pendown()
      
      a.goto(-25,25)
      a.left(90) 
      a.forward(400)
      a.backward(400)
    
      b.penup()
      b.goto(-475,-25)
      b.pendown()
    
      b.goto(-25,-25)
      b.right(90)
      b.forward(400)
      b.backward(400)
    
      e.penup()
      e.goto(475,25)
      e.pendown()
    
      e.goto(25,25)
      e.left(90)
      e.forward(400)
      e.backward(400)
    
      f.penup()
      f.goto(475,-25)
      f.pendown()
    
      f.goto(25,-25)
      f.right(90)
      f.forward(400)
      f.backward(400)
    
      horizontalsplit.penup()
      horizontalsplit.goto(-475,0)
      horizontalsplit.pendown()
      
      horizontalsplit.goto(475,0)
    
      verticlesplit.penup()
      verticlesplit.goto(0,400)
      verticlesplit.pendown()
    
      verticlesplit.goto(0,-400)
    
      pedlightone.penup()
      pedlightone.goto(-25,25)
      pedlightone.pendown()
      pedlightone.right(45)
      for i in range(8):
        pedlightone.forward(5)
        pedlightone.penup()
        pedlightone.forward(5)
        pedlightone.pendown()
    
      pedlighttwo.penup()
      pedlighttwo.goto(25,25)
      pedlightone.pendown()
    
      pedlighttwo.left(45)
      for i in range(8):
        pedlighttwo.forward(5)
        pedlighttwo.penup()
        pedlighttwo.forward(5)
    
        pedlighttwo.pendown
    
    roaddiagram()
    

    For that matter, you don't really need 8 different turtles. These are all independent. You'd want to add some comments, but this does the same thing:

    import turtle
    
    a = turtle.Turtle(visible=False)
    a.speed(0)
    
    def roaddiagram():
      a.penup()
      a.goto(-475,25)
      a.pendown()
      a.setheading(0)
      a.forward(450)
      a.left(90) 
      a.forward(400)
    
      a.penup()
      a.goto(-475,-25)
      a.pendown()
      a.setheading(0)
      a.forward(450)
      a.right(90)
      a.forward(400)
    
      a.penup()
      a.goto(475,25)
      a.pendown()
      a.setheading(180)
      a.forward(450)
      a.right(90)
      a.forward(400)
    
      a.penup()
      a.goto(475,-25)
      a.pendown()
      a.setheading(180)
      a.forward(450)
      a.left(90)
      a.forward(400)
    
      a.penup()
      a.goto(-475,0)
      a.pendown()
      a.goto(475,0)
      a.penup()
      a.goto(0,425)
      a.pendown()
      a.goto(0,-425)
    
      a.penup()
      a.goto(-25,25)
      a.pendown()
      a.setheading(315)
      for i in range(8):
        a.pendown()
        a.forward(5)
        a.penup()
        a.forward(5)
    
      a.penup()
      a.goto(25,25)
      a.setheading(225)
      for i in range(8):
        a.pendown()
        a.forward(5)
        a.penup()
        a.forward(5)
    
    roaddiagram()