Advertisement
Guest User

PS3 #4 issue

a guest
Sep 22nd, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. def play_hand(hand, word_list):
  2.  
  3.     """
  4.    Allows the user to play the given hand, as follows:
  5.  
  6.    * The hand is displayed.
  7.    
  8.    * The user may input a word.
  9.  
  10.    * An invalid word is rejected, and a message is displayed asking
  11.      the user to choose another word.
  12.  
  13.    * When a valid word is entered, it uses up letters from the hand.
  14.  
  15.    * After every valid word: the score for that word is displayed,
  16.      the remaining letters in the hand are displayed, and the user
  17.      is asked to input another word.
  18.  
  19.    * The sum of the word scores is displayed when the hand finishes.
  20.  
  21.    * The hand finishes when there are no more unused letters.
  22.      The user can also finish playing the hand by inputting a single
  23.      period (the string '.') instead of a word.
  24.  
  25.      hand: dictionary (string -> int)
  26.      word_list: list of lowercase strings
  27.      
  28.    """
  29.  
  30.     #set variables for word score and hand score
  31.     totalScore = 0
  32.     score = 0
  33.     newHand = hand.copy()
  34.     while len(newHand) > 0:
  35.         # display available letters
  36.         print 'Current Hand:',
  37.         display_hand(newHand)
  38.         # ask what user wants to do
  39.         word = raw_input('Enter word, or a "." to indicate that you are finished: ')
  40.         if word == '.':
  41.             # game over! go print the total score.
  42.             break
  43.         elif not is_valid_word(word, newHand, word_list):
  44.             # returns to beginning of loop
  45.             print 'Invalid word, please try again.'
  46.         else:
  47.             # update hand and score word
  48.             newHand = update_hand(newHand, word)
  49.             score = get_word_score(word, HAND_SIZE)
  50.             totalScore = totalScore + score
  51.             #display result and total score
  52.             print '"', word, '" earned', score, 'points. Total:', totalScore, 'points.'
  53.     print 'Total score:', totalScore, 'points.'
  54.  
  55.  
  56. def calculate_handlen(hand):
  57.     handlen = 0
  58.     for v in hand.values():
  59.         handlen += v
  60.     return handlen
  61.    
  62. play_hand({'r': 1, 'a': 3, 'p': 2, 'e': 1, 't': 1, 'u':1}, load_words())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement