Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 4

Tell us what’s happening:

Hmm, hey there! :slight_smile: I need help, not asking what I’m doing since I’m not sure myself. I could use some assistance here.

Your code so far

import math

GRAVITATIONAL_ACCELERATION = 9.81
PROJECTILE = "∙"
x_axis_tick = "T"
y_axis_tick = "⊣"

class Projectile:
    __slots__ = ('__speed', '__height', '__angle')

    def __init__(self, speed, height, angle):
        self.__speed = speed
        self.__height = height
        self.__angle = math.radians(angle)
        

# User Editable Region

    def __calculate_displacement(self):
        d = v*math.cos()*(v.math.sin()+math.sqrt(v**2)math.sin()**2*GRAVITATIONAL_ACCELERATION*height)/GRAVITATIONAL_ACCELERATION

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 4

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

Hmm, I made a small modification:

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:

shouldn’t this be:

math.sqrt(v**2*math.sin(θ)**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

Whats now wrong?
console:

what do you want to do here? math is not a property of v

and you can’t have two methods one after the other like this, it’s here that is asking if you forgot a comma

you also have a= in the middle of the expression, you can’t have that

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 :weary_face:

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!

I Not understanded it.

Try changing math.sqrt(pow(v,2),pow(math.sin(θ),2) to math.sqrt(math.pow(v,2) * math.pow(math.sin(θ),2)

Also, I just returned the calculation rather than creating a variable for the calculation.

I hope that’s clearer.

I’m pretty sure the test is only checking the output, implementation is not taken into consideration

if you call the function like suggested

    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)

you still need to keep fixing syntax errors

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.

That’s because of the error in this bit: math.sqrt(pow(v,2),pow(math.sin(θ),2).

you should let people find their own errors instead of telling them how to fix them too

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.

1 Like

Well, after a lot of cooking, I made some changes and found the problem was in my math.sqrt(). I fixed it, and now it’s working. Thanks for the help :blush: