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.

3
  • What output are you getting? That would help.... Commented Oct 17, 2013 at 21:51
  • Since your problem seems to be getting rid of the newlines only, this question is probably a duplicate of stackoverflow.com/questions/493386 Commented Oct 17, 2013 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 Commented Oct 17, 2013 at 22:01

5 Answers 5

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

Comments

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

Comments

1
def main():
    for i in range(10):
        for n in range(i):
            print(i, end='')

        print()

main()

5 Comments

The fact it's that easy makes me sad, but I guess that's why I need practice
There's always a learning curve, don't worry. Pick a book you like, print it and take your time...
This prints two newlines after each number series.
@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.
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.
0
def main(n):
  for x in range(n):
    if x>0:
      print(str(x)*x+'\n')

main(n)

1 Comment

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.
0

One line, and don't use str:

print((10**(i)//9)*i)

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.