使用字典获取拼字游戏的总分
Using a dictionary to get total points for a scrabble word
对于我正在参加的class,我真的很难回答这个问题。在这个问题中,我必须使用包含带点的字母的字典编写程序。所以输入任何单词,我都必须输出点数。我正在使用的程序是 Python.
这些是说明:
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).*
例如:
if the input is PYTHON
the output is 14
这是已经为我写的:
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 }
所以现在,我只需要编写一个代码来输出输入的任何单词的分数。我不知道从哪里开始...
你可以一个字符一个字符地枚举单词,然后使用这个字符作为tile_dict
字典的键来获取字符值。您可以使用 sum()
函数对这些值求和:
word = "PYTHON"
cnt = sum(tile_dict[char] for char in word)
print(cnt)
打印:
14
对于我正在参加的class,我真的很难回答这个问题。在这个问题中,我必须使用包含带点的字母的字典编写程序。所以输入任何单词,我都必须输出点数。我正在使用的程序是 Python.
这些是说明:
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).*
例如:
if the input is PYTHON
the output is 14
这是已经为我写的:
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 }
所以现在,我只需要编写一个代码来输出输入的任何单词的分数。我不知道从哪里开始...
你可以一个字符一个字符地枚举单词,然后使用这个字符作为tile_dict
字典的键来获取字符值。您可以使用 sum()
函数对这些值求和:
word = "PYTHON"
cnt = sum(tile_dict[char] for char in word)
print(cnt)
打印:
14