Using Python to Create a Regular Triangle
I am confused and frustrated as to why my program is not doing the way it should. I am trying to create a right triangle using asterisks in python ver 3. I am using the exact code from the tutorial and it comes out as a straight line. Please enlighten me.
Example
size = 7
for row in range (size):
for col in range (row + 1):
print('*', end='')
print()
leads to
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
all in one column
source to share
In Python, indents are counted. The final one print
should be aligned with the second for
.
size = 7
for row in range (size):
for col in range (row + 1):
print('*', end='')
print()
Result:
*
**
***
****
*****
******
*******
An empty print
function effectively means "go to new line". if both statements print
are executed with the same indentation level, then every time you type an asterisk, you go to the next line. If the second print
goes outside the inner loop for
, you only get 7 new lines.
source to share