Problem Set 6; Problem 1 of 5
A module is a set of previously created functions that have been documented and are normally available for all python programmers to use. To access them you simply type in import *name of module* at the beginning of your code. Sometimes there are additional things needed, which you can read more about on http://docs.python.org/. The time module that I import at the beginning of my code allows me to implement time related functions within my game. Which brings me to my first problem in the problem set.
The first problem needs a simple end result. It simply wants you to alter your function play_hand() so that it can determine how long it takes you to enter your word, and then spit that information back out at you. As I had never worked with this module before, it was incredibly helpful that the MIT problem documentation gave the following hint.
Hint: The following code will tell you how long it takes to enter your name: import time start_time = time.time()name = raw_input('What is your name?: ') end_time = time.time() total_time = end_time - start_time print 'It took %0.2f to enter your name' % total_time |
This hint basically gives you exactly what you need to solve this problem. Start_time and End_time are variables, and time.time() is a function within the time module. When you place the start_time variable before a raw_input section (identified with the time.time() function), It will implement the function time.time() once the question has been printed. Then you identify end_time with the time.time() function, and it collects the total amount of time it took to enter the information into the question prompt.
Here is the altered play_hand() function that includes the time aspects. Please note that I am using the ps6.py document provided by the course, instead of my own ps5.py. That is because I realized after the fact that there is an error in one of my functions that I posting a review of here in the near future. I have made the text bold for the time module areas of the function that needed altering, as discussed above.
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:', display_hand(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 else: print 'You have no time left.' if userWord == '.' or userWord == '': break else: print userWord isValid = is_valid_word(userWord, hand, word_list) if not isValid: print 'Invalid word, please try again.' else: points = get_word_score(userWord, initial_handlen)/total_time total += points print '%s earned %d points. Total: %d points' % (userWord, points, total) hand = update_hand(hand, userWord) print 'Total score: %d points.' % total |
As you can see from the altered function, the time module additions are almost a clear cut and paste of the hint provided. An interesting note here, is that originally I missed part of the original problem, which was also to adjust how the points were assessed. The total points are to be divided by the total_time it took to complete the problem. That is shown at the very end, where the variable points is defined, and total_time divides the words score.
Difficulties? I didn’t really have any difficulties with this Problem Set. The hint made the implementation so easy to understand that I barely had to do any independent thinking to solve it. While the hint was very helpful, It made this problem fairly imbalanced in terms of difficulty in comparison with the rest of the Problem Set. What was problematic was the next problem set, when learning to collected the total_time and learn how to implement a time_limit function.
Up Next Time: Problem Set 6; Problem 2 of 5