Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 3.x Python-检查一个用户输入是否在另一个用户输入中_Python 3.x - Fatal编程技术网

Python 3.x Python-检查一个用户输入是否在另一个用户输入中

Python 3.x Python-检查一个用户输入是否在另一个用户输入中,python-3.x,Python 3.x,Python 3.5 我正在写一个程序,基本上要求用户输入一个没有标点符号的句子。然后它会要求用户输入一个单词。我想让程序识别这个单词是否在原始句子中,我把这个句子称为string1str1,把这个单词称为string2str2。以我目前的代码,它只会告诉我这个词已经找到了,而我似乎找不到修复它的方法 str1 = input("Please enter a full sentence: ") print("Thank you, You entered:" , str1) str2 = inp

Python 3.5

我正在写一个程序,基本上要求用户输入一个没有标点符号的句子。然后它会要求用户输入一个单词。我想让程序识别这个单词是否在原始句子中,我把这个句子称为string1str1,把这个单词称为string2str2。以我目前的代码,它只会告诉我这个词已经找到了,而我似乎找不到修复它的方法

str1 = input("Please enter a full sentence: ")
print("Thank you, You entered:" , str1)

str2 = input("Now please enter a word included in your sentence in any way you like: ")

if (str2,str1):
    print("That word was found!")
else:
    print("Sorry, that word was not found")
如果任何人对此有任何建议,可能会对我和其他对此主题感兴趣的人有所帮助,那么将不胜感激!:

虽然这对我来说是一个学习过程,但我并不想直截了当地说,这是你应该拥有的代码。。。但如果这就是我能提供的一切,那么我很乐意接受

if str2 in str1:
    print("That word was found!")
else
    print("Sorry, that word was not found")
这就是你要找的吗

in检查str2是否真的在str1中。由于str1是一个单词列表,它会检查str2是否在str1中

这就是你要找的吗


in检查str2是否真的在str1中。由于str1是一个单词列表,它会检查str2是否在str1中。

提供的答案正常,但如果需要单词匹配,则会提供一个假阳性:

编辑:刚刚发现提示要求在句子中输入一个单词。。。根据我的经验,这类东西在与人类互动时最容易被破坏,所以我尝试相应地计划

str1 = "This is a story about a man"
str2 = "an"
其中:

broken_string = str1.split()

if str2.lower() in [x.lower() for x in broken_string]:
    print("The word {} was found!".format(str2))
else:
    print("{} was not found in {}.".format(str2, str1))

标点符号带来了一点更复杂的乐趣。

提供的答案正常,但如果需要单词匹配,将提供一个假阳性,给出:

编辑:刚刚发现提示要求在句子中输入一个单词。。。根据我的经验,这类东西在与人类互动时最容易被破坏,所以我尝试相应地计划

str1 = "This is a story about a man"
str2 = "an"
其中:

broken_string = str1.split()

if str2.lower() in [x.lower() for x in broken_string]:
    print("The word {} was found!".format(str2))
else:
    print("{} was not found in {}.".format(str2, str1))

使用标点符号会获得更复杂的乐趣。

如下面的答案所示。在secondString运算符中使用IF firstString,如下所示。如果在第二个字符串操作中使用IF firstString,这对我很有效,我想我以前试过,但没有成功。但它做到了,干杯;是的,这对我很有效,我想我以前试过,但没有成功。但它做到了,干杯;