1

I need to output this

1
22
333
4444
55555
666666
7777777
88888888
999999999

So far all I have is:

def main():
    for i in range(10):
        for n in range(i):
            print(i)

    return
main()

I get all the correct numbers, it's just not formatted correctly. If you guys could throw some hints my way I'd appreciate it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
three-six-oh
  • 47
  • 1
  • 4
  • 8
  • What output are you getting? That would help.... – Edward Oct 17 '13 at 21:51
  • Since your problem seems to be getting rid of the newlines only, this question is probably a duplicate of http://stackoverflow.com/questions/493386 – us2012 Oct 17 '13 at 21:51
  • 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 This is my output right now, and I have to use a nested for loop for the assignment – three-six-oh Oct 17 '13 at 22:01

5 Answers5

13

Simply multiply the string version:

for i in range(10):
    print(str(i) * i)

Output:

>>> for i in range(10):
...     print(str(i) * i)
... 

1
22
333
4444
55555
666666
7777777
88888888
999999999

If you have to use a nested loop, tell the print() function to not print a newline in the inner loop by setting the end option to an empty string; an empty print() call in the outer loop then suffices to add the necessary newline:

for i in range(10):
    for n in range(i):
        print(i, end='')
    print()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

Pretty much every answer stated above uses string to accomplish this problem. There is another way, a more easier way by applying simple math using the formula a_n= n * ((10^n - 1)/9) for n<=9. Please refer the snippet below (Using Python 3)

for i in range(1,n)
    print i*(((pow(10,i)-1) // 9)

n = 4

Output

1 
22
333
4444
manju4ever
  • 325
  • 1
  • 13
1
def main():
    for i in range(10):
        for n in range(i):
            print(i, end='')

        print()

main()
Asotos
  • 995
  • 11
  • 14
  • The fact it's that easy makes me sad, but I guess that's why I need practice – three-six-oh Oct 17 '13 at 22:04
  • There's always a learning curve, don't worry. Pick a book you like, print it and take your time... – Asotos Oct 17 '13 at 22:08
  • 1
    This prints *two* newlines after each number series. – Martijn Pieters Oct 17 '13 at 22:45
  • @MartijnPieters You're right, I haven't noticed the exact spacing in the question. I edited out the '\n' argument in the last print statement. – Asotos Oct 18 '13 at 09:12
  • The exact spacing was wrong in the original post; a classical error made when first encountering MarkDown and Stack Overflow, where an editor tries to come to grips with the way paragraphs are handled (you need a double newline or else text is flowed into paragraphs). In this case the solution was to use preformatted text instead by indenting. – Martijn Pieters Oct 18 '13 at 09:47
0
def main(n):
  for x in range(n):
    if x>0:
      print(str(x)*x+'\n')

main(n)
ryanevius
  • 1
  • 1
  • The OP is using the print *function*, not *statement*, so there should be parentheses. OR, at least you might want to point out this answer is valid for python2, not python3. – SethMMorton Oct 18 '13 at 01:49
0

One line, and don't use str:

print((10**(i)//9)*i)
Tue Nguyen
  • 11
  • 1