Demo entry 6235162

python

   

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

def Validation(StrToValid):
      
      if (StrToValid == "") or (StrToValid == " "):       # fails the presence check
            print(" Error! Nothing has been input. Please enter a sentence.")
            return False

      # punctuation presence check
      Punctuations = "!'£$%^&*()_+=~#{}[]:;@<>,.?/|\""        # created a list of punctuation

      for PuncChar in Punctuations:
            if PuncChar in StrToValid:     # find whether there is Punctuations in UserSent
                  print(" Error! There are punctuations in the sentence. ")
                  return False
      return True             # when it goes to here, it is valid

#  Prodedure to check whether there is a space to determine is a word or sentence
def SpaceCheck(StrToValid):

      global Spacing          # making "Spacing" available outside the module
      
      # counting whitespace in StrToValid          
      NumWhitespace = StrToValid.count(" ")
      if (NumWhitespace < 1):
            Spacing = "word"
            print(" This is word. ")
            print()

      else:
            Spacing = "sentence"
            print(" This is a sentence. ")
            print()

#  Splash:
print(" This is A453 Task 1 - search a word's position in a sentence. ")
print(" By Wu Sze Lam Christy - 7069")
print()

#  Get inputs with validation:
IsValid = False     #  boolean for validation


while not IsValid :

      UserSent = input(" Please enter a SENTENCE without any punctuation: ")

      if not Validation(UserSent):
          print(" Please try again. ")
          print()

      else:
            SpaceCheck(UserSent)
            if Spacing == "word":
                  print(" Please try again. ")
                  print()

            else:
                  IsValid = True

IsValid = False     #  boolean for validation


while not IsValid :

      UserWord = input(" Please enter a WORD without any punctuation: ")

      if not Validation(UserWord):
          print(" Please try again. ")
          print()

      else:
            SpaceCheck(UserWord)
            if Spacing == "sentence":
                  print(" Please try again. ")
                  print()

            else:
                  IsValid = True



#  Capitalise the inputs:
UserSent = UserSent.upper()     #  http://www.tutorialspoint.com/python/string_upper.htm
UserWord = UserWord.upper()

#  Confirm what the user had entered:
print(" Your sentence: %s " %UserSent)
print(" Your word to search is: %s" %UserWord)
print()

#  split the sentence into individual words:
ListOfWord = UserSent.split(" ")        # split the sentence into a list of words by whitespace
print("There are:")
Seperator = ", "
print(Seperator.join(ListOfWord))
print("in your sentence.")
print()


IsFound = False
for WordNum in range(len(ListOfWord)):      # go through word by word
    WordSent = ListOfWord[ WordNum ]
    
    if WordSent == UserWord:         # when there is a match
        IsFound = True
        print(" Word poition: " , (WordNum + 1))
        
#  Checked all words
if IsFound == False:        # word is not found after all words matched once 
    print(" The word is not found. ")       # error message when word is not in sentence

input()           # pause for any key - test purpose

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).