Nested Loop Python

后端 未结 13 2218
一个人的身影
一个人的身影 2020-12-09 23:40
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

相关标签:
13条回答
  • 2020-12-10 00:29

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

    0 讨论(0)
  • 2020-12-10 00:30

    Is this what you want:

    for i in range(10):
        print(str(i) * i)
    
    0 讨论(0)
  • 2020-12-10 00:33
    """2. 111 222 333 printing"""
    
    for l in range (1,10):
        for k in range(l):
            print(l,end='')
    print()
    
    0 讨论(0)
  • 2020-12-10 00:35
    count = 1
    for i in range(9):
        for j in range (-1, i):
            print (count, end = '')
        count = count + 1
        print (" ")
    
    0 讨论(0)
  • 2020-12-10 00:40

    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. :)

    0 讨论(0)
  • 2020-12-10 00:40

    This is one line solution. A little bit long:

    print ('\n'.join([str(i)*i for i in range(1,10)]))
    
    0 讨论(0)
提交回复
热议问题