1

This is my code and this is wrong

i = ''
for a in range (1,37):
    i += str(a)+' '
    print(i)

This is the output i want

This is the output i want

Seyn
  • 13
  • 5
  • This code has syntax errors. What is `for b in range` supposed to do? – Selcuk Nov 01 '21 at 04:01
  • Your question had nothing to do with Jupyter or `while` loops for that matter, so I removed those tags. Please provide a code example that has its obvious syntax errors fixed and is a genuine attempt to solve the problem yourself. – Grismar Nov 01 '21 at 04:04
  • @Selcuk i'm sorry, i fixed it – Seyn Nov 01 '21 at 04:06
  • @Grismar i'm sorry bro, im will fix it now – Seyn Nov 01 '21 at 04:08

6 Answers6

2

Try this way:

def contnum(n):
    num = 1
    for i in range(0, n):
        for j in range(0, i+1):
            print(num, end=" ")
            num = num + 1
            print("\r")
         
n = 5       
contnum(n)
Bibhav
  • 1,579
  • 1
  • 5
  • 18
gretal
  • 1,092
  • 6
  • 14
1

An option is to use itertools.count:

import itertools

n = 8
c = itertools.count(start=1)

for i in range(1, n + 1):
    print(' '.join(str(next(c)) for _ in range(i)))

(Actually you don't need join; you can just use unpacking: print(*(next(c) for _ in range(i))))

If you don't want to import a module, but you are willing to use walrus operator (python 3.8+),

n = 8
c = 0
for i in range(1, n + 1):
    print(*(c := c + 1 for _ in range(i)))

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
j1-lee
  • 13,764
  • 3
  • 14
  • 26
  • What is the ( _ ) symbol for sir? – Seyn Nov 01 '21 at 04:17
  • @Sennayga It's just throwing away the value; you can just replace it with any other insignificant letter, such as `x`. This value won't be used anyways. For example, `[[] for _ in range(3)]` will result in `[[], [], []]`, which is a good trick to initialize a list of empty lists, by the way. (https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) – j1-lee Nov 01 '21 at 04:18
  • Aaah i see, and what is the word "join" for ? – Seyn Nov 01 '21 at 04:26
  • @Sennayga `', '.join(['Alice', 'Bob', 'Charles'])` will result in `'Alice, Bob, Charles'`; i.e., it joins a list of strings into a string, putting a specified string between them. In this case, the 'in-between' string is a blank `' '`. – j1-lee Nov 01 '21 at 04:30
0
limit,a, k = 37,1, 1
while a < limit:
    print(' '.join(str(x) for x in range(a, min(a + k,limit))))
    a += k
    k += 1

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
0

One way using itertools.islice and count, and iter's sentinel:

it = iter(range(1, 37))
n = count(1)
f = lambda : list(islice(it, next(n)))
list(iter(f, []))

Output:

[[1],
 [2, 3],
 [4, 5, 6],
 [7, 8, 9, 10],
 [11, 12, 13, 14, 15],
 [16, 17, 18, 19, 20, 21],
 [22, 23, 24, 25, 26, 27, 28],
 [29, 30, 31, 32, 33, 34, 35, 36]]

Or if you want to print:

for i in iter(f, []):
    print(*i)

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
Chris
  • 29,127
  • 3
  • 28
  • 51
0

You can also do it like this:-

n=1
for i in range(8):
    for j in range (i+1):
        print(n,end=' ')
        n += 1
    print()
Bibhav
  • 1,579
  • 1
  • 5
  • 18
Python learner
  • 1,159
  • 1
  • 8
  • 20
  • 1
    I like this one, it looks simple and easy to understand, Thank you sir for helping me! – Seyn Nov 01 '21 at 06:55
0

My entry for code golf:

for n in range(1, 9):
    print(*range(1+n*(n-1)//2, 1+n*(n+1)//2))

produces the expected outcome.