Discuss / Python / 关于python缩进问题提问

关于python缩进问题提问

Topic source

同样一个关于x的n次方的函数,因为return缩进不一样,结果不一样,有大佬可以普及下python缩进的知识吗

>>> def p(x,n):

...     s = 1

...     while n>0:

...         n = n-1

...         s = s*x

...     return s

...

>>> p(3,2)

9

>>> def p(x,n):

...     s=1

...     while n>0:

...         n=n-1

...         s=s*x

...         return s

...

>>> p(3,2)

3

>>>

语句要在循环语句后缩进才属于这个循环中  

第一个return不属于while循环内 while循环结束后S值为9 返回后输出为9

第二个return属于while循环体内 第一次循环中 s值为3 遇到return后返回3  而循环中遇到return后循环便终止了  所以第二个便输出3


  • 1

Reply