Python间隔三角形 [英] Python Spaced Triangle
本文介绍了Python间隔三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该写一个最终这样的程序:
I'm supposed to write a program that ends up such as this:
* *
* *
* *
*
我已经为常规代码编写了代码,但是我不确定如何在其中合并空格。
I have the code written for a regular one, but I'm not sure how to incorporate spaces into it.
def triangle(i, t = 0):
if i == 0
return 0
else:
print ' ' * (t + 1) + '*' * (i * 2 - 1)
return triangle(i - 1, t + 1)
咨询?
推荐答案
尝试:
def triangle(i, t = 0):
if i == 0:
print (t+1) *' '+ '*'
else:
print ' ' * (t + 1)+ '*' + ' ' * (i * 2 - 1) + '*'
triangle(i - 1, t + 1)
triangle(5)
此代码打印:
* *
* *
* *
* *
* *
*
这篇关于Python间隔三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文