At this point in the Problem Set, we’ve created and finished all the necessary functions required.  Now to put them together in one final function called play_hand().  This function will take all the other functions, and run them as needed to play the game.

 

def play_hand(hand, word_list):
   n = HAND_SIZE
   display_hand(hand)
   print "You may enter '.' if you cannot create a word."
   word = raw_input("Please enter a word:")
   score = 0
   totalscore = 0
   while not len(hand) == 0 and not word == ".":
       score = get_word_score(word,n)
       if is_valid_word(word,hand,word_list) == False:
           print "That is not a valid word"
           word = raw_input("Please enter a word:")
       elif is_valid_word(word,hand,word_list) == True:
           print "You word was worth " + str(score) + " points."
           totalscore = totalscore + score
           print "Your total score is: " + str(totalscore) + "."
           print "Here is your updated hand."
           hand = update_hand(hand,word)
           display_hand(hand)
           if len(hand) != 0:
               word = raw_input(enterword)
   print "Your total score is: " + str(totalscore) + "."
   return

The first chunk of this function is starting the game up.  First the hand size is defined as n.  Then the current hand is displayed using the display_hand function.  The player is then told they can enter a period ‘.’ if they cannot create a word.  The player is then asked to enter a word.  Finally one sees that score and totalscore are given arbitrary integer assignments, as they will be replaced as the game continues.  

The majority of this problem is solved using a combination of a while loop and three if statements.  In the beginning when I was trying to create this function I created it using a common misconception regarding while loops.  When I first wrote my while not line, I used or between my two conditionals.  I thought I was creating a while not loop that would take into consideration both conditionals, and only allow it to continue process while both were not valid.  However, it seems that with programming, a common misunderstanding is that exact linguistic difference.  One might think to use or, however within python one would use and.  Using and will make it a requirement that both conditionals be met before it exits the loop.  

So, back to the loop at hand.  Here I use a while not loop and state that as long as the length of the hand, len(hand),is not equal to zero and while the word entered is not equal to ‘.’, the following processes will occur.  

First the word that was entered is scored via the get_word_score(word,n) function.  Then an if statement is used as a conditional.  If the word is not a valid word (via the is_valid_word(word,hand,word_list) function), then the player is told that the word is not valid, and the player is then requested to enter another word.  

If the word is valid, then the player is told it is valid, along with the points for the word.  Then the total score is shown and the players hand is updated as the letters have been used.  Finally the newly updated_hand is displayed.  As long as the length of the hand is not zero, then the player will be requested to enter a word based on their current hand of letters.  Once there are no more words capable of being made or a ‘.’ is entered the while not loop is exited, and the total score is once more shared before one leaves the play_hand() function.  
          
One might ask why the score (via get_word_score()) is not calculated outside the loop.  This was a problem I had myself.  I originally calculated the score outside the loop and because I did it that way it never allowed the score or the totalscore to differentiate.  That meant that my total score was never being fully calculated.  The lesson learned here was the importance of identifying what items you want to be updated within a loop and which ones must be identified outside the loop.  If you place them in the wrong spot your information will never collect correctly.  

The last little piece I wanted to mention about problem four, was in how I learned to use if statements within a while loop.  I am still working on fully comprehending where the loop begins and ends, and sometimes loops within loops still confuse me.  I knew I could create an if block within a loop, I just wasn’t used to the logic.  As it was unfamiliar to me still I noticed how interacting with it was fairly confusing.  Until I finished debugging the problem and talked about it with Robey did I start to get a better handle on how they worked together, and how to use such combinations in the future.  

This was the first time I collected such a large group of functions to create one bigger final function.  It seemed quite easy at first, but I quickly learned how difficult debugging such a beast can be.  That knowledge was put to good use when working on the final problem in this set, problem 6.

What happened to problem 5?  Well, it seems that all problem 5 was, was to delete some sections of the previously written code and un-comment other parts, so the entire game could play once my final play_hand() function was finished.  So I did it, and the game was completed.

Up Next Time: Problem 6 of 6, the creation of a game called ghost.