python输出星号等腰三角形_颠倒python中的星号三角形

1586010002-jmsa.png

I am trying to use nested for loops to ask the user for an integer and then the program will output a reverse, upside down triangle, with the base of it having the number of asterisks and working its way down.

It's supposed to look like this:

*****

****

***

**

*

The code I have:

def pattern():

integer = requestInteger("Please enter a number")

for number in range(0, integer):

for variable in range(integer, 0, -1):

if variable - 1 > number:

sys.stdout.write(' ')

else:

sys.stdout.write('*')

sys.stdout.write('\n')

Outputs this:

*

**

***

****

*****

I'm not really sure how to go about changing my for loops around to make this work, and I've been trying for a while, so help would be much appreciated. Thank you

解决方案

My take would be something like this:

def pattern():

integer = int(raw_input("Please enter a number"))

for number in range(0, integer):

print " " * number + "*" * (integer - number)

Is this homework (nested loops are mandatory)? Hint: the * operator can be easily replaced by a for loop.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值