-2

I'm very new to coding and I'm trying to figure out how to make a sequence that grows like 1+(1+1)+(2+1)+(3+1)+(4+1) etc. until it reaches exactly 180 (in python). So pretty much adds 1 to the last answer. Then when it reaches 180 it should come back down like ("last number of going up"-1)-("last result"-1) etc. until it reaches 1 again.

Sorry if it's a little hard to understand, but I didn't know how else to explain it really.

Aruand
  • 3
  • 1
  • What have you searched for, and what did you find? What have you tried, and how did it fail? – tripleee Sep 19 '22 at 12:04
  • Hello Aruand. Could you share with us what have you tried? – Jino Michel Aque Sep 19 '22 at 12:04
  • 1
    The sequence you're describing will never reach exactly 180. Starting from 1 you'll reach 171 on the 18th iteration followed by 190. – Mathias R. Jessen Sep 19 '22 at 12:06
  • So pretty much ithe 180 represents degrees that have to turn on a servo, but it's more about the loop than making the servo turn. I tried finding answers on google multiple times but all I could find was info about arithmetic sequences and geometric sequences. – Aruand Sep 19 '22 at 12:08
  • I have tried: n=1 while n < 180: n = n+1 print(n) but I know it doesn't work and why, but I just can't figure out how to proceed because I'm so new. (edit: sorry I didn't know the code would look like that :) ) – Aruand Sep 19 '22 at 12:12
  • Sorry, I misread. This is still simple to put together from standard library tools, but not simple enough to mark as a duplicate. – Karl Knechtel Sep 19 '22 at 15:59
  • "but I know it doesn't work and why" When you encounter this problem, start by thinking: **what happens** when it "doesn't work"? **How is that different** from what needs to happen? – Karl Knechtel Sep 19 '22 at 16:16

3 Answers3

0

Your pseudo code looks like it reveals some C or C++ thinking.

I guess you simply need a for-loop:

import numpy as np

for angle in np.arange(0,181,1):
    print(f'{angle=}')

The arange function, as it is used here, takes two or three arguments. The first is start position, the second is the end condition (excluded so will never be produced) and the third is an optional step size.

0
# This might work

# A lube within a lube and they are combined

for i in range(1,10):
    for j in range(1,10):
        print(f'{i} + {j} = {i+j}')

# some results

(1 + 1) = 2
(1 + 2) = 3
(1 + 3) = 4
(1 + 4) = 5
(1 + 5) = 6
(1 + 6) = 7
(1 + 7) = 8
(1 + 8) = 9
(1 + 9) = 10
(2 + 1) = 3
(2 + 2) = 4
(2 + 3) = 5
(2 + 4) = 6
(2 + 5) = 7
(2 + 6) = 8
0

The built-in range gives us ascending and descending sequences:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(9, -1, -1))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Notice, each range already "represents" the sequence, and we can iterate over it in a for loop for example. Using list here is just to show all the values.

If we just want to iterate over both of those ranges, we could just write two loops. However, if those loops are doing the same thing, maybe we instead want to join them together into the same sequence. For this, we use chain from the standard library itertools module:

>>> import itertools
>>> list(itertools.chain(range(10), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The 9 was repeated, because it's in both ranges; we can easily adjust either of the ranges to avoid that. For example:

>>> import itertools
>>> list(itertools.chain(range(9), range(9, -1, -1)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

What if we want to repeat the sequence forever (or until some break condition is met)? We could just wrap our for loop over that sequence, inside a while loop; but it would be neater if we could make the sequence itself be unbounded, and repeat the pattern indefinitely when we iterate.

We can also do that with the standard library, using cycle, also from itertools. We will want to remove the 0 from the end to avoid duplicating it when the pattern starts over; fortunately, that's just as easy as before - just set the end point for the second range appropriately. We get something quite elegant. I will wrap up the sequence creation in a function, too:

from itertools import cycle, chain

def up_and_down(n):
    return cycle(chain(range(n), range(n, 0, -1)))

Let's test it:

>>> import time
>>> for i in up_and_down(180):
...     print(f'{i: 4}', end='\r')
...     time.sleep(0.1)

We can see (until we abort with ctrl-C) the counter smoothly count up to 180 and back down to 0, then repeat (if we wait 36 seconds).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thanks a lot, this is exactly what I was looking for. Also thank you for the detailed talk trough the steps so that I know how it works and how to use it to solve some future problems :) – Aruand Sep 19 '22 at 16:53