Using a dictionary to get total points for a scrabble word

Question:

I am having a really hard time answering this question for a class that I am taking. In this problem, I have to write a program using a dictionary containing letters with points. So any word that is entered, I have to output the number of points. The program that I am using is Python.

These are the instructions:

Scrabble is a word game in which words are constructed from letter tiles, each letter tile containing a point value. The value of a word is the sum of each tile’s points added to any points provided by the word’s placement on the game board.*

Write a program using the given dictionary of letters and point values that takes a word as input and outputs the base total value of the word (before being put onto a board).*

EX:

if the input is PYTHON

the output is 14

This is what is already written for me:

tile_dict = { 'A': 1, 'B': 3, 'C': 3, 'D': 2, 'E': 1, 'F': 4, 'G': 2, 'H': 4, 'I': 1, 'J': 8, 
          'K': 5, 'L': 1, 'M': 3, 'N': 1, 'O': 1, 'P': 3, 'Q': 10, 'R': 1, 'S': 1, 'T': 1, 
          'U': 1, 'V': 4, 'W': 4, 'X': 8, 'Y': 4, 'Z': 10 } 

So now, I just had to write a code that will output the score of any word that is input. I am not sure where to start…

Asked By: ellie1

||

Answers:

You can enumerate the word char by char, then use this char as key to the tile_dict dictionary to obtain character value. You can sum these values with sum() function:

word = "PYTHON"

cnt = sum(tile_dict[char] for char in word)
print(cnt)

Prints:

14
Answered By: Andrej Kesely

If the input is:

PYTHON

the output is:

PYTHON is worth 14 points.

tile_dict = { ‘A’: 1, ‘B’: 3, ‘C’: 3, ‘D’: 2, ‘E’: 1, ‘F’: 4, ‘G’: 2, ‘H’: 4, ‘I’: 1, ‘J’: 8,
‘K’: 5, ‘L’: 1, ‘M’: 3, ‘N’: 1, ‘O’: 1, ‘P’: 3, ‘Q’: 10, ‘R’: 1, ‘S’: 1, ‘T’: 1,
‘U’: 1, ‘V’: 4, ‘W’: 4, ‘X’: 8, ‘Y’: 4, ‘Z’: 10 }

word = input()

cnt = sum(tile_dict[char] for char in word)
print(f'{word} is worth {cnt} points.’)

Answered By: PAO
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.