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

I am writing a program that should have the output that looks like this.

1
22
333
4444
55555
666666
7777777
88888888
999999999 

With the above code I am pretty close, but the way my count is working it just literally counts up and up. I just need help getting it to only count to 9 but display like above. Thanks.

enter image description here

Celius Stingher
18.4k6 gold badges26 silver badges54 bronze badges
asked Jul 6, 2014 at 1:25
1
  • I came here to make sure I was indenting properly. Commented Jul 18, 2018 at 1:37

13 Answers 13

10

You're incrementing count in the inner loop which is why you keep getting larger numbers before you want to

You could just do this.

>>> for i in range(1, 10):
 print str(i) * i
1
22
333
4444
55555
666666
7777777
88888888
999999999

or if you want the nested loop for some reason

from __future__ import print_function
for i in range(1, 10):
 for j in range(i):
 print(i, end='')
 print()
answered Jul 6, 2014 at 1:36
Sign up to request clarification or add additional context in comments.

1 Comment

Yea this was per an assignment that required nested loops. So I needed it the second way. Thanks a bunch!
2

This works in both python2 and python3:

for i in range(10):
 print(str(i) * i)
answered Jul 6, 2014 at 1:34

Comments

2
for i in range(1,10):
 for j in range(0,i):
 print i,
print "\n"
answered May 28, 2015 at 14:24

Comments

1

The simple mistake in your code is the placement of count = count + 1. It should be placed after the second for loop block. I have made a simple change in your own code to obtain the output you want.

 from __future__ import print_function
 count = 0
 for i in range(10):
 for j in range(0, i):
 print(count,end='')
 count = count +1
 print()

This will give the output you want with the code you wrote. :)

answered Mar 21, 2017 at 16:33

Comments

1

This is one line solution. A little bit long:

print ('\n'.join([str(i)*i for i in range(1,10)]))
answered Nov 2, 2017 at 7:51

Comments

0

Change print(count, end='') to print(i + 1, end='') and remove count. Just make sure you understand why it works.

answered Jul 6, 2014 at 1:29

4 Comments

I suspect you mean i, not j.
@MattBryant fixed it as you were commenting.
That helped @David, but it starts my lines off at 2 instead of one now.
@iMaxPrime also start your inner range at 1, not 0. for j in range(1, i): Again, the important thing here is debugging a bit on your own and thinking things through, not just copy and pasting the code of others.
0

Is this what you want:

for i in range(10):
 print(str(i) * i)
answered Jul 6, 2014 at 1:36

Comments

0
"""2. 111 222 333 printing"""
for l in range (1,10):
 for k in range(l):
 print(l,end='')
print()
answered Dec 11, 2014 at 10:57

1 Comment

Please describe in short what is happening in your code for the benefit of other readers.
0
count = 1
for i in range(9):
 for j in range (-1, i):
 print (count, end = '')
 count = count + 1
 print (" ")
answered May 27, 2017 at 17:08

1 Comment

Please read this how-to-answer for providing a quality answer. Just post the code is not the best answer.
0

What you are trying to do involves a mathematical concept called repunit numbers

you could also do it as follows:

for i in range(1,n):
 print (int(i*((10**i)-1)/9))
answered Jul 28, 2015 at 2:07

Comments

0

I realised that the problem is solved but here's how you wanted your code to look like.

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

i think @Dannnno answer is shorter and straight to the point :)

Zian
5995 silver badges16 bronze badges
answered Jan 12, 2017 at 20:02

Comments

0

This can be your one line code to problem

print(''.join([str(x)*x+ '\n' for x in range(1,10)]))
answered May 4, 2019 at 22:21

Comments

-1

Others have suggested some interesting solutions but this can also be done mathematically using a simple observation. Notice that:

1 - 1*1

22 - 2*11

333 - 3*111

4444 - 4*1111

and so on ....

We can have a general formula for producing 1,11,111,1111,... at every iteration. Observe that:

1 = 9/9 = (10 - 1)/9 = (10^1 - 1)/9

11 = 99/9 = (100 - 1)/9 = (10^2 - 1)/9

111 = 999/9 = (1000 - 1)/9 = (10^3 - 1)/9

......

that is we have (10^i - 1)/9 for the ith iteration.

Now it is simple enough to implement. We will multiply i with the above formula in each iteration. Hence the overall formula is:

i*(10^i - 1)/9 (for every ith iteration). Here's the python code:

for i in xrange(1,10):
 print i*(10**i-1)/9

Hope this helps.

answered Mar 29, 2016 at 14:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.