Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def play_hand(hand, word_list):
- """
- Allows the user to play the given hand, as follows:
- * The hand is displayed.
- * The user may input a word.
- * An invalid word is rejected, and a message is displayed asking
- the user to choose another word.
- * When a valid word is entered, it uses up letters from the hand.
- * After every valid word: the score for that word is displayed,
- the remaining letters in the hand are displayed, and the user
- is asked to input another word.
- * The sum of the word scores is displayed when the hand finishes.
- * The hand finishes when there are no more unused letters.
- The user can also finish playing the hand by inputting a single
- period (the string '.') instead of a word.
- hand: dictionary (string -> int)
- word_list: list of lowercase strings
- """
- #set variables for word score and hand score
- totalScore = 0
- score = 0
- newHand = hand.copy()
- while len(newHand) > 0:
- # display available letters
- print 'Current Hand:',
- display_hand(newHand)
- # ask what user wants to do
- word = raw_input('Enter word, or a "." to indicate that you are finished: ')
- if word == '.':
- # game over! go print the total score.
- break
- elif not is_valid_word(word, newHand, word_list):
- # returns to beginning of loop
- print 'Invalid word, please try again.'
- else:
- # update hand and score word
- newHand = update_hand(newHand, word)
- score = get_word_score(word, HAND_SIZE)
- totalScore = totalScore + score
- #display result and total score
- print '"', word, '" earned', score, 'points. Total:', totalScore, 'points.'
- print 'Total score:', totalScore, 'points.'
- def calculate_handlen(hand):
- handlen = 0
- for v in hand.values():
- handlen += v
- return handlen
- 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