Member Avatar for sravan953

I wanted to make a Python prog just for fun, to get a pattern like:

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

So, basically, I started off by making a script to simply print

*****

using a loop.

Here's the code I used:

import time

for a in range(5):
    print ('*')
    a+=1

time.sleep(3)

But I only get 5 asterisks in a line one below the other, not beside one another! Why?

Recommended Answers

All 3 Replies

You can exploit the multiplication operator for strings "Hi"*2="HiHi" .

The solution is pretty trivial and is a one-liner:

>>> for a in range(7): print "*"*a
... 

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

Now modify this code so that it can print the pattern you want.

it prints them ontop of eachother, because at the end of each print() in the loop, print() automatically prints a newline, to get rid of this just go print('*', end='')

Think about this, You want to start with a line of 9 spaces and one '*' , the second line is 8 spaces and two '*' and so on, with the last line zero spaces and ten '*'. So you have to decrease the space counter and increase the '*' counter with each line the loop prints. Follow siddhant3s' advice where ' '*9 prints 9 spaces (there is a space between ' ') and '*'*10 prints ten '*'.

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.