【发布时间】:2019-02-09 05:22:06
【问题描述】:
喜欢:
*
* * *
* * * * *
----n=5----
注意:其中 n 将用户输入奇数。
【问题讨论】:
喜欢:
*
* * *
* * * * *
----n=5----
注意:其中 n 将用户输入奇数。
【问题讨论】:
遍历从0 到n / 2 的行号,然后将行号的两倍加一星居中。
n = 5
for l in range(n//2+1):
print(' '.join(['*'] * (l*2+1)).center(n*2))
给出:
*
* * *
* * * * *
【讨论】:
def asterisk_triangle(n):
"""
takes an integer n and then returns an
asterisk triangle consisting of (n) many columns
"""
x = 1
while (x <= (n+1)/2):
space = int((n+1)/2-x)
print(" " *space*2, end='')
print("* " * (2*x-1))
x = x + 1
return
# call asterisk_triangle function here
# here n=9
asterisk_triangle(9)
输出:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
【讨论】: