0

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?

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by vegaseat
0

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.

0

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='')

0

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 '*'.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.