-1

I just started learning Python and I need help on creating this pattern:

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

I currently have this code:

      base = 6
      for row in range (base):
          for col in range (row+1):
              print('*', end='')
          print()

which becomes:

*
**
***
****
*****
******
2
  • Check this question for clues. Commented Mar 11, 2015 at 1:55
  • What is your question? Commented Jun 17, 2018 at 20:49

3 Answers 3

0

What you need here is to track how many spaces you need—and you pretty much already have code for that—and how many '*'s you need. Then for each line, you need to print the appropriate number of ' 's, and the appropriate number of '*'s. Since my guess is that you want the total width to stay constant, you'll want x number of ' 's, and n-x number of '*'s, where n is the number of lines / total width you want.

So for example

n=6; print('\n'.join(' '*x+'*'*(n-x) for x in range(0,n)))

will work.

If this is a homework problem, I'd advise against turning in the above solution, however. It would be better to write it out in a standard way.

Sign up to request clarification or add additional context in comments.

Comments

0

Since this might be some homework or something as specified above by @cge as well, I just hope that a difference of two years is enough and I am writing the answer to tell about something which lot many aren't well aware of in Python.

See the pattern you are trying to make is one of the most common things asked to form in any programming language and quite easy to make in Java or C++. But this same thing came to my mind while thinking of how will I make such pattern in Python because while using Python, the print() function changes the line whenever used. So there is no feature like we have in Java where print and println are two different things.

Now here's an interesting thing to know about. In Python 3, with the introduction of print as a method, some other things also came with it which also had this thing called end="\n" attached to it as default. This is the reason why when being called, the print function ends up on the next line. So all we need to do is not let the default work.

Here's how the code works:

for i in range(6):
    for x in range(6):
        if i!=0:
            print(" ",end="")
            i-=1

        else:
            print("*",end="")

    print()

So we see that using the above additional features of the print function, we can make use of and create such patterns as you have asked.

Comments

-1

I think you're making it too complicated

base = 6

for i in range(base, 0, -1):
    print "*"*i

and you get:

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

Think about how your ranges are being set up. If you want to print "******" before you print "*****" you need to let python know to print 6 before 5. Do that by counting down your range.

I've done that here:

for i in range(base, 0, -1):

It starts the range at base (which you set to 6) and counts down by -1, all the way to zero.

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.