Python Homework Help
Python Homework Help
Dealing
Scoring
● The score for the hand is the sum of the score for each word
formed.
● The score for a word is the product of two components:
o First component: the sum of the
points for letters in the word.
o Second component: either [7 * word_length - 3 * (n-
word_length)] or 1, whichever value is greater,
where:
▪ word_length is the number of letters used in the
word
▪ n is the number of letters available in the
current hand
● Letters are scored as in Scrabble; A is worth 1, B is worth 3, C
is worth 3, D is worth 2, E is worth 1, and so on. We have
defined the dictionary SCRABBLE_LETTER_VALUES that maps
each lowercase letter to its Scrabble letter value.
● Examples:
o For example, if n=6 and the hand includes 1 'w', 2 'e's,
and 1 'd' (as well as two other letters), playing the word
'weed' would be worth 176 points: (4+1+1+2) * (7*4 -
3*(6-4)) = 176. The first term is the sum of the values
of each letter used; the second term is the special
computation that rewards a player for playing a longer
word, and penalizes them for any left over letters.
Getting Started
test_get_word_score
test_update_hand
Test the update_hand
implementation.
test_is_valid_word
test_wildcard
s = “My
string”
print(s.l
ower())
>>>> “my
string”
If you don’t know what this does you could try typing
help(str.lower) in your Spyder shell to see the documentation
for the functions.
Displaying a hand
Testing: Make sure the test_update_hand tests pass. You may also
want to test your
implementation of update_hand with some
reasonable inputs.
Problem 3. Valid words
At this point, we have not written any code to verify that a word given
by a player obeys the rules of the game. A valid word is in the word
list (we ignore the case of words here) and it is composed entirely of
letters from the current hand.
During the game, a player wishing to use a wildcard should enter "*"
(without quotes) instead of the intended letter. The word-validation
code should not make any assumptions about what the intended
vowel should be, but should verify that at least one valid word can
be made with the wildcard as a vowel in the desired position.
Current Hand: * z
Enter word, or "!!" to indicate that you
are finished: !! Total score: 198 points
Current Hand: o s
Enter word, or "!!" to indicate
that you are finished: !! Total
score: 0 points
Current Hand: c z
Enter word, or "!!" to indicate
that you are finished: !! Total
score: 0 points
Note that after the line # BEGIN PSEUDOCODE there is a bunch of,
well, pseudocode! This is to help guide you in writing your
function. Check out the Why Pseudocode? resource to learn more
about the What and Why of Pseudocode before you start this
problem.
Testing: Try out your implementation as if you were playing the
game: run your program and call the play_hand function from your
shell with a hand and the word_list.
Note: Your output should match the examples below. You should
not print extraneous "None" messages.
Example #1
Current Hand: a j e f * r x
Enter word, or "!!" to indicate that you are
finished: jar "jar" earned 90 points. Total:
90 points
Current Hand: * f x e
Enter word, or "!!" to indicate that you are
finished: f*x "f*x" earned 216 points.
Total: 306 points
Current Hand: e
Enter word, or "!!" to indicate that you are
finished: !! Total score: 306 points
Example #2
Current Hand: a c f i * t x
Enter word, or "!!" to indicate that you are
finished: fix "fix" earned 117 points.
Total: 117 points
Current Hand: a c t *
Enter word, or "!!" to indicate that you are
finished: ac That is not a valid word.
Please choose another word.
Current Hand: t *
Enter word, or "!!" to indicate that you are
finished: *t "*t" earned 14 points. Total:
131 points
Note that we are not providing you with pseudocode for this problem.
However, as you are deciding how to implement these functions, you
may want to write your own as a guideline.
Testing: Try out this implementation as if you were playing the
game. Try out different
values for HAND_SIZE with your program, and be sure that you can play
the word game with
different hand sizes by modifying only the
variable HAND_SIZE.
Example
Enter total number
of hands: 2
Current hand: a c i
* p r t
Would you like to
substitute a
letter? no
Current hand: a c i * p r t
Please enter a word or '!!' to
indicate you are done: part "part"
earned 114 points. Total: 114 points
Current hand: c i *
Please enter a word or '!!' to
indicate you are done: ic* "ic*"
earned 84 points. Total: 198 points
Current hand: d d * a o
u t
Please enter a word or '!!' to
indicate you are done: out "out"
earned 27 points. Total: 27 points
Current hand: d d * a
Please enter a word or '!!' to
indicate you are done: !! Total
score for this hand: 27
Would you
like to
replay the
hand? yes
Current
hand: d d *
a o u t
Please enter a word or
'!!' to indicate you are
done: d*d "d*d" earned 36
points. Total: 36 points
Current hand: a o u t
Please enter a word or
'!!' to indicate you are
from
done:ps3 import *
out
#
# Test code
#
def test_get_word_score():
"""
Unit test for get_word_score
"""
failure=False
# dictionary of words and scores
words = {("", 7):0, ("it", 7):2,
("was", 7):54, ("weed", 6):176,
("scored", 7):351, ("WaYbILl",
7):735,
("Outgnaw", 7):539,
("fork", 7):209,
("FORK", 4):308}
for (word, n) in
words.keys():
score =
get_word_score(word, n)
if score !=
words[(word, n)]:
print("FAILURE:
test_get_word_score()")
print("\
tExpected", words[(word, n)],
"points but got '" + \
str(score) +
"' for word '" + word + "',
n=" + str(n))
failure=True
if not failure:
print("SUCCESS:
test_get_word_score()")
# end of
test_get_word_score
def test_update_hand():
"""
Unit test for
update_hand
"""
# test 1
handOrig = {'a':1,
'q':1, 'l':2, 'm':1, 'u':1,
'i':1}
handCopy = handOrig.copy()
word = "quail"
# test 2
handOrig = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2}
handCopy = handOrig.copy()
word = "Evil"
if handCopy != handOrig:
print("FAILURE: test_update_hand('"+ word
+"', " + str(handOrig) + ")")
print("\tOriginal hand was", handOrig)
print("\tbut implementation of
update_hand mutated the original hand!")
print("\tNow the hand looks like this:",
handCopy)