First time posting but I have used your site a lot to help me out, so thanks for that. I have hit a wall with what I am trying to do. The code I have below will allow a user to enter a sentence and then a word. It will then return the position of the word in the sentence. The issue I need help with is, if the user enters a word that is not in the sentence I want an error message to appear. I have looked into else and elif statements which to me would be the logical option but I cannot get them to work, I am not sure if I am putting them in the right place (I am sure its something simple its just I cannot see it). Any help or a point in the right direction would be most appreciated.
sentence = raw_input ("Enter sentence: ")
x = 0
message = "Your Search Word Appears at position(s) "
print (sentence)
search_word = raw_input("Enter your word ").lower()
if sentence.find(search_word):
print "success"
word_list = sentence.lower().split()
for word in word_list:
if word == search_word:
message = message + str (x+1)+ ",";
x += 1;
print (message)
Aucun commentaire:
Enregistrer un commentaire