Conor - 1 year ago 63
Python Question

# How to make an unfilled diamond using while loops in python

I can only create a filled diamond, i can't figure out how to get it unfilled.`#

``````# The size of the diamond
N = 7

# The top part (includes the middle row)
i = 0
while i < N // 2 + 1:
print((N // 2 - i) * " " + (2 * i + 1) * "*")
i += 1

# The bottom part
i = 0
while i < N // 2:
print(" " * (i + 1) + "*" * (N - 2 - 2 * i))
i += 1
``````

You just need to print `(2*i-1)` spaces in between `'*'` characters instead instead of only `'*'`. And have to deal with the very top and very bottom separately:

``````# The size of the diamond
N = 7

# The top part (includes the middle row)
print((N // 2) * " " + '*')
i = 1
while i < N // 2 + 1:
print((N // 2 - i) * " " + '*' + (2 * i - 1) * " " + '*')
i += 1

# The bottom part
i = 0
while i < N // 2 - 1:
print(" " * (i + 1) + '*' + " " * (N - 4 - 2 * i) + '*')
i += 1
print((N // 2) * " " + '*')

*
* *
*   *
*     *
*   *
* *
*
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download