1

I have already drawn a right-side-up right triangle already that looks like this:

*
* *
* * *
* * * *
* * * * *

with code:

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

    print '' 

    row = row + 1
print ''

But I need to draw a triangle that looks like this:

* * * * *
  * * * *
    * * * 
      * *
        *

and I am not entirely sure how to go about it. I know it requires at least 2 nested loops utilizing printing spaces as well as the character. It is required that only while loops are used.

I would appreciate it if someone could shed light on how to write this for me.

1
  • Also this is Python 2 Commented Nov 8, 2013 at 4:25

4 Answers 4

3

Using str.join:

def solve(width):
    for i in xrange(width, 0, -1):
        print ' '.join([' ']*(width-i) + ['*']*i)
...         
>>> solve(5)
* * * * *
  * * * *
    * * *
      * *
        *
>>> solve(7)
* * * * * * *
  * * * * * *
    * * * * *
      * * * *
        * * *
          * *
            *
Sign up to request clarification or add additional context in comments.

2 Comments

You can also use str.rjust, e.g. print str.rjust("*"*i, width), which is a little more concise.
Unfortunately I am required to use while loops only, for loops won't work though they are more efficient. So though it's probably right I can't use it :(
1

Edited to make rotate triangle, also came up with a better way of doing it

chr = "*"
size = 5
row = 1
while row <= size:
    col = size - row + 1
    while col <= size:
        print ' ', 
        col = col + 1
    col = 0
    while col <= size-row:
        print chr, 
        col = col + 1
    row = row + 1
    print ''
print ''

3 Comments

I tried running this but it didn't work. It just kept printing spaces not characters. Sorry.
Actually this does work, but it prints a mirror of the first triangle that I drew not the rotation of that first one 180 degrees like I needed it to.
I'm sorry but I can't use this because while it's probably much easier to do this with for loops, we are required to figure it out with while loops. Thank you for staying with me though :)
0
    size = 10
    def draw(n):
        if n<=size:
            draw(n+1)
            for i in range(n):
                print("#", end="")
            print(end= "\n")


    draw(0)

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
-1

Use shortest

num = int(raw_input('Enter number :'))
for a in reversed(range(num+1)):
    print ' '*(num-a)+'*'*(a)

Enter number :7
*******
 ******
  *****
   ****
    ***
     **
      *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.