-1

I am trying to cycle through a list of numbers (mostly decimals), but I want to return both 0.0 and the max number. for example

maxNum = 3.0 
steps = 5
increment = 0
time = 10
while increment < time:
    print increment * (maxNum / steps)% maxNum
    increment+=1
#

I am getting this as an output

0.0
0.6
1.2
1.8
2.4
0.0

but I want 3.0 as the largest number and to start back at 0.0 I.E.

0.0
0.6
1.2
1.8
2.4
3.0
0.0

Note, I have to avoid logical loops for the calculation part.

Goufalite
  • 2,253
  • 3
  • 17
  • 29
user1869582
  • 449
  • 1
  • 3
  • 10

3 Answers3

1

You could create the numbers that you want then use itertools.cycle to cycle through them:

import itertools
nums  = itertools.cycle(0.6*i for i in range(6))
for t in range(10):
    print(next(nums))

Output:

0.0
0.6
1.2
1.7999999999999998
2.4
3.0
0.0
0.6
1.2
1.7999999999999998
John Coleman
  • 51,337
  • 7
  • 54
  • 119
0

You could make a if statement that looks ahead if the next printed number is 0.0 then print the maxNum

maxNum = 3.0
steps = 5
increment = 0
time = 10

while increment < time:
    print(round(increment * (maxNum / steps)% maxNum, 2))
    increment+=1
    if (round(increment * (maxNum / steps)% maxNum, 2)) == 0.0:
        print(maxNum)
0.0
0.6
1.2
1.8
2.4
3.0
0.0
0.6
1.2
1.8
2.4
3.0
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

Only small change did the trick:

maxNum = 3.0
steps = 5
i = 0
times = 10
step = maxNum / steps
while (i < times):
    print(step * (i % (steps + 1)))
    i += 1

0.0
0.6
1.2
1.7999999999999998
2.4
3.0
0.0
0.6
1.2
1.7999999999999998
MBo
  • 77,366
  • 5
  • 53
  • 86