我试图编写一个绘制Spirograph的Python turtle程序,但我始终收到以下错误:
Traceback (most recent call last):
File "C:\Users\matt\Downloads\spirograph.py", line 36, in <module>
main()
File "C:\Users\matt\Downloads\spirograph.py", line 16, in main
spirograph(R,r,p,x,y)
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y)
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
>>>
这是代码:
from turtle import *
from math import *
def main():
p= int(input("enter p"))
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
spirograph(R,r,p,x,y)
def spirograph(R,r,p,x,y):
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
while p<100 and p>10:
goto(x,y)
spirograph(p-1, x,y)
if p<10 or p>100:
print("invalid p value, enter value between 10 nd 100")
input("hit enter to quite")
bye()
main()
我知道这可能有一个简单的解决方案,但我真的不知道我做错了什么,这是我在计算机科学1班的一个练习,我不知道如何纠正错误。
该错误告诉您使用的参数太少,无法调用
spirograph
更改此代码:
虽然您没有使用这些参数,但是您仍然必须将它们交给函数来调用它。
回溯的最后一行告诉您问题所在:
在您的代码中,
spirograph()
函数接受5个参数:def spirograph(R,r,p,x,y)
,它们是R
、r
、p
、x
、y
。在错误消息中突出显示的行中,您只传递了三个参数p-1, x, y
,由于这与函数所期望的不匹配,Python将引发一个错误。我还注意到您正在重写函数体中的一些参数:
下面是一个简单的例子:
由于始终希望将此值保留为默认值,因此可以从函数签名中删除以下参数:
或者,可以给它们一些默认值:
因为这是一个任务,剩下的由你决定。
相关问题 更多 >
编程相关推荐