Python打印“菱形”星号代码方法
作者:laozhang 时间:2023-02-05 17:34:42
本人是一名python初学者,刚刚看到一道有趣的python问题,“用python如何在编译器中打印出菱形图案?”
因此决定尝试一下,代码不多,仅供参考。
代码
def printStar(intNum):
s = "*"
spaceLength = intNum
blockCount = int(intNum/2+1)
for i in range(spaceLength):
result = s.rjust(blockCount)
if i >= int(spaceLength/2):
print(result)
s = s[2:]
blockCount -= 1
else:
print(result)
s = s+(2*"*")
blockCount += 1
def oddOReven(intNum):
if intNum%2 == 0:
print("please input a odd num data")
else:
printStar(intNum)
if __name__ == '__main__':
while True:
try:
intNum = eval(input("please input a odd num data\n"))
oddOReven(intNum)
except BaseException as e:
print("Please input as 1/2/3... Errorcode:%s" % e)
运行结果:
标签:Python,菱形星号
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
我用Python给班主任写了一个自动阅卷脚本(附源码)
2023-11-16 07:43:25
![](https://img.aspxhome.com/file/2023/3/64303_0s.jpg)
卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法
2011-11-03 16:49:09
Bootstrap实现圆角、圆形头像和响应式图片
2023-08-12 07:16:59
![](https://img.aspxhome.com/file/2023/2/56052_0s.jpg)
MySQL手动安装方法与中文解决方案
2011-04-25 18:21:00
MooTools的Cookie类太“自作聪明”了
2008-11-13 12:59:00
把网页中的电话号码生成图片的ASP程序
2008-11-07 16:12:00
Javascript 中 String.replace( ) 的妙用
2008-08-05 18:08:00
Python中seaborn库之countplot的数据可视化使用
2023-08-10 20:38:56
![](https://img.aspxhome.com/file/2023/6/62026_0s.png)
python通过SSH登陆linux并操作的实现
2022-11-02 09:30:56
如何上传一个.SQL文件到远程主机并且执行它来部署一个SQL数据库
2007-09-23 13:03:00
![](https://img.aspxhome.com/file/uploadpic/20079/23/200792313732111.jpg)
XML正在接管Web服务 成为SOA的基础
2008-09-05 17:21:00
如何把Recordset转换成彩色的XML文件?
2009-11-02 20:22:00
Python如何基于smtplib发不同格式的邮件
2023-10-03 10:28:35
揭开HTML 5工作草稿的神秘面纱
2008-02-13 08:25:00
Python使用pyserial进行串口通信的实例
2022-03-07 15:30:09
实例剖析:MySQL数据库优化详解
2008-11-22 12:19:00
XML编程实例: ASP+XML打造留言本
2008-10-25 16:35:00
浅谈Javascript中的事件流和事件绑定
2009-07-20 17:13:00
![](https://img.aspxhome.com/file/UploadPic/20097/20/event-1-37s.gif)
Python实现自动添加脚本头信息的示例代码
2022-07-02 18:12:42
边框样式的写法总结
2009-01-18 13:00:00