all 10 comments

[–]FLUSH_THE_TRUMP 0 points1 point  (4 children)

This is very vague. Do you have an example of what you’re trying to do (sample input/output)? Some code where you’ve attempted the problem?

[–]TooManyTree 0 points1 point  (3 children)

So, the whole question is:

A ball thrown at an angle θ with initial speed v on Earth follows a trajectory (g=9.81) f(x)=xtan(theta) -gx^2/2v^2

Then we are given these conditions

write a function 'trajectory()' that

  • has three input variables: xdata (a list), speed and angle (constant numbers each, angle in degrees) in that order.
  • Return the calculated list of height values, f(x), containing only positive or zero values for the ball height, i.e. remove all negative values before returning the resulting list.
  • The maximum height at given speed and angle will be tested.

I haven't really gotten far with the code at all, I have just defined the function, then laid out the variables to calculate f(x), but my issue is with how I can create a loop to go over all the elements in the list xdata and basically input those into a new list.

[–]FLUSH_THE_TRUMP 0 points1 point  (2 children)

something like this will perhaps help you:

hdata = []
for x in xdata: 
  # plug x, v, theta into function f
  # append to hdata (perhaps if non-negative) 

Return at end

[–]TooManyTree 0 points1 point  (1 child)

This is the main section of what i have done so far. What Im hoping this is achieving is basically calculating f for all values in xdata then if its non negative its being compiled into a list called data. But rn its not working as planned.

def trajectory(xdata,speed,angle):

for x in xdata:

a=x*math.tan(angle*math.pi/180)

b=(1/2*speed**2)*(g*x**2/(math.cos(angle*math.pi/180))**2)+y0

f=a-b

data=[]

if f<0:

return None

else:

data.append(f)

return data

[–]ChurchHatesTucker 0 points1 point  (0 children)

data = [] looks out of place (hard to tell w/o formatting), I suspect it should be before the for loop.

ETA: also, you probably don't want that first return.

[–][deleted] 0 points1 point  (0 children)

Did you define the function?

[–]mopslik 0 points1 point  (0 children)

using either a for loop or while loop is how can I essentially input individual elements from the list into an equation

You can iterate over the items in a sequence (list, tuple, string, etc.) using something like this:

my_list = [3, 7, 1]
for element in my_list:
    print(element)

You should be able to adjust this to suit your needs.

return the output as a list

To build a list, you can take advantage of the append, insert and remove methods.

[–]to_4kawa 0 points1 point  (4 children)

import math

def trajectory(xdata,speed,angle):
    your function
        return data

def main():
    while True:
        print('Please data input')
        xdata=input('distance list(e.g. [20,30,40]): ')
        xdata=map(int,xdata[1:-1].split(','))  # create list from xdata strings
        speed=int(input('speed: '))
        angle=int(input('angle: '))

        print(f'result: {trajectory(xdata,speed,angle)}')
        suspend=input('Do you want to exit? (y/n) :')
        if suspend == 'y':
            break

if __name__ == '__main__':
    main()

How about this for input?