2016-12-13 1 views
-2
varSentence = ("The fat cat sat on the mat") 

print (varSentence) 

varWord = input("Enter word ") 

varSplit = varSentence.split() 

if varWord in varSplit: 
    print ("Found word") 
else: 
    print ("Word not found") 
    for (num, x) in enumerate(sentence): 
    if word == x: 
     print ("Your word is in position",num,"!") 
+6

Ist das:

for i in enumerate(varSplit): if i[1] == varWord: print(i[0]) 

Sie die diese oben wie verwenden können eine Hausübung? [Sehr ähnliche Frage] (http://stackoverflow.com/questions/41121622/need-to-get-it-to-display-the-position-of-the-inputted-word-not-every-word) wurde gepostet ungefähr 15 Minuten vor diesem. – elParaguayo

+0

http://stackoverflow.com/questions/41121622/need-to-get-it-to-display-the-position-of-the-inputted-word-not-every-word/41122004?noredirect=1#comment69447633_41122004 – harshil9968

Antwort

0

Versuchen mit enumerate ist:

varSentence = ("The fat cat sat on the mat") 

varWord = input("Enter word ") 

varSplit = varSentence.split() 

if varWord in varSplit: 
    for i in enumerate(varSplit): 
     if i[1] == varWord: 
      print("Your word is in position", i[0], "!") 
      break # To stop when the first position is found! 
else: 
    print ("Word not found") 
+0

@JacobStaple Bearbeiten Sie Ihre Frage und markieren Sie Python3x –

Verwandte Themen