python - Need assistance with sentence analysis -


my code takes sentence , finds given word in sentence.

if word in sentence needs has found word , positions said word in.

if word not in sentence should display error message.

i have this:

print("please insert sentence without punctuation") sentence=(input()) variable1='sentence' print("which word find in sentence?") word=input() variable2='word' if 'word'=='country':     'variable3'==5     'variable4'==17 if word in sentence:     print([word], "is in positions", [variable3], "and", [variable4]); else:     print("your word not in sentence!") 

i want deal misunderstandings in presented code.

first,

print("please insert sentence without punctuation") sentence=(input()) 

is simpler

sentence = input("please insert sentence without punctuation") 

now have variable called sentence wihich should not muddled string 'sentence'

similarly can say

word = input("which word find in sentence?") 

gives variable word again not muddled string 'word'

suppose sake of argument have,

sentence = "has got elephant in?" 

and search word 'elephant'

the posted code attempts use in, happen:

>>> "elephant" in sentence true >>> "ele" in sentence true >>> "giraffe" in sentence false >>> 

close. not close enough. not looking whole word, since found 'ele' in 'elephant'.

if split sentence words, suggested other answer, can search whole words and find position. (look split; can choose other characters default ' ').

words = sentence.split() word = 'ele' words.index(word) 

if word isn't there error:

traceback (most recent call last): file "<stdin>", line 1, in <module> valueerror: 'ele' not in list 

i leave error handling you.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -