'''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?

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()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.