K
K
KeitLis2021-05-17 23:04:08
JavaScript
KeitLis, 2021-05-17 23:04:08

Build a table using loops in Python. How to make a sequence?

Good day.
Guys, help me figure out how to build a "table" at least a hint.
It is necessary to build using cycles:

0 2 4 6 8 10
1 3 5 7 9 11
2 4 6 8 10 12
3 5 7 9 11 13
4 6 8 10 12 14
5 7 9 11 13 15

The sequence is approximately clear - as if each subsequent the column increases by 2.
If you write something like:

size = int(input("Enter table size: "))
for col in range(size):
for row in range(size):
if row % 2 == 0:
print (col, end = " ")
else:
print(col + 2, end = " ")
print()

results in:
2 4 2 4 2
3 5 3 5 3
4 6 4 6 4

The first 2 columns of norms, and then they are repeated.
I would be grateful for at least a hint, I'm missing something obvious.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Shumov, 2018-12-04
@inoise

You have understood the general meaning correctly. Read a lot about REST and don't forget about security

D
D1os, 2021-05-18
@KeitLis

In python, you can set the loop to work manually.

for i in range(2, 11, 2):
    print(i)

In this example, the first argument in the loop brackets is the initial value of the counter, the second argument is the final value of the counter, the third is the increment (increase) of the counter.
That is, this loop will print every other number in the range from 2 to 10 (2, 4, 6, 8, 10).
Take a break from this.
Ps, if necessary, the finished code is below.
spoiler
size = int(input("Table size: "))
for i in range(size):
    for j in range(0, 2*size, 2):
        print(i + j, end=' ')
    print('\n')

# Второй вариант
for i in range(size):
    for j in range(i, 2*size + i, 2):
        print(j, end=' ')

Y
Yupiter7575, 2021-05-17
@yupiter7575

pip install PrettyTable

M
MinTnt, 2021-05-17
@MinTnt

size = 6

for i in range(size):
  print(*range(i, size*2+i, 2))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question