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

Extra line at start when printing a pattern using while loops

this is a simple piece of code, I use it to print a pattern of series of asterisks depending on whether you want the pattern to be inverted or not and the number of rows

def printX(Inverse, rows):
#prints pattern using arguments, does it need to be inverted and number of rows
    if Inverse:
        row = 0
        while row <= rows:
            output = row * "*" 
            row += 1
            print(output)

output = printX(patternType, rows) 

So, if in place of patternType, I write True, and in place of rows, I write 3, I expect my output to look something like this:

*
**
***

I instead get something like this, with an extra line on top

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


*
**
***

So why is this extra line left in the start?

>Solution :

You started row at 0, so your first loop iteration prints "*" 0 times, aka a blank line. I’d suggest starting row at 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