首頁 >後端開發 >Python教學 >python中怎麼實現星星排列

python中怎麼實現星星排列

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼原創
2019-06-25 14:01:557170瀏覽

python中怎麼實現星星排列

python中怎麼實現星星排列?下面帶給大家幾種排列陣型。

星星陣型1

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

python中怎麼實現星星排列

#星星陣型2

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

python中怎麼實現星星排列

相關推薦:《Python影片教學

星星陣型3

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

python中怎麼實現星星排列

星星陣型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)

python中怎麼實現星星排列

#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)

python中怎麼實現星星排列

#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)

python中怎麼實現星星排列

以上是python中怎麼實現星星排列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn