0

I'm trying to output a right triangle with numbers. Here is what I have so far:

for i in range(1, 10):
    for j in range(i):
    print(i, end='')
print()

My output is this

1
22
333
4444
55555
666666
7777777
88888888
999999999

My question is this. Can I make these numbers run in sequence using a nested for loop example:

1
12
123
1234
12345
123456
1234567
12345678
123456789

I've tried about 6 other sets and mostly keep getting the same output or multiple errors. Can anyone help me out with this?

0

2 Answers 2

1

You might want to consider what happens next? This gives you a few ideas.

import itertools

for i in range(1, 21):
    cycle = itertools.cycle([1,2,3,4,5,6,7,8,9,0])
    for j in range(i):
        print(next(cycle), end="")
    print()

This cycles through the digits giving you output as follows:

1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
12345678901234
123456789012345
1234567890123456
12345678901234567
123456789012345678
1234567890123456789
12345678901234567890

Or alternatively:

for i in range(1, 21):
    for j in range(i):
        print(((j % 10) + 1) % 10, end="")
    print()
Sign up to request clarification or add additional context in comments.

5 Comments

This works out perfectly. Like I said I'm still new to this language now I see in yours that 0 is actually coming up. How does that work out? because anytime I try to run 0 as part of my range I always start off with 1 instead of 0 like I'm trying for
The first example just cycles through the digits, the second one makes use of the % modulus operator to give a remainder, e.g. 12 % 10 = 2 and 0 % 10 = 0, so add 1 and it would then cycle from 1 to 10, another % would make it cycle 1 to 0.
I was just getting ready to post back to you that I had figured that portion out. Thank you so much for your help on this now its starting to make a little more sense.
so out of curiosity why is it when I try to run a set of higher numbers in stepped count i.e. 10 11 12 13 14 15 16 17 18 19 I tried to change around the variables but either get the exact printing as before or it prints nothing but 0's. possibly need to build up a library of some type? not sure how yet but ill do some more reading tomorrow when I have the chance
apologies for blowing up your feed I found where my problem was after I started reading through it again. in the mad rush to develop the loop code I totally had a blond moment and forgot to add height variable into the final equation. As soon as that was input it took straight off
1

j can do more than a counter:

for i in range(1, 10):
    for j in range(i):
        print(j + 1, end='')
    print()

1 Comment

thank you for your help, I can see where I need to reread a number of sections I had to have missed this

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.