3

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!

4
  • 4
    You don't need i+=1, the for loop increments the variable automatically. Commented Jan 21, 2016 at 17:07
  • Is 8 related to n or just some sort of cyclic limitation you wanted to be? Commented Jan 21, 2016 at 17:08
  • Oops, I forgot to delete it from the code! It's because I was using a while loop before;) Commented Jan 21, 2016 at 17:10
  • It is not, bigOther answered my question right! Commented Jan 21, 2016 at 17:10

2 Answers 2

9

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try this, I'm pretty sure this is what I missed! Thanks a lot:D
0

I Like methods...

def yourPattern(b,s):
    r = ""
    q = [str(k) for k in range(1,b+1)*(s/b)+range(1,b+1)[0:s%b]]
    for k in q:#    ^Iterator[^Array ^Scale^add^Array   ^Portion]
        r += k # Put string together
    return(r)

def pattern(b,l):
    r = ""
    for k in range(1,l+1):
        r+=yourPattern(b,k)+"\n"
    return(r)
print pattern(8,18)

o(4n)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.