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

python打印等腰三角形、菱形、空心菱形

2020-01-11 13:03 573 查看

python打印等腰三角形

n=int(input('>>>'))
width=2*n-1
for i in range(1,n+1):
print('{:^{}}\n'.format('*'*(2*i-1),width))

结果:

python打印菱形(完美菱形n必须为奇数)

n=int(input('>>>'))
width=n
for i in range(1,n+1):
if i<=(n+1)/2:
str='*'*(2*i-1)
else:
str='*'*(2*(n-i)+1)
print('{:^{}}\n'.format(str,width))


python打印空心菱形(完美菱形n必须为奇数)

n=int(input('>>>'))
width=n
for i in range(1,n+1):
if i<=(n+1)/2:
c=(2*i-1)
if c==1:
str='*'
else:
str='*'+' '*(c-2)+'*'
else:
c=(2*(n-i)+1)
if c==1:
str='*'
else:
str='*'+' '*(c-2)+'*'
print('{:^{}}\n'.format(str,width))

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