``````totalRows = int(raw_input ("Please enter a number: "))

for currentRow in range(1, totalRows+1):
for currentCol in range (1, currentRow+1):
print '*',
print
print

for currentRow in range (1, totalRows+1):
for currentCol in range(1, currentRow+1):

print '*',
print``````

Output

*
* *
* * *
* * * *

*
* *
* * *
* * * *

Having some problems trying to rearrange this code so that this output prints instead. So basically the bottom triangle should mirror the top but all my logical approaches such as reversing the for loop hasn't worked at all. Any step into the right direction would be greatly appreciated

*
* *
* * *
* * * *

* * * *
* * *
* *
*

3
Contributors
3
Replies
5
Views
8 Years
Discussion Span
Last Post by Ultralisk

You can use the code you have with a range from total rows to zero and a step of -1, or a counter for the number of stars to print.

``````total_rows = 5

num_stars = total_rows
for row in range (total_rows):
print '*' * num_stars
num_stars -= 1``````

Edited by woooee: n/a

Like this?

``````totalRows = int(raw_input ("Please enter a number: "))

for currentRow in range(1, totalRows + 1):
print '* ' * currentRow

print

for currentRow in reversed(range(1, totalRows + 1)):
print '* ' * currentRow``````

So there is an actual 'reversed' command? I had no idea. Simplifies everything I already had done. Thanks!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.