python - While 循环范围(0,5)与 ran = ran[ :-1]; ran ! = [] 始终为真

标签 python python-3.x

我有一个非常简单的 Python 代码,但不明白为什么循环不会停止。我特别想在函数内部使用辅助函数。有什么想法吗?

def x():
    a = range(0,5)

    def y(ran):
        while ran != []:
            ran = ran[:-1]
            print(ran)
            return y(ran)

    return y(a)

x()

最佳答案

使用 while ran: 作为您指定的条件将始终为 true,因为 ran 是范围而不是列表。

关于python - While 循环范围(0,5)与 ran = ran[ :-1]; ran ! = [] 始终为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57963520/

相关文章:

python - numpy:使用 OR 将多个赋值转换为单个赋值

python - 如何遍历文件中的每第 n 行?

python-3.x - 使用pygame时如何处理python中的时间

python - 使用 numpy 将数组元素添加到矩阵的每一列

python - 这行代码如何在 python 中引发 'ValueError: Invalid format specifier' ?

python - django-rest-framework:为什么我的 API 为我的 ENUMS 返回键而不是值?

python - 在 django Updateview 中保存表单集时出现问题

python - 带有 print json 的 Ffprobe 不打印任何内容

python - 如何使用正则表达式查找字符串中的重复字符

python - 如何在事件发生后返回鼠标位置值