Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

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

I am trying to write a Python turtle program that draws a Spirograph and I keep getting this error:

Traceback (most recent call last):
  File "C:UsersmattDownloadsspirograph.py", line 36, in <module>
    main()
  File "C:UsersmattDownloadsspirograph.py", line 16, in main
    spirograph(R,r,p,x,y)
  File "C:UsersmattDownloadsspirograph.py", line 27, in spirograph
    spirograph(p-1, x,y)
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
>>> 

This is the code:

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()

I know this maybe has a simple solution but I really can't figure out what I am doing wrong, this was an exercise in my computer science 1 class and I have no idea how to fix the error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  File "C:UsersmattDownloadsspirograph.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...