Вот еще один способ использования Python 3 f-strings
:
def draw_grid(x):
for _ in range(x):
print(''.join(f'{_+1} ' for _ in range(x)))
И вот подтверждение концепции:
Python 3.7.4 (default, Aug 12 2019, 14:45:07)
[GCC 9.1.1 20190605 (Red Hat 9.1.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def draw_grid(x):
... for _ in range(x):
... print(''.join(f'{_+1} ' for _ in range(x)))
...
>>> draw_grid(5)
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
>>> draw_grid(10)
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
>>>
И, наконец, вот однострочная версия:
def draw_grid(x):
print(*[''.join([f'{_+1} ' for _ in range(x)]) for _ in range(x)], sep='\n')