【发布时间】:2020-11-06 19:04:17
【问题描述】:
我正在尝试使用以下脚本检查我的列表是否为空:
#!/usr/bin/env python3
#First we start with the empty list of "users"
user_names = []
#Run a check with if to see if the list has "users"
if user_names:
for user in user_names:
print("Message to user logging in.")
#The assumption is this will evaluate to False and else will execute
else:
print("Message when the list is empty.")
但是当脚本被执行时,什么都没有运行并且输出是空白的。我希望知道:
- 如果这是由于我的代码中的错误?
- 或者如果它是一个错误?
- 有没有更好的方法来编写这段代码?
我的 Python 版本是 3.8.3
【问题讨论】:
标签: list loops boolean python-3.8