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
I know it's been such a lang time, but i want to share my approach to the question.
My take would be something like this:
Is this homework (nested loops are mandatory)? Hint: the
*
operator can be easily replaced by afor
loop.You can use
reversed range
: