从 Hetland 的 python 书中得到了这个示例函数。
def interval(start, stop=None, step=1):
if stop is None:
start, stop=0, start
result = []
i = start
while i < stop:
result.append(i)
i += step
return result
Run Code Online (Sandbox Code Playgroud)
两个开始在 if 语句中做什么?为什么会出现两次?我将后者的开始更改为 int,这将我的列表长度更改为该 int。
我似乎也无法围绕交互部分。在这个例子中,start = 10。所以,当迭代...而 10 < 0 它将继续增长列表,每次增加 step=1 的计数。但是,10 不小于 0。首先怎么跑?