Python--使用fou循环用( * )打印空心的菱形
程序员文章站
2022-06-30 16:32:32
...
Python–使用fou循环用( * )打印空心的菱形
Python–使用fou循环用( * )打印空心的菱形:
例如:
layer = int(input("请输入要打印的层数:"))
for x in range(1, layer + 1):
# 先打印空格
space_num = layer - x
for y in range(0, space_num):
print(" ", end="")
# 再打印*
star_num = 2*x - 1
for y in range(0, star_num):
if y == 0 or y == star_num - 1:
print("*", end="")
else:
print(" ", end="")
# 换行
print("")
for x in range(layer - 1, 0, -1):
# 先打印空格
space_num = layer - x
for y in range(0, space_num):
print(" ", end="")
# 再打印*
star_num = 2*x - 1
for y in range(0, star_num):
if y == 0 or y == star_num - 1:
print("*", end="")
else:
print(" ", end="")
# 换行
print("")
输出举例: