Python 检查用户输入

标签 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!')

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

因此,通过说“程序检测到您输入了有关以下内容的查询:”,我如何将他们的关键字过滤到这句话中,比如说,如果他们输入“我的手机最近被弄湿了”,我想选择“移动”和“湿”而不说:

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

因为在我看来这听起来很愚蠢。

谢谢

最佳答案

如果我正确理解你的问题,这应该可以解决你的问题。只需将 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!')

关于Python 检查用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833640/

相关文章:

python - 无法将 sendgrid 导入 GAE 应用程序

执行外部程序后可以发送多个输入的Python脚本

java - 如何添加搜索方法作为命令输入

python - Pandas date_range 从结束日期到开始日期

Python - 使用 structlog 从多个模块记录

jquery - 使用 jquery 调用 ajax 后如何等待图像加载?

javascript - 带有 jQ​​uery 的按钮监听器不起作用

javascript - 如何根据输入值显示/隐藏多个div?

input - 从 FORTRAN90 中的 TXT 文件读取复数数组和向量

python - 计算并返回 Nose 测试的数量