Python 我如何修复我的代码,使其只包含一个单词';当输入字符时,是否找到s位置而不是包含字符的单词? 句子=输入(“请输入不含标点符号的句子”) chars=set('0123456789!'$%^&*()_-+={}[];:@#~/?,\ | `,`,`) 而任何((字符中的c)表示句子中的c): 句子=输入(“请输入除标点符号以外的其他句子”) 其他: 打印(‘你的句子很好’) 句子=句子。上() locateword=输入(“从输入的句子中输入要定位的单词”) locateword=locateword.upper() 当定位词不在句子中时: locateword=输入(“从输入的句子中输入另一个单词进行定位”) locateword=locateword.upper() 其他: 打印(“你的话很好”) 句子=句子。拆分() 对于枚举(句子)中的(位置、单词): 如果(以文字表示的单词): 打印(“单词的位置是”,位置+1)

Python 我如何修复我的代码,使其只包含一个单词';当输入字符时,是否找到s位置而不是包含字符的单词? 句子=输入(“请输入不含标点符号的句子”) chars=set('0123456789!'$%^&*()_-+={}[];:@#~/?,\ | `,`,`) 而任何((字符中的c)表示句子中的c): 句子=输入(“请输入除标点符号以外的其他句子”) 其他: 打印(‘你的句子很好’) 句子=句子。上() locateword=输入(“从输入的句子中输入要定位的单词”) locateword=locateword.upper() 当定位词不在句子中时: locateword=输入(“从输入的句子中输入另一个单词进行定位”) locateword=locateword.upper() 其他: 打印(“你的话很好”) 句子=句子。拆分() 对于枚举(句子)中的(位置、单词): 如果(以文字表示的单词): 打印(“单词的位置是”,位置+1),python,Python,此代码在输入的句子中查找输入单词的位置。但是,当要查找的单词是单个字符时,代码会查找包含该字符的单词的位置,我如何修复此代码以避免发生这种情况?例如,当句子是“我喜欢足球”而单词是“球”时,返回的位置是3,因为在“football”中找到了“ball”,但我不希望发生这种情况 试试这个: sentence = input("Please enter a sentence excluding punctuation") chars = set('0123456789!"£$%^&*(

此代码在输入的句子中查找输入单词的位置。但是,当要查找的单词是单个字符时,代码会查找包含该字符的单词的位置,我如何修复此代码以避免发生这种情况?例如,当句子是“我喜欢足球”而单词是“球”时,返回的位置是3,因为在“football”中找到了“ball”,但我不希望发生这种情况

试试这个:

sentence = input("Please enter a sentence excluding punctuation")



chars = set('0123456789!"£$%^&*()_-+={}[];:@#~/?.,<>\|`¬')
while any((c in chars) for c in sentence):
    sentence = input("Please enter another sentence excluding punctuation")
else:
    print('Your sentence is fine')
sentence = sentence.upper()

locateword = input ("Enter a word from the inputted sentence to locate")
locateword=locateword.upper()


while locateword not in sentence:

    locateword = input ("Enter another word from the inputted sentence to locate")
    locateword=locateword.upper()
else:
    print ("Your word is fine")

sentence = sentence.split()

for (position,words) in enumerate(sentence):
    if (locateword in words):
        print ("The position of your word is",position+1)
在你分开你的句子之前使用这个

或者

在你再次分开你的句子之前先这样做


\b
用于正则表达式中的单词边界。因此,它不会在一个单词中捕捉到一个单词。

在你的代码中,因为
句子已经被拆分,所以
if(locateword In words)
将在
football
中搜索
球,而不是在
[“I”,“like”,“football”]

你可以试试:

import re
index = re.search(r'\b'+locateword+r'\b', sentence)
if index:
    print(index.start())
    print(index.end())
else:
    print("Word not in sentence")
也许:

尽管这不是你的问题,但检查字符串是否包含字母数字字符的更好方法是:

for (position,words) in enumerate(sentence):
    if (upper(locateword) == upper(words)): #case insensitive
        print ("The position of your word is",position+1)
完整代码:

import re
sentence = input("Please enter a sentence excluding punctuation")
if re.search(r"\W", sentence):
    sentence = input("Please enter another sentence excluding punctuation")
else: print("Your sentence is ok!")

这样就不会返回任何位置,结果仍然是编辑答案后的结果。你只需要单词在句子中的位置,对吗?
print(确切地说是句子索引(locateword)+1)
。我需要单词在句子中的位置。编辑后的答案仍然具有相同的结果,但随后打印出另一个数字。您能否举例说明您的输出应该是什么样子?但是代码应该允许任何单词和句子,而不仅仅是一个example@User123这个答案只是一个例子。只需将
句子=…
替换为
句子=输入('您的句子:')
,因此变量
word
。调整它以适应你们的需要你们想让它找到那个单词,不管它是大写还是小写?这就是
upper()
的作用吗?如果可能的话,整个代码都可以打印出来,因为解决方案会混淆它们应该放在代码中的什么位置。单词和句子都是大写的,这样代码就不区分大小写了。因此,用户可以在任何情况下输入。请检查我的答案是否有效,这是否符合您的要求?这并不能满足我的要求,就像句子中包含标点符号一样,程序刚刚结束。如果单词不在句子中,代码也会结束。这些应该是循环,这样当句子包含标点符号时,用户应该不断被要求输入另一个句子。@User123编辑了它
for (position,words) in enumerate(sentence):
    if (upper(locateword) == upper(words)): #case insensitive
        print ("The position of your word is",position+1)
import re
sentence = input("Please enter a sentence excluding punctuation")
if re.search(r"\W", sentence):
    sentence = input("Please enter another sentence excluding punctuation")
else: print("Your sentence is ok!")
import re
sentence = input("Please enter a sentence excluding punctuation: ")
while( re.search(r"\W ", sentence)):
    sentence = input("Please enter another sentence excluding punctuation: ")
print("Your sentence is ok!")

sentence = sentence.split(" ")

locateword = input("Enter a word: ")
while(locateword.lower() not in [x.lower() for x in sentence]):
    locateword = input("Enter another word: ")

for (position,words) in enumerate(sentence):
    if (locateword.upper() == words.upper()): #case
        print ("The position of your word is",position+1)