Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My python program keeps on giving me an error saying: File \"/Users/petermanfred

ID: 3910677 • Letter: M

Question

My python program keeps on giving me an error saying:

File "/Users/petermanfredonia/Desktop/CSE 1010/CSE 1010 Assignments Due/HW3NBody.py", line 92, in setup
tBody = newTurtle(bodNum)
TypeError: newTurtle() takes 0 positional arguments but 1 was given

Here is my program:

import math, random, time, turtle

NBodies = 5
G = 5
SpaceRadius = 250
MinMass = 5
MaxMass = 100
MaxVelocity = 100
BodyColor = 'black'
TraceColor = 'green'

Turtles = []
Masses = []
Xs = []
Ys = []
Vxs = []
Vys = []

OffScreen = []
WinX2 = 0
WinY2 = 0

def newTurtle():
tnew = turtle.Turtle()
Turtles.append([tnew])   
tnew.speed(0)
tnew.pensize(5)
return tnew;

def printBodyInfo(n):
print('Body', n, 'mass =', Masses[n], ', x =', Xs[n], ', y =',
Ys[n], ', vx =', Vxs[n], ', vy =', Vys[n])

def initBody(t):
tMass = random.randint(MinMass, MaxMass)
Masses.append(tMass)
t.turtlesize(tMass * 0.03, tMass * 0.03, None)      
t.shape("circle")   

tposx = random.randint(-SpaceRadius, SpaceRadius)   
Xs.append(tposx)
tposy = random.randint(-SpaceRadius, SpaceRadius)
Ys.append(tposy)

tvelx = random.randint(-MaxVelocity, MaxVelocity)   
Vxs.append(tvelx)   
tvely = random.randint(-MaxVelocity, MaxVelocity)   
Vys.append(tvely)

OffScreen.append(False)
t.penup()
t.goto(tposx, tposy)
t.pendown()
  
def setup():
turtle.tracer(0, 0)
for bodNum in range(NBodies):
tBody = newTurtle(bodNum)
initBody(tBody)
printBodyInfo(tBody)
turtle.update()

def main():
print('N-Body simulation starting')
screen = turtle.Screen()
screen.title('N-Body Simulator')
global WinX2, WinY2
WinX2 = screen.window_width() / 2
WinY2 = screen.window_height() / 2

setup()

print('Program finished')
screen.mainloop()

if __name__ == '__main__':
main()

Explanation / Answer

This is happening because the newTurtle() function defined by you does not take any arguments, but while calling the newTurtle() function, you are trying to pass one argument 'bodNum' to it.

My suggestion to you would be to not include the bodNum argument while calling the fucntion since you are nowhere using bodNum inside the newTurtle().

You can do something like this:

for bodNum in range(NBodies):
tBody = newTurtle()