Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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'
>>> 

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.

like image 851
user2803457 Avatar asked Sep 22 '13 04:09

user2803457


1 Answers

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.

like image 79
Burhan Khalid Avatar answered Sep 28 '22 18:09

Burhan Khalid