我现在正在学习python,按照“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 += stop
return result如果我调用这个函数,比如说interval(10),它不会打印任何东西。我在PyCharm Python3.9中编写代码
发布于 2021-01-20 22:09:41
你只是在增加步长的时候,在你的while循环中有一个拼写错误,即i += stop。使用step.代替stop
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
print(interval(10))输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
https://stackoverflow.com/questions/65810996
复制相似问题