Home > Back-end >  How do I print a sequence of "1 2 3" for a length up to an input integer?
How do I print a sequence of "1 2 3" for a length up to an input integer?

Time:04-01

for instance:

input=7 -> print : 1 2 3 1 2 3 1
input=2 -> print : 1 2

I've only been able to print the whole "1 2 3" repeated for the input integer with the code below. (input=2 -> print : 1 2 3 1 2 3)

n = int(input())
for i in range(1,n 1):
    for num in range(1,4):
        print(num, end="")
        num  = 1

CodePudding user response:

you could use modulo operation to accomplish this:

for x in range(int(input("number:"))):
    print(x%3 1,end=' ')

CodePudding user response:

You can access element in [1, 2, 3] based on index modulo 3.

values = [1, 2, 3]
n = int(input())
for i in range(n):
    index = i % len(values)
    print(values[index], end=" ")

CodePudding user response:

you can simply write,

" ".join([str((i%3) 1)  for i in range(n)] ) 
  • Related