Converting for loops to while loops in python

后端 未结 3 1517
既然无缘
既然无缘 2020-12-11 08:55

I am struggling to find an efficient way to convert these for loops to a working set of while loops. Any Suggestions? I am using 2.7

def pr         


        
3条回答
  •  醉话见心
    2020-12-11 09:40

    You can simplify it and just use one while loop:

    def printTTriangle(height):
        row = 1
        while row <= height:
            print 'T '*row
            row += 1
    

    AND if you are not obsessed with while loops, here is a one liner:

    def printTTriangle(height):
        print "\n".join(['T '*row for row in range(1, height+1)])
    

提交回复
热议问题