Python检查用户输入

时间:2015-11-20 18:30:17

标签: python input

所以,基本上,这是我的代码:

import random
import os

answer = input('What is the problem with your mobile phone? Please do not enter more than one sentence.')
print('The program has detected that you have entered a query regarding: ' 
if 'wet' or 'water' or 'liquid' or 'mobile' in answer:
    print('Put your mobile phone inside of a fridge, it sounds stupid but it should work!')

我想知道的是,例如,如果用户输入关键字“湿”和“移动”作为输入,如何知道我的程序已识别出他们的查询,如何反馈给他们。

所以通过说'该程序检测到您输入了一个查询:'如何将他们的关键字过滤到这句话中,比如说,如果他们输入'我的手机最近已经湿了',我想要选择'移动'和'湿'而不说:

print('The program has detected that you have entered wet')

因为这听起来很愚蠢IMO。

由于

2 个答案:

答案 0 :(得分:2)

如果我理解你的问题,这应该可以解决你的问题。只需将print语句放在if条件中即可!很简单,我想:)

import random
import os

answer = input('What is the problem with your mobile phone? Please do not enter more than one sentence.')
if 'wet' or 'water' or 'liquid' or 'mobile' in answer:
    print('The program has detected that you have entered a query regarding: water') # or anything else wet or whatever
    print('Put your mobile phone inside of a fridge, it sounds stupid but it should work!')

答案 1 :(得分:1)

您可以使用元组,列表和any函数来执行此操作:

SEND_REPORT_TUPLE = ('wet', 'water', 'liquid', 'mobile')
#make a list from the input
input_list = answer.split(" ")
#And then the use any function with comprehension list
if any(e in SEND_REPORT_TUPLE for e in input_list):
    print("The program has detected a query...")