Write a python program which asks user to enter value for number of line and then according to input following pattern should be displayed. - Pratik Ghelani

Pratik Ghelani

Breaking

Sunday, May 10, 2020

Write a python program which asks user to enter value for number of line and then according to input following pattern should be displayed.


Code:-
num = int(input("Enter the number of D pattern:"))
string = ""
def D_Pattern(string, n):
    for i in range(0, n):    
        for j in range(0, n):
            if (j == 1 or ((i == 0 or i == n-1) and
               (j > 1 and j < n-2)) or (j == n-2 and i != 0 and i != n-1)):   
                string = string + "*"   
            else:      
                string = string + " "
        string = string + "\n"   
    return(string)
print(D_Pattern(string, num))


Output:-


No comments:

Post a Comment