Small Python Program For Beginners To Practice |
In this article, we are going to write python code to print out hollow hour glass as shown in the featured Image.
If you are looking for a Python Tutorial series then it's here
The python code we are going to write is a combination of normal hollow pyramid and Inverted hollow pyramid. we are going to combine both of them to get the desired output.
def sand(n):
for i in range(1, n+1):
for j in range(1, i):
print(" ", end="")
for j in range(1, (n*2 - (2*i - 1))+1):
if i == 1 or j == 1 or j ==(n*2 -(2*i-1)):
print("*", end="")
else:
print(" ", end="")
print()
for i in range(2,n+1):
for j in range(n-i):
print(" ", end="")
for j in range(1, (2*i-1)+1):
if i == n or j == 1 or j == (2*i-1):
print('*', end='')
else:
print(" ", end="")
print()
sand(10)
The output of the above python program is:
*******************
* *
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *
* *
*******************
Comments
Post a Comment