python嵌套循环三角形,如何在Python中使用for循环制作三角形

I'm new to programming and have been experimenting with for loops to try and figure out how to make different shapes however I have encountered a problem that I cannot solve.

So far I have been able to create a rectangle of 1's as shown below in a 5X5

for i in range(0, 5):

X = 0

for j in range(0, 5):

X = (X*10)+1

print(X)

I would like to be able to modify this code using a for loop to be able to create a triangle like so...

1 1 1

1 1

1

How would I go about doing this? Is there also a way that I could input n and dependant on whatever number is input the program creates a triangle of that size? for example I input a 5 and it creates a triangle like...

11111

1111

111

11

1

Ive tried various different things but i'm unable to figure it out.

解决方案def triangle(c, n):

for i in xrange(n, 0, -1):

print c * i

triangle("X", 5)

prints:

XXXXX

XXXX

XXX

XX

X

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值