Получение "TypeError: объект 'str' не вызывается" в программе Python

import turtle      
wn = turtle.Screen()  
Kasey = turtle.Turtle()

sides = input("Number of sides in polygon?"  )
length = input("Length of the sides in polygon?" )
colorname = input("Color of polygon?" )
Hcolor = input("Fill color of polygon?")

Kasey.pencolor(colorname)
Kasey.fillcolor(Hcolor)

Kasey.begin_fill()
for i in range(int(sides)):
  Kasey.forward (int(length))
  Kasey.left (int(360)/int(sides))

Kasey.end_fill()

Ошибка:

Number of sides in polygon? 3
Length of the sides in polygon?5
Color of polygon?red
Fill color of polygon?blue

Traceback (most recent call last):

  File "<ipython-input-108-82ed860f1049>", line 1, in <module>
    runfile('/home/kasey/turtle.py', wdir='/home/kasey')

  File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/kasey/turtle.py", line 18, in <module>
    Kasey.fillcolor(Hcolor)

TypeError: 'str' object is not callable

2 ответа

Заменить Kasey.fillcolor(Hcolor) с Kasey.color(<pen color>, <fill color>)Посмотрите эту ссылку, чтобы узнать больше.

Присвоение имени исходному файлу turtle.pyявляется серьезной ошибкой, но она не учитывает полученное вами сообщение об ошибке, поскольку оно должно было выйти из строя ранее в коде. Я пробовал различные сценарии сбоя, но не смог воспроизвести ваш.

Я бы структурировал ваш код следующим образом:

from turtle import Screen, Turtle

sides = int(input("Number of sides in polygon: "))
length = int(input("Length of the sides in polygon: "))
pen_color = input("Color of polygon: ")
fill_color = input("Fill color of polygon: ")

screen = Screen()

kasey = Turtle()
kasey.color(pen_color, fill_color)

kasey.begin_fill()

for _ in range(sides):
    kasey.forward(length)
    kasey.left(360 / sides)

kasey.end_fill()

screen.mainloop()

Посмотрите, работает ли это лучше в вашей среде Anaconda. (Последняя строка может вызвать проблемы в вашей среде - при необходимости удалите ее.)

Другие вопросы по тегам