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