-1

i'm struggling with this seemingly simple python problem.

i need 90 values in the list that increase by 6 (from 120) until it reaches 240, once it reaches 240 it should decrease by 6 back to 120. this loop would continue until 90 values are reached.

x = [30, 36, 42, 48, 54, 60]
e = [120]


for row in range(90):
    if e[row] >= 120 and e[row] != 240:
        e.append(e[row] + 6)
        print(e[row], "1")
    elif e[row] <= 240 and e[row] != 120:
        e.append(e[row] - 6)
        print(e[row])

the code i have so far doesn't work well. after it reaches 240, it goes down to 236. 236 satisfies the >= 120 and != 240 condition so it just goes back up to 240.

any guidance would be appreciated!

lukeborgen
  • 13
  • 4

3 Answers3

0

I guess you want something like this:

x = [30, 36, 42, 48, 54, 60]
e = [120]
dir = 1

for row in range(90):
    if dir == 1:
        if e[row] >= 240:
            dir = -1
    else:
        if e[row] <= 120:
            dir = 1

    e.append(e[row] + (dir * 6))
    print(e[row])
    print(f'  LENGTH: {len(e)}')


rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
0

A one-line way to do this by just gluing ranges together would be:

>>> ((list(range(120, 240, 6)) + list(range(240, 120, -6))) * 3)[:90]
[120, 126, 132, 138, 144, 150, 156, 162, 168, 174, 180, 186, 192, 198, 204, 210, 216, 222, 228, 234, 240, 234, 228, 222, 216, 210, 204, 198, 192, 186, 180, 174, 168, 162, 156, 150, 144, 138, 132, 126, 120, 126, 132, 138, 144, 150, 156, 162, 168, 174, 180, 186, 192, 198, 204, 210, 216, 222, 228, 234, 240, 234, 228, 222, 216, 210, 204, 198, 192, 186, 180, 174, 168, 162, 156, 150, 144, 138, 132, 126, 120, 126, 132, 138, 144, 150, 156, 162, 168, 174]

To build it in a loop the way you're trying to do, I'd have the delta in another variable and only flip it when you hit one of the edges, like this:

>>> e = [120]
>>> d = 6
>>> for _ in range(89):
...     n = e[-1] + d
...     if n >= 240 or n <= 120:
...         d *= -1
...     e.append(n)
...
>>> e
[120, 126, 132, 138, 144, 150, 156, 162, 168, 174, 180, 186, 192, 198, 204, 210, 216, 222, 228, 234, 240, 234, 228, 222, 216, 210, 204, 198, 192, 186, 180, 174, 168, 162, 156, 150, 144, 138, 132, 126, 120, 126, 132, 138, 144, 150, 156, 162, 168, 174, 180, 186, 192, 198, 204, 210, 216, 222, 228, 234, 240, 234, 228, 222, 216, 210, 204, 198, 192, 186, 180, 174, 168, 162, 156, 150, 144, 138, 132, 126, 120, 126, 132, 138, 144, 150, 156, 162, 168, 174]
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

You can use a variable to hold the amount that you're adding, and switch it between 6 and -6. Test whether it's positive or negative to know which end to check for.

e = [120]
increment = 6

for _ in range(90):
    e.append(e[-1] + increment)
    if increment > 0 and e[-1] == 240:
        increment = -6
    elif increment < 0 and e[-1] == 120:
        increment = 6
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks for the answer, it works well. i'm trying to understand the code but i am confused as to why the index of e is -1 – lukeborgen Sep 12 '21 at 08:06
  • Negative indexes count from the end, so `e[-1]` is the last element -- the one that was just appended. – Barmar Sep 12 '21 at 19:03