Python中的while循环:有东西存在吗?

问题描述 投票:-2回答:1

学习Python,并希望有所了解。

a = ['this', 'is', 'a', 'reproducible', 'example']


while a:
    print(a.pop())
else:
    print('There are no more elements.')

while a条件是否表示“ a存在时”或a非空列表?它的显式版本是什么?是a != 0吗?换句话说:用尽列表中的元素?

我认为这提供了较少击键的优势。

python while-loop boolean-logic
1个回答
0
投票

由于a是一个列表,所以您要查找的是在不为空时运行循环。因此,您的循环应为:

while a:
    print(a.pop())
print("There are no more elements.")

在您的原始程序中,您在片刻之后有一个else语句,该语句不执行任何操作。结束while循环后,a已经为空,因此您只能说不再有leemnts。

© www.soinside.com 2019 - 2024. All rights reserved.