Question :
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.
Answer #1:
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()
Answer #2:
This works in both python2 and python3:
for i in range(10):
print(str(i) * i)
Answer #3:
for i in range(1,10):
for j in range(0,i):
print i,
print "n"
Answer #4:
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. 🙂
Answer #5:
This is one line solution.
A little bit long:
print ('n'.join([str(i)*i for i in range(1,10)]))
Answer #6:
Change print(count, end='')
to print(i + 1, end='')
and remove count
. Just make sure you understand why it works.
Answer #7:
Is this what you want:
for i in range(10):
print(str(i) * i)
Answer #8:
"""2. 111 222 333 printing"""
for l in range (1,10):
for k in range(l):
print(l,end='')
print()