Home > OS >  Extra line at start when printing a pattern using while loops
Extra line at start when printing a pattern using while loops

Time:07-24

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


*
**
***

So why is this extra line left in the start?

CodePudding user response:

You started row at 0, so your first loop iteration prints "*" 0 times, aka a blank line. I'd suggest starting row at 1.

  • Related