Jus*_* D. 1 python string list
我有一个Python列表.该列表有5个元素,可以是"None"或任何其他字符串.我想测试每个元素是否不是"无"(即如果设置了所有元素).
我编写了以下代码(userInventory5元素列表在哪里):
if all(item is not None for item in userInventory):
print = "Inventory is full, you rat!"
else:
...
Run Code Online (Sandbox Code Playgroud)
例如,['None', 'Monkey', 'None', 'Jewel', 'None']应该转到else语句,同时['None', 'None','None','None','None']应该显示消息.但是现在,if语句对所有列表都返回true ...
注意该列表来自cgi库解析的HTML表单.这些数据最初是由Python放置的,在HTML中显示如下:<input type="hidden" name="Inventory1" value="None">
userInventory=[form.getvalue("Inventory1"),form.getvalue("Inventory2"),form.getvalu e("Inventory3"),form.getvalue("Inventory4"),form.getvalue("Inventory5")]
Run Code Online (Sandbox Code Playgroud)