Python基础-画菱形
方法一
n = int(input('请输入:')) for i in range(1, n, 2): print(('*'*i).center(n)) for i in reversed(range(1, n-1, 2)): print(('*'*i).center(n))
方法二
n = int(input('请输入:')) i = 1 while i <= n: print((n-i)*' '+'* '*i) i += 1 while n < i < 2*n: print((i-n)*' '+'* '*(i-2*(i-n))) i += 1
方法三
i = 1 p = int(input('请输入:')) while i <= p: print(' '*(p-i) + '*'*(2*i-1)) i += 1 n = p-2 while i <= 2*p: print(' '*(i-p)+ '*'*(2*p - i + n)) i += 1 n -= 1
方法四
intNum = int(input('请输入:')) s = "*" blockCount = int(intNum / 2 + 1) i = 1 while i <= intNum: result = s.rjust(blockCount) if i >= int(intNum / 2): print(result) s = s[2:] blockCount -= 1 else: print(result) s = s + (2 * "*") blockCount += 1 i += 1
作者:枪林弹雨丶
若标题中有【转载】字样,则本文版权归原作者所有。若无转载字样,
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
请注意:作者五行缺钱,如果喜欢这篇文章,请随意打赏!
![](https://www.cnblogs.com/images/cnblogs_com/Python-XiaCaiP/1389040/t_right_top.png)
![](https://www.cnblogs.com/images/cnblogs_com/Python-XiaCaiP/1389040/t_right_bottom.png)