Python 编写返回函数
这是我的代码:Python 编写返回函数,python,return,Python,Return,这是我的代码: def issorted(numlist): sortbool = True for i in range(1, len(numlist)): if numlist[i] < numlist[i-1]: sortbool = False if True: return(not sortbool) return(sortbool) 少用变量。有时它们很有用,有
def issorted(numlist):
sortbool = True
for i in range(1, len(numlist)):
if numlist[i] < numlist[i-1]:
sortbool = False
if True:
return(not sortbool)
return(sortbool)
少用变量。有时它们很有用,有时它们让你困惑
def issorted(numlist):
for i in range(1, len(numlist)):
if numlist[i] < numlist[i-1]:
#you want to return False and exit the function.
#return is the best option here.
return False
#if the for loop succeeds, you want to return True by default
return True
def被分类(numlist):
对于范围(1,len(numlist))中的i:
如果numlist[i]
节约使用变量。有时它们很有用,有时它们让你困惑
def issorted(numlist):
for i in range(1, len(numlist)):
if numlist[i] < numlist[i-1]:
#you want to return False and exit the function.
#return is the best option here.
return False
#if the for loop succeeds, you want to return True by default
return True
def被分类(numlist):
对于范围(1,len(numlist))中的i:
如果numlist[i]
只需返回一个常量;这里不需要变量,您完全搞糊涂了。您的代码:
if numlist[i] < numlist[i-1]:
sortbool = False
if True:
return(not sortbool)
注意,我在那里耍了个小把戏;我使用一个额外的变量来跟踪上一个值,而不是生成索引。只需返回一个常量;这里不需要变量,您完全搞糊涂了。您的代码:
if numlist[i] < numlist[i-1]:
sortbool = False
if True:
return(not sortbool)
注意,我在那里耍了个小把戏;我使用一个额外的变量来跟踪上一个值,而不是生成索引。如果为True:
始终为True。由于您设置了sortbool
太False
并返回not sortbool
(所以True
),所以如果numlist[i]return True
,而不会丢失功能。如果True:
始终为True。由于您将sortbool
也设置为False
并返回not sortbool
(因此True
),您的整个如果numlist[i]
块可以替换为return True
,而不会丢失功能。