简体   繁体   English

在此 function def interval() 中找不到问题的解决方案

[英]can't find a solution to the problem in this function def interval()

I'm learning python right now and following this book "Beginning Python" I've got stuck at this example:我现在正在学习 python 并遵循这本书“Beginning 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

If I call this function let say interval(10) it doesn't print anything.如果我称它为 function 让我们说interval(10)它不会打印任何东西。 I code in PyCharm Python 3.9我在 PyCharm Python 3.9 中编码

You just had a typo in your while loop when incrementing your step, ie i += stop .增加步数时,您的 while 循环中出现了拼写错误,即i += stop Instead of stop use step.而不是stop使用step.

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))

Output: Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM