Python 官方文档:入门教程 => 点击学习
示例1: #!/usr/bin/python # -*- coding: UTF-8 -*- # 根据输入打印 rows = int(raw_input('please input number:')) # 打印菱形上半部分 for i i
示例1:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 根据输入打印
rows = int(raw_input('please input number:'))
# 打印菱形上半部分
for i in range(rows):
for j in range(rows - i):
print " ",
j += 1
for k in range(2 * i - 1):
if k == 0 or k == 2 * i - 2:
print "*",
else:
print " ",
k += 1
print "\n"
i += 1
# 打印菱形的下半部分
for i in range(rows):
for j in range(i):
print " ",
j += 1
for k in range(2 * (rows - i) - 1):
if k == 0 or k == 2 * (rows - i) - 2:
print "*",
else:
print " ",
k += 1
print "\n"
i += 1
print 'the end';
代码截图1:
运行截图1:
示例2:
#!/usr/bin/Python
# -*- coding: UTF-8 -*-
# 根据输入打印
rows = int(raw_input('please input number:'))
# 打印菱形上半部分
for i in range(rows):
for j in range(rows - i):
print " ",
j += 1
for k in range(2 * i - 1):
print "*",
k += 1
print "\n"
i += 1
# 打印菱形的下半部分
for i in range(rows):
for j in range(i):
print " ",
j += 1
for k in range(2 * (rows - i) - 1):
print "*",
k += 1
print "\n"
i += 1
print 'the end';
代码截图2:
运行截图2:
查看更多技术请移步:https://ui.29mn.com
--结束END--
本文标题: Python打印菱形
本文链接: https://lsjlt.com/news/191542.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0