user2955471
user2955471

Reputation: 63

Fixing code to make a triangle

I'm a beginner and trying to learn Python by myself. I've been working on coding some basic shape exercises, so far I have the following code to make a diagonal.

size = input('Please enter the size: ')
chr  = raw_input('Please enter the drawing character: ')

row = 1
while row <= size:

    col = 1
    while col < row:
        print ' ', 
        col = col + 1
    print chr

    row = row + 1
print ''

I get this output :

X
 X
  X
   X

I would love some help on how to make this into a triangle like this....

X X X X
  X X X
    X X
      X

Any explanations on how to make the loop necessary for the characters to show up to make the triangle shaped output would be appreciated.

Upvotes: 5

Views: 3257

Answers (3)

Simeon Visser
Simeon Visser

Reputation: 122376

You can do:

>>> for i in xrange(4):
...     print '  ' * i + 'X ' * (4 - i)
...
X X X X
  X X X
    X X
      X

The value of i goes from 0 to 3 (by using xrange) and it prints the string ' ' (two spaces) i number of times and it prints 'X ' in total (4 - i) number of times. This means it'll print the inverted triangle as desired.

Upvotes: 5

BartoszKP
BartoszKP

Reputation: 35891

The simplest fix is just to print the character print chr, instead of space print ' ',.

To invert the result vertically a simple change in the condition, from while col < row: to while col < (size - row + 1): will suffice. And finally, to invert it horizontally, add a loop that prints spaces:

size = input('Please enter the size: ')
chr  = raw_input('Please enter the drawing character: ')

row = 1
while row <= size:

    col = 1
    while col < row:
        print ' ',
        col = col + 1

    col = 1
    while col < (size - row + 1):
        print chr, 
        col = col + 1
    print chr

    row = row + 1
print ''

And finally, you can simplify this a bit:

size = input('Please enter the size: ')
chr  = raw_input('Please enter the drawing character: ')

row = 1
while row <= size:

    col = 1
    while col < size:
        if col < row:
            print ' ',
        else:
            print chr,
        col = col + 1
    print chr

    row = row + 1
print ''

Result:

Please enter the size: 4
Please enter the drawing character: x
x x x x
  x x x
    x x
      x

And of course you can make this really simple looking at Simeon Visser's answer.

Upvotes: 3

Inbar Rose
Inbar Rose

Reputation: 43447

I wrote some code earlier that does shapes, its a bit more detailed than what you might need, but here it is:

>>> def make_triangle(size, siblings=1, step=1, char='*'): 
    return '\n'.join([' '.join(line) for line in [[char * (i-(step*sib)) + ' ' * (((size-(step*sib))-(i-(step*sib)))) for sib in xrange(siblings)] for i in xrange(1, size+1)]])

Making some triangles:

>>> print make_triangle(4, char='X') # standing
X   
XX  
XXX 
XXXX
>>> print make_triangle(4, char='X')[::-1] # hanging (reversed)
XXXX
 XXX
  XX
   X

Some of the extra features:

>>> print make_triangle(6,3,2)
*                 
**              
***    *       
****   **     
*****  ***  * 
****** **** **

Upvotes: 2

Related Questions