从控制台输入n,利用format方法生成一个星号三角形

floorStr = input('请输入一个层数:')
floor = int(floorStr)
num = floor * 2 -3   # 17
while floor > 0:
    print("{:<{a}}{:*<{b}}".format(" ","",a =floor,b=(num - (floor - 2)*2)))
    floor -= 1
'''
print("{:<10}{:*<1}".format(" ",""))
print("{:<9}{:*<3}".format(" ",""))
print("{:<8}{:*<5}".format(" ",""))
print("{:<7}{:*<7}".format(" ",""))
print("{:<6}{:*<9}".format(" ",""))
print("{:<5}{:*<11}".format(" ",""))
print("{:<4}{:*<13}".format(" ",""))
print("{:<3}{:*<15}".format(" ",""))
print("{:<2}{:*<17}".format(" ",""))
print("{:<1}{:*<19}".format(" ",""))
'''
上一篇:c – 为什么许多(旧)程序使用floor(0.5输入)而不是round(input)?


下一篇:JS取整的四种方法