Printing Ladder Pattern in Python

Welcome to another simple programming tutorial in Python. This tutorial is going to be very useful for those who are at the beginner level in Python. We will learn how can we print the ladder pattern in the console window using a simple Python code snippet.

Let’s move towards our coding section to achieve this.

Ladder Pattern Python Code

This tutorial will give you an understanding of how can we take input from the user and how can we execute the for loop and also how can we use the if statement.

This is the complete code given below for printing the ladder pattern:

n=int(input("Enter the number of frets:"))
for i in range(n+1):
    print("*   *")
    print("*   *")


    if i<n:
        print("*****")

First-line will take the input by printing “Enter the number of frets”. The input type is an integer and it is stored in variable n.

Then the for loop, which will run n+1 times. and print “* *”. in order to draw frets we will insert the statements if. That line will print “*****” as per the input. Now Why n+1 times? because we want “* *” to be printed at last too.

Let suppose our input is 5. Then it will produce the following output.

 

Output:

Enter the number of frets: 5
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

From the output result, we can see that we are successfully able to print the Ladder Pattern.

 

Leave a Reply