
Python Course: Dictionaries
In this project, I wrote a script to keep track of point totals for people playing a game of scrabble. This project was to practice what I learned about dictionaries in Python.
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 4, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
#combined into a dictionary that maps a letter to its point value.
letter_to_points = {letters:points for letters, points in zip(letters, points)}
letter_to_points[" "] = 0 #for blank tiles
#a function that takes a word, loops through its letters, returns the points for that word.
def score_word(word):
point_total = 0
for i in word:
point_total += letter_to_points.get(i, 0)
return point_total
#testing the score_word function
brownie_points = score_word("BROWNIE")
#print(brownie_points)
#Each player's words
player_to_words = {"player1": ["BLUE", "TENNIS", "EXIT"], "wordNerd": ["EARTH", "EYES", "MACHINE"], "Lexi Con": ["ERASER", "BELLY", "HUSKY"], "Prof Reader": ["ZAP", "COMA", "PERIOD"]}
#Loops through the words of each player and adds up each player's points
player_to_points = {} #the score
for player, words in player_to_words.items():
player_points = 0
for word in words:
player_points += score_word(word)
player_to_points[player] = player_points
print(player_to_points)
#a function that would take in a player and a word, and add that word to the list of words they’ve played
def play_word(player, word):
player_to_words[player].append(word)