Python打印“菱形”星号代码方法,python星号
Python打印“菱形”星号代码方法,python星号
本人是一名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获取本机所有网卡ip,掩码和广播地址实例代码,
- python实现求最长回文子串长度,python回文
- Python实现简单文本字符串处理的方法,python字符串
- Python使用Tkinter实现机器人走迷宫,pythontkinter
- Python深度优先算法生成迷宫,python迷宫
- python正则表达式及使用正则表达式的例子,python正则表
- 浅谈使用Python内置函数getattr实现分发模式,pythongetat
- 一道python走迷宫算法题,python走迷宫算法题
- Python的SimpleHTTPServer模块用处及使用方法简介,
- python导出hive数据表的schema实例代码,hiveschema
评论关闭