Guest User

ps5.py

a guest
Apr 26th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. def play_hand(hand, word_list):
  2.     """
  3.    Allows the user to play the given hand, as follows:
  4.  
  5.    * The hand is displayed.
  6.    
  7.    * The user may input a word.
  8.  
  9.    * An invalid word is rejected, and a message is displayed asking
  10.      the user to choose another word.
  11.  
  12.    * When a valid word is entered, it uses up letters from the hand.
  13.  
  14.    * After every valid word: the score for that word and the total
  15.      score so far are displayed, the remaining letters in the hand
  16.      are displayed, and the user is asked to input another word.
  17.  
  18.    * The sum of the word scores is displayed when the hand finishes.
  19.  
  20.    * The hand finishes when there are no more unused letters.
  21.      The user can also finish playing the hand by inputing a single
  22.      period (the string '.') instead of a word.
  23.  
  24.    * The final score is displayed.
  25.  
  26.      hand: dictionary (string -> int)
  27.      word_list: list of lowercase strings
  28.    """
  29.     word = ''
  30.     total_score = 0
  31.     letters_left = HAND_SIZE
  32.    
  33.     while word <> '.':
  34.         if letters_left == 0:
  35.             return
  36.         print 'Current Hand:',
  37.         display_hand(hand)
  38.         print 'Enter word, or (.) to indicate that you are finished:',
  39.         word = raw_input().lower()
  40.         if word == '.':
  41.             print 'Total score:', total_score, 'points.',
  42.             return
  43.         elif not is_valid_word(word, hand, word_list):
  44.                 print 'Invalid word, please try again.'
  45.         else:
  46.             score = get_word_score(word, HAND_SIZE)
  47.             total_score += score
  48.             print word, 'earned', score, 'points. Total:', total_score, 'points'
  49.             hand = update_hand(hand, word)
  50.             letters_left -= len(word)
Advertisement
Add Comment
Please, Sign In to add comment