|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def interval(start,stop=None,step=1):
'Imitates range() for step>0'
if stop is None:
start,stop=0,start
result = []
i = start
while i < stop:
result.append(i)
i +=step
return result
print(interval(10))
这段代码为何输出为:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
调用interval是start为10;那么stop=none;则参数被赋值为10,0,10;
这样的话i=10,这下面的while的条件永远不能满足啊?怎么还能输出一段数列呢?
问题出在 start,stop=0,start ==> 等价于
a = start
start = 0
stop = a
所以执行了这一句以后,
start = 0
stop = 10
while i < stop:
result.append(i)
i +=step
return result
就是:
while i < 10:
result.append(i)
i +=1
return result
|
|