python - Python 中的星号三角形

标签 python

我正在尝试创建一个程序,允许我在 Python 程序中使用星号制作“金字塔”或“三角形”。我已经开始编写代码,但似乎无法弄清楚。

这是我想出的代码:

    def triangle():
            totalRows = int(eval(input("How big? ")))
            for currentRows in range(1,totalRows+1):
                for currentCol in range (1, currentRows+1):
                            print("*", end = " ")
    triangle()

最终结果应该反射(reflect)这一点!

How big? 1
* 
------------------------------------------------------
How big? 2
 * 
* * 
------------------------------------------------------ 
How big? 3
  * 
 * * 
* * * 
------------------------------------------------------ 

最佳答案

对 RParadox 的解决方案进行轻微修改以符合要求:

for i in range(totalRows + 1):
    numWhite = totalRows - i
    print ' ' * numWhite + '* ' * i

关于python - Python 中的星号三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13260642/

相关文章:

python - 打印随机 unicode 字符(不使用 exec)

python - 在输入文件中查找确切的字符串

python - 使用 Numpy 向量化标量函数调用

python - Django - 用户在 View 中上传 S3 文件

python - RuntimeError : Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] 有 3 个 channel ,但得到了 1 个 channel

python - 如何替换 bool 列表序列中的值?

python - 处理 Python 数学运算的准确性

python - TimeDistributed 与 TimeDistributedDense Keras

python - 使用 OpenCV 改进图像中的矩形轮廓检测

python - 如何使用 Pandas 获取每个组中的上一个和中间日期