This next problem is another simple use of the time module.  Now, instead of simply collecting the time, we’re also setting a time limit on how long the player has to enter an answer.  This problem only required some simple modifications to the play_hand() function.  Those modifications included simply requesting a time limit from the player using raw_input(provides a prompt that allows the player to enter in the time limit).  I’ve made more modifications as I’ve been working on the problem, so I’m going to simplify the code for you, so you get the gist of what one would need to solve it.


def play_hand(hand, word_list):
 k = 2.0
 time_limit = int(get_time_limit(points_dict,k))
 total = 0
 initial_handlen = sum(hand.values())
 while sum(hand.values()) > 0:
       print 'Current Hand:',
       start_time = time.time()
       userWord = pick_best_word(hand,points_dict)
       end_time = time.time()
       total_time = end_time - start_time
       total_time = round(total_time,2)
       if total_time == 0:
           total_time = .01
       print 'It took %0.2f seconds to provide an answer.' % total_time
       time_left = time_limit - total_time
       if time_left > 0:
           print 'You have %0.2f seconds remaining.' % time_left
           print 'You have no time left.'


The basic difference between this alteration to the function, and the previous function, is simply the request for two new variables (time_limit, and time_left), which both provide integers, and a print statement.  Using basic mathematics you can determine how much longer that the player has to enter an answer by subtracting the time_limit from the total_time it took for the player to enter their first answer.  

Another part of this adjustment was including a conditional statement that would verify that the player still had time left to enter an answer.  You can see that with my last if conditional statement.


if time_left > 0:
           print 'You have %0.2f seconds remaining.' % time_left
           print 'You have no time left.' 


Basically, if the total remaining time left was equal to or less than zero, then you would be unable to continue the game.

Difficulties?  There were several issues with this problem, actually.  I encountered a, what it seems may be, consistent issue of mine.  When I am programming I sometimes place a the original function run within a loop, instead of outside of it.  When I place it in the loop, the variable I am trying to monitor is reset each time the loop restarted.  If I place it right outside the loop it runs once, and then all additional information is accumulated within the loop, like it needs to be done.  This sometimes instigates a feels of brain drain when I realize I need to fix it, because for some reason that idea takes a little more brain power than the rest.  The other aspect was learning to use the round() function that is built into python.  That was my first time using it, but not at all difficult task to learn.    

Important Notes: Often I find that additional conditional statements always seem to be required to make sure that the user doesn’t enter in information that doesn’t make sense.  From my own experiences prior to working with programming, I remember situations when I would use the program incorrectly, but the programmer would not have provided sufficient response information.  In such cases then I would get error messages with nothing in addition, so I would have no idea what I could do differently to fix the problem.  These little details seem to be part of the difference between a good application and a great application.

Up Next Time: My impressions on the difference between a good application and a great application