Demo entry 6235189

Word Matcher

   

Submitted by anonymous on Oct 13, 2016 at 16:11
Language: Python 3. Code size: 4.9 kB.

#A453Task1 : Word Match
# A non-case-sensitive program that can identify all positions of where the
# word input by user occurs in a sentence without punctuation.
# program would return an error message if the word isn’t in the sentence.

#By Cecilia 7008 June 28th 
print ("*******************************")
print (" A453 Task1 : WordMatch ")
print ("*           Cecilia 7008           *")
print ()

#start code
PunctuationList=[ ".", "," , "!" ,"£", "$", ":" , ";" , "/" , ">", "@" , "+", "-" ]
#list of punctuations not allowed

IsValid = False
#boolean loop control until a valid sentence is entered

while IsValid== False:
#initialize IsValid is False at first (boolean)
          ValidateBasic= False     #initialize ValidateBasic is False at first (boolean)
          SentenceIn= input ("Enter a sentence without punctuation:  ")     #get input
          
          #start validation for SentenceIn
          if (SentenceIn== " ") or (SentenceIn== ""):     #if input is null or a space
                    print (" Error- please enter something!" )     #output error msg

          elif not SentenceIn.count(" ") > 0:     #if there isn't more than one word
                    print ("Error - please enter more than one word.")     #output error msg
          
          else:
                    ValidateBasic= True
                    
          #search for punctuations now
                    
          IsPunc =False
          #Initialize IsPunc is False at first (boolean)
          
          for Punchar in PunctuationList:  #count-controlled loop
                    
                    if Punchar in SentenceIn:
                    #if a punctuation is in the sentence inputted by the user
                              print ("Please do not enter punctuation.")    #output error message
                              
                              IsPunc=True
                              
          #Arrive here after punctuation check
                              
          IsValid= ValidateBasic and not IsPunc
          #with two or more words and no punctuations, the sentence is valid
                    
WholeSent= SentenceIn.upper()    #cast them all into caps
print (WholeSent)     #print the sentence back in capitals

IsValid = False
#boolean, loop control until a valid word is entered

while IsValid== False:     #initialize IsValid is False at first (Boolean)
          ValidateBasic= False     #initialize ValidateBasic is False at first (Boolean)
          
          WordIn= input ("Enter a word: ")    #get input
  
          #start validation for WordIn
          if (WordIn== " ") or (WordIn== ""):     #if input is null or a space
                    print (" Error- please enter something!" )     #output error msg
                    
          elif WordIn.count(" ") > 0:    #if there is more than one word
                    print ("Error - please enter less than two words.")    #output error msg

          else:
                    ValidateBasic= True
                    
          #search for punctuations now
                    
          IsPunc =False   #initialize IsPunc as False at first (Boolean)
          for Punchar in PunctuationList:  #count-controlled loop
                    
                    if Punchar in WordIn:  #if a punctuation is in the sentence inputted by the user
                              print ("Please do not enter punctuation.")  #output error message
                              
                              IsPunc=True
                              
          #Arrive here after punctuation check
                              
          IsValid= ValidateBasic and not IsPunc
          #with no more than one word and no punctuations, the sentence is valid
          
WholeWord= WordIn.upper() #cast them all into caps
print (WholeWord) #print the Word back in capitals

WordList= WholeSent.split() #go through sentence word by word

IsCorrect= False    #initialize IsCorrect to be False (Boolean)
for index in range (len (WordList)) :
#count-scontrolled loop
#search through list word by word
          
          SentWord= WordList [index]
          
          if SentWord==WholeWord:  #check whether these two words equal to each other
                    IsCorrect= True #making IsCorrect to be True
                    print ("The position of your word is: " ,(index+1)) #output the postition of word
                    #+1 to output actual no. as position 1 equals 0 in python
                    
if IsCorrect== False: #comparing if it's True or False so to only output error if word isn't in sentence
          print ("Error! Word is not in sentence!") #output if word input cannot be matched with the sentence

input ( ) #pause for any key - test purposes

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).