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

Function not printing anything to the console in Python

I have written a function that takes one argument – rows, and prints an upside-down pyramid of stars.

def printUpsideDownPyramid(rows):
    for i in range(rows, -1, -1): #For spaces
        for space in range(rows-i):
            print(" ", end="")
    for star in range(2*i-1): #For stars
        print("*", end="")
    print() #New row


printUpsideDownPyramid(9)

When I call it, nothing is printed on the console. I am a beginner in Python, please help me understand what is the problem

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

>Solution :

This is either bad logic (planning the program), or bad indentation (typing it). What you presumably want is:

def printUpsideDownPyramid(rows):
    for i in range(rows, -1, -1): #For spaces
        for space in range(rows-i):
            print(" ", end="")
        for star in range(2*i-1): #For stars
            print("*", end="")
        print() #New row

printUpsideDownPyramid(9)

See this running at https://ideone.com/UBP93T, with output:

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

Consider also the shorter formulation:

def printUpsideDownPyramid(rows):
    for i in range(rows, -1, -1): #For spaces
        print((" " * (rows - i)) + ("*" * (2*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