SufferProgrammer SufferProgrammer - 7 months ago 14
Python Question

remove asterisk symbol inside diamond shape

i want to ask about this think
i have a script and it's print a diamond shape like this.

Value : 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*


the script code is like this

masuk = int(input("Value : "))

for jarak in range(1, masuk):
for jarak6 in range(-1, masuk - jarak):
print (" ", end=" ")
for jarak7 in range(1, jarak):
print ("*", end=" ")
for jarak8 in range(jarak, 0, -1):
print ("*", end=" ")

print()

for jarak in range(1, masuk *2 , + 1):
for jarak2 in range(jarak):
print (" ", end=" ")
for jarak3 in range(jarak, masuk + 1):
print ("*", end=" ")
for jarak4 in range(jarak + 1, masuk + 1):
print ("*", end=" ")

print()


what i want to ask is :


  1. is it a effective way to make a diamond shape ?

  2. how removing asterisk symbol inside the diamond shape ?


Answer

Your inner loops can be replaced by simple string multiplication. Instead of writing

for i in range(a, b):
    print(" ", end=" ")

you could just write

print("  " * (b - a))

With this, you can combine all three inner loops of both your outer loops into one print call:

print(" " * num_spaces + "*"*num_asterisks)

I'm not sure what you mean by removing the diamond inside the shape though.

Comments