The final Ghost() function
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" |
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." |
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