0
'''Triangle generator''' 

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

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

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

Current output:

* 
* * 
* * * 
* * * * 

* * * * 
* * * 
* * 
*

I would like to know if there is a way to reflect the output triangles so that they print like this? So that the triangle starts to build from left to right, instead of decreasing from left to right

* 
    * * 
  * * * 
* * * * 

* * * * 
  * * * 
    * * 
      *

Is it something to do with spacing?

Edited by Ultralisk: I have a double post for some reason

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by griswolf
0

One way: Instead of directly printing the '*', save them into a string, then at the end print("%[I]<width>[/I]s"%theAsteriskString) Another way: Instead of printing the asterisk, append it to an array. Kind of like this (but using your code instead of my for loop), then convert and print the array:

blanks = [' ']*[I]<width>[/I]
asterisks = []
for i in range(theCurrentLineLength):
  asterisks.append('*')
asterisks = (blanks + asterisks)[:-[I]<width>[/I]]
print(" ".join(asterisks))

Another way: Build the asterisks array, then append "enough" blanks. Then before you convert and print the array, do this: asterisks.reverse()

Edited by griswolf: negative width in option two

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.