您的位置:首页 > 编程语言 > Python开发

Python中使用for循环打印菱形,三角形,空心菱形等图形

2020-01-12 15:25 381 查看

三角形

def shape1():
for i in range(1, 5):
str1 = "*" * (i * 2 - 1)
print(str1.center(7))

if __name__ == "__main__":
shape1()

实心菱形

def shape2():
for i in range(1, 8):
if i < 4:
str2 = "*" * (i * 2 - 1)
else:
str2 = "*" * (15 - 2 * i)
print(str2.center(7))

if __name__ == "__main__":
shape2()

空心菱形

def shape3():
s = "*"
for i in range(1, 8):
if i == 1 or i == 7:
str1 = s.center(7)
elif i < 5:
str1 = s.rjust(5 - i)
str1 += s.rjust(2 * i - 2)
else:
str1 = s.rjust(i - 3)
str1 += s.rjust(14 - 2 * i)
print(str1)

if __name__ == "__main__":
shape3()
  • 点赞
  • 收藏
  • 分享
  • 文章举报
SupremeGGG 发布了1 篇原创文章 · 获赞 0 · 访问量 248 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: