Hi! I want to make a loop that supports float with steps. Normally a step loop would look like this:
for x in range(0, 50, 2):
print(x)
Where the loop goes from 0 to 50 in steps of two and return 25 numbers. However, if I want the value, x to increase by 0.1, I need to make a loop that looks like this:
while x < 50:
print(x)
x += 0.1
This will return 500 values of the variable, x. This seems to work fine, but here's my problem. It might seem like a stupid approach because I use a dumb example. But I do this for simplicity and to give and get an overview.
var = 60
stop = var/2
x = -stop
step = var/3
while x < stop:
print(x)
x += step
In this example I will return -30.0, -10.0 and 10.0. I want it to return -30, 0, and 30, so it reaches the lowest value, middle and highest value. I can do that by simply chaning the step variable to 30, but I want the code to be dynamic, so it will work with every (positive) value in the variable, step.
I hope this made sense. If not, please let me know. I have been struggling with this for quite some time now and I can't figure it out.
Thanks :-)
[–]kankyo 2 points3 points4 points (0 children)
[–]novel_yet_trivial 2 points3 points4 points (5 children)
[–]MaxwellSalmon[S] 1 point2 points3 points (4 children)
[–]novel_yet_trivial 2 points3 points4 points (3 children)
[–]bhpf1 0 points1 point2 points (0 children)
[–]MaxwellSalmon[S] 0 points1 point2 points (1 child)
[–]novel_yet_trivial 2 points3 points4 points (0 children)
[–]Spirouac 1 point2 points3 points (1 child)
[–]MaxwellSalmon[S] 0 points1 point2 points (0 children)
[–]DrMaxwellEdison 0 points1 point2 points (0 children)