Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

Is there a way to sweep through floats in python using a for loop?

I'm trying to sweep through values of pi/24 to pi/2 in steps of pi/24. Currently I'm getting an error, with float object not being able to be interpreted as an integer. Is there any work around to this?

Sample of my code:

for theta in range(np.pi/24,np.pi/2,np.pi/24):
    v_initial_x = np.cos(theta)*velocity
    v_initial_y = np.sin(theta)*velocity
question from:https://stackoverflow.com/questions/65866236/is-there-a-way-to-sweep-through-floats-in-python-using-a-for-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is because the built-in function range only takes integers. You can use numpy like so:

for theta in np.arange(np.pi/24, np.pi/2, np.pi/24):
    ''' Do your stuff here '''

Remember that the stop value is excluded from the range, so if you want to include that in your calculation, then you will have to add np.pi/24 to your stop value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...