0

I'm trying to run a code that will draw a LOVE image using pycharm but i get an error that i seem unable to fix. Please, i need help with this. what am i doing wrong?

'''
Traceback (most recent call last):
File "/home/itgeekng/PycharmProjects/test/love.py", line 24, in <module>
heart()
File "/home/itgeekng/PycharmProjects/test/love.py", line 8, in heart
pen.fillcolor('red')
File "/usr/lib/python3.8/turtle.py", line 2294, in fillcolor
return self._color(self._fillcolor)
AttributeError: 'str' object has no attribute '_color'
,,,

i get this error while runing the code below in pycharm i don't know what am doing wrong

'''
import turtle
pen = turtle.Turtle
def curve():
for i in range(200):
pen.right(1)
pen.forward(1)
def heart():
pen.fillcolor('red')
pen.begin_fill()
pen.left(140)
pen.forward(113)
curve()
pen.left(120)
curve()
pen.forward(112)
pen.end_fill()

def txt():
pen.up()
pen.setpos(-68, 95)
pen.down()
pen.color('lightgreen')
pen.write('i love you', font=('verdana', 12, 'bold'))
heart()
txt()
pen.ht()
'''
cdlane
  • 40,441
  • 5
  • 32
  • 81
NPeter
  • 1
  • 1

2 Answers2

2

pen = turtle.Turtle assigns the class turtle.Turtle to pen (everything is an object in python, classes too), but what you want is an instance. Thus, it should be:

pen = turtle.Turtle()
frippe
  • 1,329
  • 1
  • 8
  • 15
2

I agree with @frippe's explanation of the problem (+1). But since you're calling it a pen instead of a turtle, I'd use the Pen alias of Turtle. Also, even with the suggested fix, the drawing is a bit off, so I've reworked it below to fix the turtle usage issues, and actually draw (and fill) a heart, though it may not be what you intended:

from turtle import Pen

def curve():
    for _ in range(200):
        pen.right(1)
        pen.forward(1)

def heart():
    pen.forward(112)
    pen.right(80)
    pen.forward(112)

def txt():
    pen.up()
    pen.setpos(-68, 95)
    pen.color('lightgreen')
    pen.write('i love you', font=('verdana', 12, 'bold'))

pen = Pen()
pen.fillcolor('red')

pen.begin_fill()

curve()
pen.left(120)
curve()
heart()

pen.end_fill()

pen.hideturtle()

txt()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I didn't continue reading the question beyond the fix I proposed in my answer, so this is definitely the better answer, +1 :) – frippe Dec 22 '21 at 06:14