import re
while True:
*** sentence = input("Enter a sentence: ").casefold()
*** if not re.match("^[a-zA-Z ]*$", sentence):
******* print("Error! Only letters a-z allowed!")
*** else:
******* sentence.split()
*** break
splitsentence = sentence.split()
word = input("Enter a word from your sentence.")
if word in splitsentence:
* print("The word:",word, "is located on word number(s):", splitsentence.index(word)+ 1)
else:
* print("Error! Word not found in your sentence.")
import re
while True:
*** sentence = input("Enter a sentence: ").casefold()
*** if not re.match("^[a-zA-Z ]*$", sentence):
******* print("Error! Only letters a-z allowed!")
*** else:
******* sentence.split()
*** break
splitsentence = sentence.split()
word = input("Enter a word from your sentence.")
if word in splitsentence:
* print("The word:",word, "is located on word number(s):", splitsentence.index(word)+ 1)
else:
* print("Error! Word not found in your sentence.")
import re
def main():
sentence = input("Enter a sentence: ").casefold()
if not re.match("^[a-zA-Z ]*$", sentence):
print("Error! Only letters a-z allowed!")
return
word_count = len(sentence.split())
word = input("Enter a word from your sentence: ")
occurences = [occurence.start() for occurence in re.finditer(word, sentence)]
print("The word {} is located at: {}".format(word, str(occurences)))
if __name__ == "__main__":
main()
Copyright © 2024, NextGenUpdate.
All Rights Reserved.