Python基础-画菱形

方法一

n = int(input('请输入:'))
for i in range(1, n, 2):
    print(('*'*i).center(n))
for i in reversed(range(1, n-1, 2)):
    print(('*'*i).center(n))

方法二

n = int(input('请输入:'))
i = 1
while i <= n:
    print((n-i)*' '+'* '*i)
    i += 1
    while n < i < 2*n:
        print((i-n)*' '+'* '*(i-2*(i-n)))
        i += 1

方法三

i = 1
p = int(input('请输入:'))
while i <= p:
    print(' '*(p-i) + '*'*(2*i-1))
    i += 1
n = p-2
while i <= 2*p:
    print(' '*(i-p)+ '*'*(2*p - i + n))
    i += 1
    n -= 1

方法四

intNum = int(input('请输入:'))
s = "*"
blockCount = int(intNum / 2 + 1)
i = 1
while i <= intNum:
    result = s.rjust(blockCount)
    if i >= int(intNum / 2):
        print(result)
        s = s[2:]
        blockCount -= 1
    else:
        print(result)
        s = s + (2 * "*")
        blockCount += 1
    i += 1

 

posted @ 2018-09-03 10:36  Python不秋  阅读(3259)  评论(0编辑  收藏  举报