Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print an inverted pyramid that has asterisks surrounding it?

The goal is to write a program that can print this output

Enter a number: 4

*00000*

**000**

***0***

*******

These are what I’m currently working with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

row = int(input('Enter number of rows required: '))

for i in range(row,0,-1):
    for j in range(row-i):
        print('*', end='') 
    
    for j in range(2*i-1):
        print('0',end='')
    print() # printing new line

And this is the current output:

0000000
*00000
**000
***0

I can’t figure out what to change to have the correct output

>Solution :

you can use f-string and siple nth number from the formula a + (n-1)*d

row = int(input('Enter number of rows required: '))

for i in range(row):
    print(f'{"*"*(i+1)}{"0"*(2*(row-i-1) - 1)}{"*"*(i+1)}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading