+ 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!

3 Answers

+ 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)

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()

- 1

httWe3Â³wÃ·3~\~\Â³3\~\psesess:/kkol/www.iÃ·3Ã·[email protected]/p/CMyzkqzBlHv/?igsh!335!4zis is a 3Â³id=seqk638y4bws

Hot today

Bug: Sololearn notification is still showing number in the notification icon even i did mark as read

3 Votes

Izzy the iguana code coach

0 Votes

Finished python for beginners

1 Votes

C++ Question re stringstream

0 Votes

Why sometimes 0 is printed?

0 Votes

Web development

0 Votes