Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I remove this extra kite from the diamond in this code?

I have written a python program for printing a diamond. It is working properly except that it is printing an extra kite after printing a diamond. May someone please help me to remove this bug? I can’t find it and please give a fix from this code please.
CODE:

limitRows = int(input("Enter the maximum number of rows: "))
maxRows = limitRows + (limitRows - 1)
while currentRow <= limitRows:
    spaces = limitRows - currentRow
    while spaces > 0:
        print(" ", end='')
        spaces -= 1
    stars = currentRow
    while stars > 0:
        print("*", end=' ')
        stars -= 1
    print()
    currentRow += 1
while currentRow <= maxRows:
    leftRows = maxRows - currentRow + 1
    while leftRows > 0:
        spaces = limitRows - leftRows
        while spaces > 0:
            print(" ", end='')
            spaces -= 1
        stars = leftRows
        while stars > 0:
            print("*", end=' ')
            stars -= 1
        print()
        leftRows -= 1
    currentRow += 1

OUTPUT(Case 1):

D:\Python\diamond\venv\Scripts\python.exe D:/Python/diamond/main.py
Enter the maximum number of rows: 4
   * 
  * * 
 * * * 
* * * * 
 * * * 
  * * 
   * 
  * * 
   * 
   * 

Process finished with exit code 0

OUTPUT(Case 2):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

D:\Python\diamond\venv\Scripts\python.exe D:/Python/diamond/main.py
Enter the maximum number of rows: 5
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
  * * * 
   * * 
    * 
   * * 
    * 
    * 

Process finished with exit code 0

>Solution :

You have an extra while loop in the second half of your code.

Try this

limitRows = int(input("Enter the maximum number of rows: "))
maxRows = limitRows + (limitRows - 1)
currentRow = 0
while currentRow <= limitRows:
    spaces = limitRows - currentRow
    while spaces > 0:
        print(" ", end='')
        spaces -= 1
    stars = currentRow
    while stars > 0:
        print("*", end=' ')
        stars -= 1
    print()
    currentRow += 1
while currentRow <= maxRows:
    leftRows = maxRows - currentRow + 1
    spaces = limitRows - leftRows     # Removed unnecessary while loop here
    while spaces > 0:
        print(" ", end='')
        spaces -= 1
    stars = leftRows
    while stars > 0:
        print("*", end=' ')
        stars -= 1
    print()
    leftRows -= 1
    currentRow += 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading