Draw an M-shaped pattern with nested loops

前端 未结 4 1144
慢半拍i
慢半拍i 2020-12-12 02:11

I just started Python 2.7 very recently, but I\'m stuck at this a problem:

Make a program that prints an \"M\" pattern of asterisks. The user shall in

4条回答
  •  独厮守ぢ
    2020-12-12 02:53

    The dumbest approach is to print an increasing number of stars justified left and right until their number reaches N//2 (floor division), the rest is easy.

    for n in range(1,N//2+1):
        line = bytearray(' ' * N)
        line[:n] = line[-n:] = '*' * n
        print line
    

    Ha, with this, you can even loop all the way to N instead of N//2 - since assigning to intersecting ranges will do no harm.

提交回复
热议问题