Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python检查用户输入_Python_Input - Fatal编程技术网

Python检查用户输入

Python检查用户输入,python,input,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'

基本上,这是我的代码:

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!')
我想知道的是,例如,如果用户输入关键字“wet”和“mobile”作为他们的输入,我如何向他们反馈我的程序已经识别了他们的查询

因此,通过说类似“程序检测到您输入了一个关于以下内容的查询:'我如何在这句话中过滤他们的关键字,比如说,如果他们输入了'我的手机最近湿了',我想挑出'手机'和'湿'而不说:

print('The program has detected that you have entered wet')
因为这听起来很愚蠢


谢谢

如果我正确理解了你的问题,这将解决你的问题。只需将print语句放入if条件中!我想很简单:)


您可以使用元组、列表和
任何
函数:

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...")

将打印语句放入
if
测试中。事实上,你的整个
if
语句无效,需要重新编码。是的,我知道它应该是一个if-not-elif,我也需要删除导入操作系统,我之前只是在尝试一些东西,因此为什么会有不需要的代码位,但我想要一种从用户输入中提取特定字符串的方法,例如,如果有一个“有效输入”列表,并且来自用户输入的两个字符串与列表匹配,那么它会打印说stringsHow,你建议我编码吗?这就是问题所在,我知道怎么做,这很简单,但是我不想问关于“水”的问题,而是想知道是否有任何方法可以使用列表或其他东西从用户的句子中挑出字符串并交叉引用,如果有匹配项,它会从列表中使用它们并打印出来。但是,如果没有匹配项,我将使用它,以便他们可以选择是否向技术人员编写报告,并提供报告编号等,这将只向文件写入一些信息是的,这将非常有效,我知道,仅从查看它,因为我考虑过这样做,但我认为这是非常基本的,那么你的预期产出是多少<代码>程序检测到您输入了一个关于:mobile wet?检查@Angel Cruijff我编辑的答案的查询。这对你有用吗?@Angel Cruijff:我更改了你的答案,以便能够访问与输入列表中的单词匹配的
e
。一个问题,什么是元组?@Thom9son元组是一系列不变的Python对象。元组是序列,就像列表一样。元组和列表之间的区别是,元组不能更改,而列表和元组使用括号,而列表使用方括号。>>在本例中,可以使用元组或列表。
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...")