Let us look at the code for how to Print a Ladder Pattern in Python. It is quite simple and interesting
Given a number N, the task is to print the ladder with N steps (frets) using ‘*’. The ladder will have a gap of 3 spaces between the two side rails.
Examples:
Example1:
Input:
Given N = 5
Output:
* * * * ***** * * * * ***** * * * * ***** * * * * ***** * * * * ***** * * * *
Example2:
Input:
Given N = 3
Output:
*Â Â * * * ***** * * * * ***** * * * * ***** * * * *
Program to Print Ladder Pattern in Python
Method #1: Using For loop (Static Input)
Approach:
- Give the number of steps(frets) of a ladder as static input and store it in a variable.
- Loop till the given number of steps using the for loop
- Print “* *”
- Check if the iterator value is less than the given number of steps(gvn_N) using the if conditional statement
- If it is true, then print “*****” i.e, frets.
- The Exit of the Program.
Below is the implementation:
# Give the number of steps of a ladder as static input and store it in a variable. gvn_N = 5 # Loop till the given number of steps using the for loop for k in range(gvn_N+1): # Print "* *" print("* *") print("* *") # Check if the iterator value is less than given number of steps(gvn_N) using the # if conditional statement if k<gvn_N: # If it is true, then print "*****" i.e, frets print("*****")
Output:
* * * * ***** * * * * ***** * * * * ***** * * * * ***** * * * * ***** * * * *
Method #2: Using For loop (User Input)
Approach:
- Give the number of steps(frets) of a ladder as user input using the int(input()) function and store it in a variable.
- Loop till the given number of steps using the for loop
- Print “* *”
- Check if the iterator value is less than the given number of steps(gvn_N) using the if conditional statement
- If it is true, then print “*****” i.e, frets.
- The Exit of the Program.
Below is the implementation:
# Give the number of steps(frets) of a ladder as user input using the int(input()) function # and store it in a variable. gvn_N = int(input("Enter some random number = ")) # Loop till the given number of steps using the for loop for k in range(gvn_N+1): # Print "* *" print("* *") print("* *") # Check if the iterator value is less than given number of steps(gvn_N) using the # if conditional statement if k<gvn_N: # If it is true, then print "*****" i.e, frets print("*****")
Output:
Enter some random number = 3 * * * * ***** * * * * ***** * * * * ***** * * * *