Answer to Question #266258 in Python for Bayernmunich

Question #266258

Given an integer value N as input, write a program to print a shaded diamond 2*N -1 rows using an asterisk ( * ).


Note: There is a space after each asterisk ( * ) character.


Example N = 5. There for the output should be:


*

* *

* * *

* * * *

* * * * *

* *

* *

* *

*


1
Expert's answer
2021-11-16T13:52:17-0500
N = int(input())
diamond = []
for i in range(1, N + 1):
    line = ' '.join(['*' for x in range(i)])
    diamond.append(line.center(2 * N, ' ')) 
for i in range(N - 1, 0, -1):
    line = ' '.join(['*' for x in range(i)])
    if i == 1:
        line = line[::-2]
    diamond.append(line.center(2 * N, ' '))
print('\n'.join(diamond))

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