Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 Can';“不要让程序打印”;“不在句子中”;当单词不在句子中时_Python_Python 3.x_Words_Enumerate_Sentence - Fatal编程技术网

Python Can';“不要让程序打印”;“不在句子中”;当单词不在句子中时

Python Can';“不要让程序打印”;“不在句子中”;当单词不在句子中时,python,python-3.x,words,enumerate,sentence,Python,Python 3.x,Words,Enumerate,Sentence,我有一个程序,要求输入一个句子,然后要求输入一个单词,并告诉你该单词的位置: sentence = input("enter sentence: ").lower() askedword = input("enter word to locate position: ").lower() words = sentence.split(" ") for i, word in enumerate(words): if askedword == word : print

我有一个程序,要求输入一个句子,然后要求输入一个单词,并告诉你该单词的位置:

sentence = input("enter sentence: ").lower()
askedword = input("enter word to locate position: ").lower()
words = sentence.split(" ")

for i, word in enumerate(words):
     if askedword == word :
          print(i+1)
    #elif keyword != words :
         #print ("this not")

但是,当我编辑程序时,无法使其正常工作,因为如果输入单词不在句子中,则打印“这不在句子中”

列表是序列,因此您可以使用它们来测试
单词列表中的成员资格。如果在里面,用
单词查找句子中的位置。索引

sentence = input("enter sentence: ").lower()
askedword = input("enter word to locate position: ").lower()
words = sentence.split(" ")

if askedword in words:
    print('Position of word: ', words.index(askedword))
else:
    print("Word is not in the given sentence.")
使用示例输入:

enter sentence: hello world

enter word to locate position: world
Position of word: 1
还有,一个错误的案例:

enter sentence: hello world

enter word to locate position: worldz
Word is not in the given sentence.
如果您希望检查多个匹配项,则使用
enumerate
进行列表理解是最好的选择:

r = [i for i, j in enumerate(words, start=1) if j == askedword]
然后检查列表是否为空,并相应打印:

if r:
    print("Positions of word:", *r)
else:
    print("Word is not in the given sentence.")

列表是序列,因此您可以使用它们来测试
单词
列表中的成员资格。如果在里面,用
单词查找句子中的位置。索引

sentence = input("enter sentence: ").lower()
askedword = input("enter word to locate position: ").lower()
words = sentence.split(" ")

if askedword in words:
    print('Position of word: ', words.index(askedword))
else:
    print("Word is not in the given sentence.")
使用示例输入:

enter sentence: hello world

enter word to locate position: world
Position of word: 1
还有,一个错误的案例:

enter sentence: hello world

enter word to locate position: worldz
Word is not in the given sentence.
如果您希望检查多个匹配项,则使用
enumerate
进行列表理解是最好的选择:

r = [i for i, j in enumerate(words, start=1) if j == askedword]
然后检查列表是否为空,并相应打印:

if r:
    print("Positions of word:", *r)
else:
    print("Word is not in the given sentence.")

在我看来,Jim的答案结合了对
askedword的测试和对
words.index(askedword)
的调用,是最好、最具python风格的方法

同一方法的另一个变体是使用
try
-
,除了

try:
    print(words.index(askedword) + 1) 
except ValueError:
    print("word not in sentence")
但是,我想我应该指出,OP代码的结构看起来像是您可能已经尝试采用以下模式,该模式也有效:

for i, word in enumerate(words):
    if askedword == word :
        print(i+1)
        break
else:    # triggered if the loop runs out without breaking
    print ("word not in sentence")

在大多数其他编程语言中都无法使用的异常情况下,此
else
绑定到
for
循环,而不是
if
语句(没错,编辑时不要插手我的缩进)

Jim的答案结合了对
askedword的测试和对
words.index(askedword)
的调用,在我看来,这是最好、最具python风格的方法

同一方法的另一个变体是使用
try
-
,除了

try:
    print(words.index(askedword) + 1) 
except ValueError:
    print("word not in sentence")
但是,我想我应该指出,OP代码的结构看起来像是您可能已经尝试采用以下模式,该模式也有效:

for i, word in enumerate(words):
    if askedword == word :
        print(i+1)
        break
else:    # triggered if the loop runs out without breaking
    print ("word not in sentence")

在大多数其他编程语言中都无法使用的异常情况下,此
else
绑定到
for
循环,而不是
if
语句(没错,编辑时不要插手我的缩进)

我试了试,但除了一个,它似乎只输出了第一个位置我试了试,但除了一个,它似乎只输出了第一个位置这只定位了单词第一次出现在句子中,例如,如果我输入句子“狗坐在狗身上”,它应该得到1和5,还有什么我能做到的吗?@Dummy8123对,没有一个答案明显被你抓住了。你想检查多次出现的情况,我更新了我的答案以解决这个问题。这只在单词第一次出现在句子中时才定位,例如,如果我输入句子“狗坐在狗身上”,它应该会出现1和5,还有什么我能做到的吗?@Dummy8123对,显然没有一个答案被你发现,你想检查多次出现的情况,我更新了我的答案来解决这个问题。