使用Python创建一个直角三角形

Joe*_*ark 3 python python-3.x

关于为什么我的程序没有按照应有的方式执行,我感到困惑和沮丧.我试图在python ver 3上使用星号创建一个直角三角形.我正在使用教科书中的确切代码,它是一条直线.请赐教.

size = 7

for row in range (size):
    for col in range (row + 1):
        print('*', end='')
        print()
Run Code Online (Sandbox Code Playgroud)

结果是

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* 
Run Code Online (Sandbox Code Playgroud)

全部在一栏中

Kev*_*vin 7

在Python中,缩进很重要.决赛print应与第二名保持一致for.

size = 7

for row in range (size):
    for col in range (row + 1):
        print('*', end='')
    print()
Run Code Online (Sandbox Code Playgroud)

结果:

*
**
***
****
*****
******
*******
Run Code Online (Sandbox Code Playgroud)

print函数实际上意味着"移动到新行".如果两个print语句都出现在相同的缩进级别,那么每次打印星号时,都将移动到下一行.如果第二次print外面的内for循环,你将只能得到7个换行符.