Question: Python Coding The player is dealt a hand of n letters chosen at random. For now, n will be 7. The player forms one or
Python Coding
The player is dealt a hand of n letters chosen at random. For now, n will be 7.
The player forms one or more words using the letters in the hand. Each letter may be used only once. It is not necessary to use every letter. Unused letters are not scored.
The score for each word is the sum of the point values for the letters in the word, plus a 50-point bonus if all n letters are used in the word. The score for the hand is the sum of the scores of the words formed from the hand.
Letters are scored as in Scrabble: A is worth 1, B is worth 3, C is worth 3, D is worth 2, etc. I have defined a dictionary SCRABBLE LETTER VALUES containing the point values for each letter.
For example, if is worth10points(1+1+4+4=10). Another example: if n=7 and you make the word spigots, you would receive 60 points (10 points for the letter values and 50 points for using all n letters in one word).
A) We begin with a function that will compute the score for a single word. Fill in the code for the get word score function: def get_word_score(word, n):
Returns the score for a word. Assumes the word is a valid word.
The score for a word is the sum of the points for letter in the word, plus 50 points if all n letters are used on the first go. Letters are scored as in Scrabble; A is worth 1, B worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.
n: int (number of letters in a fresh hand)
word: string (all lowercase letters)
returns: int (the score for the given word)
Representing a hand
Imagine that the player holds the letters q, i, i, i, r, e, e. We could represent this hand using a list in Python:
hand = ['q', 'i', 'i', 'i', 'r', 'e', 'e']
Instead, however, well represent the hand using a dictionary
hand = {'q':1, 'i':3, 'r':1, 'e':2}
B) def update_hand(hand, word):
Assumes that 'hand' has all the letters in 'word. In other words, assumes that however many times a letter appears in 'word', 'hand' has at least as many of that letter in it. Returns a dictionary representing the new hand, which contains all the letters in 'hand', minus the letters in 'word'. Has no side effects! Does not mutate 'hand'.
word: string/ hand: dictionary (string -> int/ returns: dictionary (string -> int)
c) def is_valid_word(word, hand, word_list):
"""
Returns True if word is in word_list and is entirely
composed of letters in hand. Otherwise, returns False.
Does not mutate hand or word_list.
word: string
hand: dictionary (string -> int)
word_list: list of lowercase strings
"""
D)
Fill in the code for the play hand function.
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 and the total
score so far are 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 entering a single
period (the string '.') instead of a word.
* The final score is displayed.
hand: dictionary (string -> int)
word_list: list of lowercase strings
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
