Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: print without overwriting printed lines

I have

for i in range(0, 11): print i, "\n", i

I'd like my python program to print this way for each for loop

1st loop:

1
1

2nd loop:

1
2
2
1

3rd loop:

1
2
3
3
2
1

I've tried using \r\n or \033[1A but they just overwrite the previous line. Is there a way I can "push" the outputted line down so I don't overwrite it?

like image 214
freesushi Avatar asked Mar 15 '23 20:03

freesushi


1 Answers

One way to do this,

def foo(x, limit):
        if x < limit :
                print x
                foo(x + 1, limit)
                print x

foo(1, 11)
like image 151
Ghazanfar Avatar answered Mar 24 '23 18:03

Ghazanfar