display_list = ['display','screen']
water_list = ['wet','water','sink','Toilet']
crack_list = ['cracked','broke','broken']
for item in display_list:
if item in problem:
f_display = True
for item in display_list and crack_list:
if item in problem:
f_screencrack = True
for item in water_list:
if item in problem:
f_water = True
elif f_screencrack == True:
print('You may need to replace your screen.')
print('Note that if the crack is too heavy, you may need a new phone.')
如果可以在display_list
和crack_list
中找到用户的答案,我希望我的程序能够显示不同的答案,但我不知道如何。
答案 0 :(得分:1)
您可以使用in
运算符检查列表中的成员资格。只需检查两个列表中的成员资格,并将它们与布尔值和
if item in display_list and item in crack_list:
...
如果你有很多名单来检查会员资格,你可以更加程序化
from operator import contains
if all(contains(x, item) for x in [display_list, crack_list, other_list]):
...
答案 1 :(得分:0)
我发现使用if语句的效果非常好
if item in display_list and item in crack_list:
for item in display_list and item in crack_list:
if item in situation:
f_scrcrack = True