I need to print a shape that looks like the following
* *
* *
* *
*
* *
* *
* *
I’ve tried doing this but only managed to print an X shape
n = 7
for i in range(0, n):
for j in range(0, n):
if (i==j or j==n-1-i):
print("*",end=" ")
else:
print(" ",end=" ")
print()
output:
* *
* *
* *
*
* *
* *
* *
Don’t know how to approach this and would like some help 🙂 🙏
>Solution :
You can create two lists with the number of spaces before the first * and the number between the two *s and then use zip() and f-strings:
before = [0, 0, 1, 2, 1, 0, 0]
between = [3, 3, 1, 0, 1, 3, 3]
for i, j in zip(before, between):
print(f'{i*" "}*{j*" "}{"*" if j else ""}')
Output:
* *
* *
* *
*
* *
* *
* *