python_打印菱形

菱形

应用for循环实现,当然还有别个方法

# 练习打印*菱形
"""
   *
  * *
 *   *
*     *
 *   *
  * *
   *
"""
n = int(input('请输入菱形每边星星个数:'))
# 1.打印菱形上面一半
for i in range(n):
    # 2.第一行
    if i == 0:
        print(" " * (n - 1 - i) + "*")
    else:
        s = " " * (n - 1 - i) + "*" + (i * 2 - 1) * " " + "*"
        print(s)
# 1.打印菱形下面一半
for i in range(n - 1):
    # 2.最后一行
    if i == n - 2:
        print(" " * (n - 1) + "*")
    else:
        s = " " * (i + 1) + "*" + (n * 2 - 5 - 2 * i) * " " + "*"
        print(s)

你可能感兴趣的:(python_打印菱形)