what is v? a non-existing variable throws an error
if you want to calculate a cosine, you need to give an argument to math.cos(), like that it throws an error
something symptactially wrong here too
if you are taking the square root of a square, it’s like doing nothing math.sin also needs an argument
and you are missing something between the two methods
note, these are only syntax issues, not checking if what you have written matches the formula
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
d = v*math.cos(θ)*(v.math.sin(θ)+math.sqrt(v**2math.sin(θ)**2+2*g*h))/g
Now v,θ,h, and g have values as shown in the formula:
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
d = v*math.cos(θ)*(v.math.sin(θ)+math.sqrt(pow(v,2)pow(math.sin(θ),2)=+2*g*h))/g
Well, thanks — the = was causing a problem and I fixed a few other things too.
But I’m still stuck on this error: “The __calculate_displacement method should return the correct value.”
No matter what I try, none of these solutions work
1.
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
return __calculate_displacement = v*math.cos(θ)*(v*math.sin(θ)+math.sqrt(pow(v,2),pow(math.sin(θ),2)+2*g*h))/g
this throwing Traceback (most recent call last): File "main.py", line 21 return __calculate_displacement = v*math.cos(θ)*(v*math.sin(θ)+math.sqrt(pow(v,2),pow(math.sin(θ),2)+2*g*h))/g ^ SyntaxError: invalid syntax
2.
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
d = v*math.cos(θ)*(v*math.sin(θ)+math.sqrt(pow(v,2),pow(math.sin(θ),2)+2*g*h))/g
return d
3.
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
d = v*math.cos(θ)*(v*math.sin(θ)+math.sqrt(pow(v,2),pow(math.sin(θ),2)+2*g*h))/g
__calculate_displacement = d
return __calculate_displacement
So yeah… what should I do now?
Am I missing something in the formula or syntax?
Looks like the tests may not like all of the variables you’ve created to plug into that algorithm, so just use self.__speed, etc. Also, I used the x ** y syntax given in the instructions to express squared (should be math.pow anyway). I did not create any variables, just returned the calculation. Also, the comma between your two pow methods is wrong. Those should be multiplied. Just make those changes and you should be good to go!
def __calculate_displacement(self):
v = self.__speed
θ = self.__angle
g = GRAVITATIONAL_ACCELERATION
h = self.__height
d = v*math.cos(θ)*(v*math.sin(θ)+math.sqrt(pow(v,2),pow(math.sin(θ),2)+2*g*h))/g
return d
ball = Projectile(10, 3, 45)
displacement_of_ball = ball._Projectile__calculate_displacement() # 12.6173996009878
you would get
Traceback (most recent call last):
File "main.py", line 25, in <module>
File "main.py", line 21, in __calculate_displacement
TypeError: math.sqrt() takes exactly one argument (2 given)
Yes. I did play with it a bit after the code passed and discovered the extra variables were not an issue. Also, use of math.pow() rather than the x ** y syntax was not an issue.
I usually try to do that, @ILM. Helping out on the forum and creating issues for improvements is just my way of contributing. However, if my approach is considered problematic, I will stop. In this case, @seopostexpert has helped others here quite a bit, so I made a special effort to help when he posted. I don’t even know much about Python yet; this just looked like a fun challenge.