Function missing 2 required positional arguments: 'x' and 'y'

拟墨画扇 提交于 2019-12-06 02:08:23

The last line of the traceback tells you where the problem is:

  File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
    spirograph(p-1, x,y) # <--- this is the problem line
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'

In your code, the spirograph() function takes 5 arguments: def spirograph(R,r,p,x,y), which are R, r, p, x, y. In the line highlighted in the error message, you are only passing in three arguments p-1, x, y, and since this doesn't match what the function is expecting, Python raises an error.

I also noticed that you are overwriting some of the arguments in the body of the function:

def spirograph(R,r,p,x,y):
    R=100 # this will cancel out whatever the user passes in as `R`
    r=4 # same here for the value of `r`
    t=2*pi

Here is a simple example of what is happening:

>>> def example(a, b, c=100):
...    a = 1  # notice here I am assigning 'a'
...    b = 2  # and here the value of 'b' is being overwritten
...    # The value of c is set to 100 by default
...    print(a,b,c)
...
>>> example(4,5)  # Here I am passing in 4 for a, and 5 for b
(1, 2, 100)  # but notice its not taking any effect
>>> example(9,10,11)  # Here I am passing in a value for c
(1, 2, 11)

Since you always want to keep this values as the default, you can either remove these arguments from your function's signature:

def spirograph(p,x,y):
    # ... the rest of your code

Or, you can give them some defaults:

def spirograph(p,x,y,R=100,r=4):
    # ... the rest of your code

As this is an assigment, the rest is up to you.

Jochen Ritzel

The error tells you that you're using too few arguments to call spirograph

Change this code:

while p<100 and p>10:
    goto(x,y)
    spirograph(R,r, p-1, x,y) # pass on  the missing R and r

You're not using these arguments though, but you still have to give them to the function to call it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!