我正在尝试在另一个内部打印一个三角形,所以我有代码
w = int(input(“Input width of first triangle: “)
c1 = input(“Pick a character: “)
c2 = input(“Pick another character: “)
print(“One: “)
for i in range(“width, 0, -2):
print(‘{:^{str_len}}’.format(c1 * i, str_len = w * 2))
如果c1为*且w为5则会打印:
*****
***
*
现在我希望它如此,如果c2是+它也会打印:
+
+++
+++++
+*****+
+++***+++
+++++*+++++
就像一个三角形,里面有第一个,但我不知道它会如何起作用
答案 0 :(得分:0)
declare var $: any;
的产率:
h = 10
h2 = 5
assert(2*h >= h2)
for i in range(h):
if i >= h2 and i <= 2*h2: #start printing second triangle as well
s ='%s%s%s' % ('+'*(i-h2), '*'*(2*h2 - i ), '+'*(i-h2))
s = " ".join(s)
print('%s%s' % (' ' * (h - i), s))
else:
s = " ".join('+'*i)
print('%s%s' % (' '*(h-i),s))