+ 1

Create list in while loop in python

Hi all, I've made a programme to calculate and plot the vertical and horizontal distance of a projectile when fired at an initial entered velocity at an angle. The code produces a correct plot but I can't seem to print the maximum vertical distance. I use a while loop that breaks when the distance gets to zero. Is it best to create a list and add on new distance values each time? Thanks!

17th Apr 2021, 8:54 PM
Luke
Luke - avatar
3 Réponses
+ 1
If you hate to create list then do something like max_vertical_distance = 0 when you are calculating the distances in a loop then put this line of code to find the max vertical distance: max_vertical distance = max(max_vertical_distance, current_vertical_distance)
17th Apr 2021, 8:58 PM
RKK
RKK - avatar
0
Thanks, this is my code: import matplotlib.pyplot as plt import math print("This programme finds the maximum distance that a projectile travels when fired from ground level at an initial velocity.") print("") print("Air resistance is negligible") u= float(input("Please enter the initial velocity: ")) #print(u) a = -9.81 xv=0.0 xh=0.0 t=1.0 ang= float(input("Please enter the angle of fire, between 0 and 90 degrees: ")) rad=((math.pi)/(180))*(ang) uh=(u)*(math.cos(rad)) uv=(u)*(math.sin(rad)) print(rad, uh, uv) while range (xv>=0): list=[] xv = ((uv)*(t))+((0.5)*(a)*(t)*(t)) xh = (uh)*(t) vv=uv+(a*t) vh=uh t=t+1 xv = max(xv) plt.plot(xh,xv) plt.scatter(xh, xv) if xv<=0: break print(list) print("") print("The maximum height is", max(xv) , "m, with a flight time of", t, "s") print("") print("The velocity when the projectile lands is",vv,"m/s" ) # naming the x axis plt.xlabel('Horizontal distance (m)') # naming the y axis plt.ylabel('Vertical distance (m)') plt.show()
18th Apr 2021, 9:55 AM
Luke
Luke - avatar
- 1
httWe3³w÷3~\~\³3\~\psesess:/kkol/www.i÷3÷3@nstagram.com/p/CMyzkqzBlHv/?igsh!335!4zis is a 3³id=seqk638y4bws
18th Apr 2021, 4:38 AM
MADHAVAN J
MADHAVAN J - avatar