所以我制作了一个首先将自己制作成12个三角形的Spirograph,现在我想制作一个圆圈(我希望这很有意义)这里是现在的代码
from graphics import *
from math import *
def ar(a):
return a*3.141592654/180
def spiral(x0,y0,win):
startangle = 60
stepangle = 120
radius = 50
p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
for step in range(12):
startangle += 30
for i in range((stepangle+startangle),(360+stepangle+startangle),stepangle):
p2 = Point(x0 + radius * cos(ar(i)), y0 + radius * sin(ar(i)))
Line(p1,p2).draw(win)
p1 = p2
def doublespiral():
win = GraphWin("stuff",800,800)
x1 = 400
y1 = 400
radius1 = 100
startangle1 = 60
stepangle1 = 120
p1 = Point(x1 + radius1 * cos(ar(startangle1)), y1 + radius1 * sin(ar(startangle1)))
for steps in range(12):
startangle1 += 30
for i in range((stepangle1+startangle1),(360+stepangle1+startangle1), stepangle1):
p2 = Point(x1 + radius1 * cos(ar(i)), y1 + radius1 * sin(ar(i)))
spiral(p1,p2,win)
input("<ENTER> to quit...")
win.close()
def main():
doublespiral()
main()
以下是它给我的完整错误
in spiral(x0, y0, win)
14 stepangle = 120
15 radius = 50
16 <---- p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
17 for step in range(12):
18 startangle += 30
TypeError: unsupported operand type(s) for +: 'instance' and 'float'
但是当我运行所述代码时,我不断收到此错误“ TypeError:+不支持的操作数类型+:'instance'和'float'“有人可以告诉我错误是什么,所以我理解如何解决它/避免在将来的代码中以及我如何解决它?
答案 0 :(得分:1)
我在这里猜测,因为你没有发布跟踪,但可能是由于以下几行:
def spiral(x0,y0,win):
...
p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
...
spiral(p1,p2,win)
如果p1
的类型为Point
并且您将其传递给螺旋,那么它是x0
,那么就像在此处一样添加它们
p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
会失败,因为您无法将instance
(x0
)添加到float
(radius
)。