Moving along with the ghost() function today.  If you remember from my last entry, I noted that I decided to separate out several of my smaller processes into individual functions.  I started out with explaining my function that collects the letters inputted by the players, verified that they are letters of the alphabet, and then added them to the list variable word.  

Today I’m going to explain the last two smaller functions I created.  These two functions are meant to verify that there is a vowel in the first three letters submitted, and once verified will return True.  First I started by creating a function only meant to check for vowels.  Here is my function.

 

def vowel(word):
   vowel = 'aeiou'
   for v in vowel:
       if v in word:          
           return True
   return False

 

This function takes in the word created and returned from the function play() I discussed in my last related entry.  The first variable I have assigned is vowel.  It’s assigned to a string of all the vowels in the English alphabet.  Then I use a for loop to go through each letter in the string vowel and check that letter against the word already created.  If there is a vowel in the word then the function will return True, otherwise it will return False.  Here is the second function.

 

def preghost(word):
   if len(word) == 3:
       foundvowel = vowel(word)
       if foundvowel == False:
           return False
       else:
           return True
   else:
       return True

 

This second function is used simply to check on the length of the word submitted, and then verify if the vowel() function returned a True or False value.  To do that, it checks if the length (len) of the word submitted was equal to 3.  If it is, then it checks to see if there is a vowel present by using the before mentioned function.  If there is no vowel the function will return False, otherwise it will return True.

I separated the two functions because it made it easier for me to view and understand my code.  I could most certainly have combined them, and might do so in the future, but as this was my first attempt at creating a more complicated piece of code on my own I decided to simplify it as much as possible.  Doing so helped me a lot in later debugging.

Notes on Debugging: The preghost() function was originally written differently.  When I first wrote it I hadn’t put in my return statements in the correct spots.  I had only placed them in once or twice and because of that my final processing function (ghost()), wasn’t actually running the function.  As my vowel() function is only called within the preghost() function, that meant I wasn’t calling either function.  When I started debugging I had no idea my bug lay in my poorly inputted return statements, that is, I didn’t have any idea until I had Robey review if for me.  It was a learning experience to know that my return statements from the preghost() function, placed incorrectly, was bypassing a good chunk of my code.  

Up Next Time: The final processing function to ghost()