DarkLordMephiles
DarkLordMephiles

Reputation: 13

For/While Loops to make *-triangles

For an assignment in a coding class, I was supposed to find a way to have Python make a triangle of asterisks, looking like this:

   x
  xx
 xxx 

Whatever I do with my code, however, I can't manage to pull that off. The best I can get is:

x
xx
xxx 

All of this has to be using only for-loops and while-loops, like what I'm using here. The code I'm using is

for a in range(1, 15):
 for a2 in range(14-a, 0, 1):
     print(" ", end='')
 for a1 in range(1, a+ 1):
     print("*", end='')
 print()

The -'s are there to represent indent. How do I make this work the way I want it to?

Edit: Turns out, I only noticed half of my problem. The code I wound up using, here, makes the two triangles I need, but stacks them one below the other. What I'm having trouble with now is making them appear side-by-side, like an M shape.

for b in range(1, 10):
    for b2 in range(9-b, 0, 1):
        print(" ", end='')    
    for b1 in range(1, b+ 1):
        print("*", end='')
    print()

for a in range(1, 10):
    for a2 in range(9-a, 0, -1):
        print(" ", end='')
    for a1 in range(1, a+ 1):
        print("*", end='')
    print()

I know I'm missing something, I just can't see what it is.

Upvotes: 1

Views: 1431

Answers (5)

George V. Reilly
George V. Reilly

Reputation: 16313

You should look into using str.format.

  • "{0}+++{0}".format('Hello!') produces two copies of the zeroth argument to format (here 'Hello!'), separated by three plusses: Hello!+++Hello!.
  • "{:<4}".format('x') left-justifies 'x' in a 4-character field; i.e., 'x '.
  • "{:>4}".format('x') right-justifies 'x' in a 4-character field; i.e., ' x'.
  • "{:>{}}".format('x', width) right-justifies 'x' in a width-character field.
  • 'ab' * 4 yields 4 copies of 'ab'; i.e., 'abababab'.

Putting them together:

>>> WIDTH = 4
>>>
>>> for a in range(1, WIDTH+1):
...     print("{0:<{1}}{0:>{1}}".format('*' * a, WIDTH))
...
*      *
**    **
***  ***
********

Handy references: PyFormat and Python String Format Cookbook.

Upvotes: 1

Yehuda Katz
Yehuda Katz

Reputation: 197

Using most basic commands:

for i in range(8, 0, -1):
    print(i * ' ', '%')

Output:

         %
        %
       %
      %
     %
    %
   %
  %

Upvotes: 0

Andrea Dusza
Andrea Dusza

Reputation: 2110

Here is how to solve both problems using only for loops. This approach is not very pythonic, but this is what you requested.

Solution for single triangle problem

   x
  xx
 xxx 

You were really close. You wrote 1 instead of -1 when you wanted to go down from 14-a to 0.

for a in range(1, 15):
 for a2 in range(14-a, 0, -1):
     print(" ", end='')
 for a1 in range(1, a+ 1):
     print("*", end='')
 print()

Explanation (from here):

range(start, stop[, step])

If the step argument is omitted, it defaults to 1. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop.

So in your case, your original for loop did nothing because 14-a was not smaller than 0 and step was positive .

Solution for M shape problem

 x    x
 xx  xx
 xxxxxx

-

HEIGHT = 3
for b in range(1, HEIGHT+1):
    for a in range(1, b + 1):
        print("*", end='')
    for a in range(0, 2*(HEIGHT-b)):
        print(" ", end='')
    for a in range(1, b + 1):
        print("*", end='')
    print()

Upvotes: 4

Boa
Boa

Reputation: 2677

A solution using a list comprehension:

>>> count_tuples = zip(range(2,-1,-1), range(1,4))
>>> stars = ['%s%s' % (space * ' ', star * '*') for space, star in count_tuples]
>>> print("\n".join(stars))
  *
 **
***

That's equivalent to the following solution:

>>> for space, star in zip(range(2,-1,-1), range(1,4)):
...     print '%s%s' % (space * ' ', star * '*')
  *
 **
***

Upvotes: 0

ilyas patanam
ilyas patanam

Reputation: 5324

Another option to avoid multiple for loops is to use the multiplication operator with the string. "*" * 3 = "***"

for a in range(1, 15):
    print(" " * (14-a), end = '')
    print("*" * a)

Upvotes: 0

Related Questions