繁体   English   中英

如何检查整个用户输入而不仅仅是第一个单词

[英]How to check through the whole user input and not just the first word

我尝试检查用户输入,但它只检查第一个单词。 关于如何让程序根据 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'

这应该可以解决问题

#!/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM