在Python中打印形状 [英] Print shape in Python
本文介绍了在Python中打印形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,我想打印星号 *
的菱形:
$ b $在
*
的菱形(上部金字塔)的上半部分,带有 $
的b &
位于钻石(下金字塔)的下半部分,
def pyramid(n):
在范围内(n):
row =' *'*(2 * i + 1)
print(row.center(2 * n))
例如,如果调用的函数是 print shape(7)
,那么它会打印出[this image]。
任何想法?
解决方案
def shape(n):$ b (2 * n + 1):$ b $ if(i print$*(n - i)+** 2 * i +$ *(n - i)
elif i == n:
print* * 2 * n
elif i> n:
打印& *(i-n)+** 2 *(2 * n-i)+& *(i - n)
In Python, I'd like to print a diamond shape of asterisks *
:
- with
$
at the top half of the diamond (upper pyramid) where there isn't a*
, and - with
&
at the bottom half of the diamond (lower pyramid) where there isn't a*
.
So far, I only know how to make a pyramid that is right side up:
def pyramid(n):
for i in range(n):
row = '*'*(2*i+1)
print(row.center(2*n))
For example, if the function called was print shape(7)
, then it would print [this image].
Any ideas?
解决方案
def shape(n):
for i in range(2*n+ 1):
if (i < n):
print "$" * (n - i) + "*" * 2 * i + "$" * (n - i)
elif i == n:
print "*" * 2 * n
elif i > n:
print "&" * (i - n) + "*" * 2 * (2* n - i) + "&" * (i - n)
这篇关于在Python中打印形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文