Extra line at start when printing a pattern using while loop

Question:

This code prints a pattern of a series of asterisks, taking the params if the pattern is to be printed normally or inverted, and also requests the number of rows.

def print_pattern(rows):
    row = 0
    while row <= rows:
        output = row * "*" 
        row += 1
        print(output)

output = print_pattern(True, 3) 

My expected output is this:

*
**
***

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


*
**
***

So why is this extra line left in the start?

Asked By: Illusioner_

||

Answers:

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

Answered By: Dominic
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.