How to Print traingle pattern by taking user input in Python

To print traingle pattern by taking input from user, first we have to store the user input in a variable.

Example: If user gives input as 5, the number of rows and columns in the traingle must be 5.

Code :


user_input = int(input("enter no of lines"))
for x in range(user_input):
    for y in range(user_input) :
        if y <= x :
            print("* \t" ,end="")
    print("")


The above python code will give the output as shown below:

enter no of lines

Let us keep the user input as 5.

The output terminal will give you the output as below:

enter no of lines 5
*       
*       *
*       *       *
*       *       *       *
*       *       *       *       *

To print the triangle in a upside-down form, Use the below code:

user_input = int(input("enter no of lines"))
for x in range(user_input):
    for y in range(user_input) :
        if y >= x :
            print("* \t" ,end="")
    print("")


The output will be as shown below :

enter no of lines 5
*       *       *       *       *       
*       *       *       *
*       *       *       
*       *
*

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT