到目前为止,这是我的代码
sentence= input("Enter a sentence")
keyword= input("Input a keyword from the sentence")
keyword.upper() == keyword.lower()
words = sentence.split(' ')
for (i, subword) in enumerate(words)
if (subword == keyword)
print(i+1)
if (keyword not in sentence)
print("Sorry, that word wasnt found in the sentence")
以下不起作用
keyword.upper() == keyword.lower()
我可以在代码中添加什么?
通常,您将使用in
关键字检查子字符串是否为另一个字符串的一部分:
sentence = input("Sentence: ")
keyword = input("Keyword: ")
if keyword in sentence:
print("Found it!")
else:
print("It's not there...")
要变得不区分大小写,您必须将句子的小写版本与关键字的小写版本进行比较(或两者都使用大写形式,没有区别):
if keyword.lower() in sentence.lower():
# ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句