欢迎光临杨雨的个人博客站!

杨雨个人网站-杨雨个人博客-杨照佳

杨雨个人博客网站

关注互联网和搜索引擎的个人博客网站

首页 > WEB开发 > Python教程 >

python中怎么实现星星排列

发布时间:2019-06-25  编辑:杨雨个人博客网站   点击:   

python中怎么实现星星排列

python中怎么实现星星排列?下面给大家带来几种排列阵型。

星星阵型1

def stars1(n):
for i in range(1,n+1):
str = "*"*i
print str
if __name__ ==  "__main__":
stars1(5)

1561441675725724.png

星星阵型2

def stars2(n):
for i in range(1,n+1):
str = '*'*(n+1-i)
print str
if __name__ ==  "__main__":
stars2(5)

1561441687712238.png

星星阵型3

def stars3(n):
for i in range(1,n+1):
str = ' ' * (n-i) + '*' * i
print str
if __name__ ==  "__main__":
stars3(5)

1561441696731485.png

星星阵型4

def stars4(n):
for i in range(1,1+n):#total n line(s)
str = ' '*(i-1) + '*'*(n+1-i)
print str
if __name__ ==  "__main__":
stars4(5)

1561441706277737.png

5. 菱形阵型

def stars5(n):
    RANGE1 = [2*i+1 for i in range(n)]
    RANGE2 = [2*i+1 for i in range(n)[::-1]][1:]
    RANGE = RANGE1 + RANGE2
    RANGE_1 = [i for i in range(n)[::-1]]
    RANGE_2 = [i for i in range(n)[1:]]
    RANGE_12 = RANGE_1 + RANGE_2
    for i in range(len(RANGE)):
        print ' '*RANGE_12[i] + '*'*RANGE[i]
    
if __name__ ==  "__main__":
stars5(5)

1561441719893993.png

6. 沙漏阵型

def star6(n):
    a = [i*2+1 for i in range(n)[::-1]]
    b = [i*2+1 for i in range(n)][1:]
    aa = a + b
    
    d = [i for i in range(n)]
    e = [i for i in range(n)[::-1]][1:]
    dd = d + e
    
    for j in range(len(dd)):
        print ' '*dd[j] + '*'*aa[j]   
         
if __name__ == "__main__":
    star6(5)

1561441729585142.png 本文地址:http://itbyc.com/Python/22015.html
转载请注明出处。

分享是一种快乐,也是一种美德:
博客首页 | WEB开发 | 网站运营 | CMS使用教程 滇ICP备14002061号-1