Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a simple number pattern in python?

I'm trying to get this number pattern

0
01
012
0123
01234
012345
0123456
01234567
012345670
0123456701

But I can't figure out how to reset the digits when I reach over 8 in my function. There is my code:

def afficherPatron(n):
triangle = ''

for i in range(0, n):
    triangle = triangle + (str(i))
    print(triangle)
    i+=1

Thanks in advance to all of you!

like image 666
Francis-Olivier Couture Avatar asked Dec 20 '25 08:12

Francis-Olivier Couture


1 Answers

Use i mod 8 (i%8) because it is cyclic 0 to 7 :

for i in range(0, n):
    triangle = triangle + str(i%8)
    print(triangle)
like image 173
Assem Avatar answered Dec 21 '25 23:12

Assem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!