2016-05-03 27 views
-1

これは単語の文字列を検索する必要があります。どのように大文字小文字を区別しないのですか?

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() 

動作しませんか? 、キーワードの小文字バージョンで刑の小文字バージョンを比較する必要が

sentence = input("Sentence: ") 
keyword = input("Keyword: ") 

if keyword in sentence: 
    print("Found it!") 
else: 
    print("It's not there...") 

は大文字小文字を区別しないになるために:

+7

"' keyword.upper()== keyword.lowerは() '" 達成するために何を想定していますか? –

+0

offcourse keyword.upper()== keyword.lower()は常にFalseを返します – Harsha

+2

@HarshaBiyani: "42" –

答えて

2

あなたは、通常のサブストリングがinキーワードを使用して、別の文字列の一部であるかどうかをチェックします(または大文字、差異なしの両方):

if keyword.lower() in sentence.lower(): 
    # ... 
0

私は(上に設定しようとする際に、あなたのロジックを見ることができる)と下()と同じ値に、それはそのような非常に動作しません。

サブワードとキーワードを比較する場所では、両方とも常に小文字または両方が大文字になるように.upper()または.lower()を呼び出す必要があります。あなたはこのようなあなたのコードを変更する必要が

+0

' .lower() 'と' .upper() 'これは、OPの試行が間違っていた場所を示す際には最高の答えとなります。 –

0

sentence= input("Enter a sentence") 
keyword = input("Input a keyword from the sentence").lower() 
Found = False 
words = sentence.split(' ') 
for (i, subword) in enumerate(words) 
    if (subword.lower() == keyword) 
     print('keyword found at position', i+1) 
     Found = True 
if not found: 
    print("Sorry, that word wasn't found in the sentence") 
関連する問題