Home > Blockchain >  Need a sequence that grows by 1 until a certain number
Need a sequence that grows by 1 until a certain number

Time:09-19

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.

CodePudding user response:

I would use recursion:

def a(num=0):

    if num ==0:
        print ("(1)", end =", ")
    
    else:
        print (f"({num} 1)", end = ", ")  
    

    if int(num) < 179:
        a(num 1)
    

a()

CodePudding user response:

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.

CodePudding user response:

# 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
  • Related