PUBLISHED ON: AUGUST 23, 2021
Python Program to print double sided stair-case pattern
In this tutorial, we are going to print a double-sided staircase pattern. The staircase is right-left aligned, composed of * symbols and spaces, and has a height and width up to n. Here, n is an integer that is provided by the user.
Let us understand the input-output given below for a better understanding.
Here, the user will be prompted to give the input which will be the size, and the output is desired double-sided staircase pattern.
Algorithm
We have seen the Input and Output of the double-sided staircase pattern now let's dive deep into the algorithm part followed by the code:
- Create a function staircase_pattern()
- Pass n as a parameter in the function
- Create a for loop for the rows
- Mark a conditional operator
- Create for loop for printing spaces
- According to value carry on with the looping operation
- The result will be the desired pattern
- Print the pattern
Program
As of now, we have a rough understanding of printing the double-sided staircase pattern. Let us now look at the code influenced by the algorithm:
def pattern(n):
for a in range(1,n+1):
c =a + 1 if(a % 2 != 0) else a
for b in range(c,n):
if b>=c:
print(end=" ")
for d in range(0,c):
if d == c - 1:
print(" * ")
else:
print(" * ", end = " ")
# Driver code
n = int(input("Please enter the size:"))
if n<10:
print("Enter range above 10")
else:
pattern(n)
Please enter the size:10
* *
* *
* * * *
* * * *
* * * * * *
* * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
Conclusion
In this tutorial, we have printed a double-sided staircase pattern in the python programming language. The staircase is composed of * and spaces. You can change the symbols according to your choice.