我尝试检查用户输入,但它只检查第一个单词。有关如何让程序通过整个字符串检查2D列表而不是仅检查第一个单词并输出缺失的任何建议。 代码如下:
my_list = [['noisey','pc','broken']['smashed','window','glass']]
search = input('Enter what is going wrong: ')
notfound = 'True'
for i in mylist[0:1]:
while notfound == 'True':
if search in my_list[0]:
print('Found in Pc')
notfound = 'False'
elif search in my_list[1]:
print('Found in Home.')
notfound = 'False'
else:
print('missing')
notfound = 'False'
答案 0 :(得分:0)
这应该可以解决问题
#!/usr/bin/python3
my_list = [['noisey','pc','broken'],['smashed','window','glass']]
search = input('Enter what is going wrong: ')
for word in search.split():
if word in my_list[0]:
print(word,'Found in PC');
elif word in my_list[1]:
print(word,'Found in Home');
else:
print(word,"Not Found");
输出:
Enter what is going wrong: noisey pc smashed accidentally
noisey Found in PC
pc Found in PC
smashed Found in Home
accidentally Not Found