快速测试所有字典项是否为“True”的方法

时间:2016-06-25 10:50:43

标签: python python-3.x dictionary

您正在制作的程序中我好,我要求用户输入他们的运送详细信息,一旦他们这样做,他们就可以继续确认他们的订单。但是,我不希望用户过早地跳过运送详细信息表单而不填写所有字段。这是存储其详细信息的字典:

user_details = {"Full Name": None, "Phone Number": None, "Street Address": None, "City": None, "Postcode": None}

一旦他们拥有每个键的值,我希望他们能够继续,但不能在此之前。我正在寻找一种快速方法来测试所有值是否为真/存在。谢谢!

2 个答案:

答案 0 :(得分:4)

>>> user_details.values()
[None, None, None, None, None]
>>> if None in user_details.values():
...   print "none in there"
... 
none in there

答案 1 :(得分:2)

all(user_details.values())

如果所有值都不是none,则返回true,否则返回false

相关问题