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