在python中打印带间隔范围输出的星形图案

时间:2018-03-18 13:42:46

标签: python python-3.x

到目前为止,我有打印星形模式的解决方案

lower = int(input("what is the minimum nmber of stars: "))
upper = int(input("what is the maximum number of stars: "))
interval=int(input("enter the interval:"))

for i in range (lower,upper+1,interval):
    print((lower) * ' ' + i * '*')

仅打印出星星,例如:

***

我正在寻找的是

***(3 stars)

********(7 stars)

***********(11 stars)

如何打印文字部分(x星)?

1 个答案:

答案 0 :(得分:1)

使用此:

lower = int(input("what is the minimum nmber of stars: "))
upper = int(input("what is the maximum number of stars: "))
interval=int(input("enter the interval:"))

for i in range (lower,upper+1,interval):
    print(lower*' ' + i*'*', '({} stars)'.format(i))

输出:

what is the minimum nmber of stars: 1
what is the maximum number of stars: 4
enter the interval:1
 * (1 stars)
 ** (2 stars)
 *** (3 stars)
 **** (4 stars)