In: Computer Science
Use this constant dictionary as a global variable:
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 }
Implement function scrabblePoints(word) that returns the calculated points for the word based on the tile_dict above. The word parameter is a string. This function takes the string and evaluates the points based on each letter in the word (points per letter is set by the global dictionary). P or p is worth the same points. No points calculated for anything that is not A-Z or a-z.
[You may use upper() and isalpha() ONLY and no other method or built-in function]
Examples:
word = “PYTHON”
print(scrabblePoints(word))
returns:
14
word = “hello!!”
print(scrabblePoints(word))
returns:
8
word = “@#$=!!”
print(scrabblePoints(word))
returns:
0
Code
#defin ethe dictionary
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 }
#create and define teh scrablle method
def scrabblePoints(words):
global tile_dict
points = 0 #set initital points 0
#itearte each character in words:
for i in list(words):
#check if the current chacatre is alphabet
if i.isalpha():
points += tile_dict[i.upper()]
#return point
return points
word = "PYTHON"
print(scrabblePoints(word))
word = "hello!!"
print(scrabblePoints(word))
word = "@#$=!!"
print(scrabblePoints(word))
Screenshot
Output