这期内容当中小编将会给大家带来有关怎么在python中实现星星排列,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
星星阵型1
def stars1(n): for i in range(1,n+1): str = "*"*i print str if __name__ == "__main__": stars1(5)
星星阵型2
def stars2(n): for i in range(1,n+1): str = '*'*(n+1-i) print str if __name__ == "__main__": stars2(5)
星星阵型3
def stars3(n): for i in range(1,n+1): str = ' ' * (n-i) + '*' * i print str if __name__ == "__main__": stars3(5)
星星阵型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)
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)
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中实现星星排列了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。