简体   繁体   English

在 python 中构建一个星号金字塔,唯一的输入是金字塔应该有的层数,

[英]Building a pyramid of asterisks in python, the only input being the number of layers the pyramid should have,

Its a beginner project, here's what I have got so far:它是一个初学者项目,这是我到目前为止所得到的:

def tower_builder(n_floors):
   tower = []
   x=1
   for i in range(1,n_floors+1):
        tower.append((x)*"*")
        x=x+2
   return tower

the only problem I have is returning the contents of the array "tower" on separate lines.我唯一的问题是在单独的行上返回数组“塔”的内容。 How do I do this.我该怎么做呢。 Also I know my code is not the most efficient but I am learning.我也知道我的代码不是最有效的,但我正在学习。 Thanks!谢谢!

You could use format specifier to center the tower:您可以使用格式说明符使塔居中:

def tower_builder(n_floors):
    for i in range(n_floors):
        print(f"{'*' * (2*i+1): ^80}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM