Answer to Question #278098 in Python for AYUSH PATIL

Question #278098

Write a python program to take the number of lines as input and print the following patterns

 

Input

Enter the number of lines

4

 

Output

         1

      2 4 2

   1 3 5 3 1

2 4 6 8 6 4 2

   1 3 5 3 1

      2 4 2

         1

1
Expert's answer
2021-12-12T01:17:42-0500
def row(i, n):
    print("  " * (n - i), end="");
    x = -1 if i % 2 == 1 else 0
    for _ in range(i):
        x = x + 2
        print(x, end=" ")
    for _ in range(i-1):
        x = x - 2
        print(x, end=" ")
    print()

def diamond(i, n):
    row(i, n);
    if (i < n):
        diamond(i + 1, n);
        row(i, n);

def main():
    n = int(input('Enter the number of lines:'))
    diamond(1, n);

if __name__=='__main__':
    main();

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS