When I started creating the ghost() function, I thought I could do most of it within one function.  I probably still could, but at the time it made it really difficult to understand what I was doing.  I had almost written it all when I realized I had no idea how to implement a cycle between the two players.  At first I had no idea how to do it.  Then Robey showed me a pretty common way to cycle, by using mod.  

Mod is a way to wrap, or find the remainder of something.  Bare with me here, because I’m still learning this myself.  Here is the basic code for using mod to cycle (or toggle) between two different players.  

 

i = 0
while True:
i += 1
i %= 2
if i == 1:
   print "player 1 action's"
if i == 0:
   print "player 2 action's"

 

First you assign your base variable i, which instigates the cycle.  Then you create a while loop that will increase incrementally, here it is incrementally increased by one.  Then you choose a total number of players.  You can see that we indicate two.  Then we place in an if statement which allows us to designate the different players actions.

How does mod work?  Mod will show the remainder, after i is divided by the number given.  The first time our loop is run, i is equal to one.  One is not divisible by two, and therefore the remainder of that division is one.  The second time this loop is run i is equal to two.  When two is divided by two the remainder is zero.  The third time this loop runs i is equal to three, and when three is divided by two the remainder is one.  Thus the cycle continues, and that’s how I was able to set up a system to cycle between the two players.  Using this idea, I was able to finish creating my function, shown below.

 

def ghost():
   i = 0
   word = ""
   while True:
       i += 1
       i %= 2
       if i == 1:
           print "Player 1's turn"
           word = play(word)
           if preghost(word) == False:
               return "Game cannot proceed as there were no vowels in the first three entries."
           elif word in word_list and len(word) > 3:
               return "Player 1 has lost the game."
       elif i == 0:
           print "Player 2's turn"
           word = play(word)
           if preghost(word) == False:
               return "Game cannot proceed as there were no vowels in the first three entries."
           elif word in word_list and len(word) > 3:
               return "Player 2 has lost the game."                      

 

As you can see I assign i to zero and word to a blank string before I implement the while loop.  This while loop uses mod to cycle and then the rest of the functions created are inserted.  First I show whose turn it is and then run the function play(word).  That function provides the variable word,which I then run against the preghost() function.  That function returns True or False.  If it’s False I know that the word has no vowels within the first three letters submitted and the game is over.  Otherwise the game continues until one player, I’m assuming accidentally, creates a word.  Once that occurs, the player who has lost the game is identified and the game ends.  

What did I learn? The major things I learned from creating this game was how to create a cycle between n amount of players.  I feel like I better understand how to use mod.  I relearned that strings can be used like lists with for loops.  I also better understand how the incorrect placement of return statements can, at times, make functions interact entirely differently than you want.  Lastly I learned the importance of separating out my processes into smaller functions, even if simply to create code that is more easily read.

Up Next Time: Musings on my own personal programming hurdles