Introduction
Scrabble is a popular word game where players form words on a board using letter tiles, each of which has a point value. In this tutorial, we will learn how to calculate the score of a Scrabble word using Python.
Objective
The objective of this tutorial is to write a Python program that calculates the score of a given word in the game of Scrabble. Each letter in Scrabble has a different point value, and we will use these values to compute the score of any word provided as input.
Python Code
# Scrabble Score Calculator Program in Python # Define the score for each letter in Scrabble scrabble_scores = { '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 } def calculate_scrabble_score(word): """ Calculate the Scrabble score for the given word. Parameters: word (str): The word for which the score is to be calculated. Returns: int: The total score of the word. """ score = 0 for letter in word.upper(): if letter in scrabble_scores: score += scrabble_scores[letter] return score # Input word from the user word = input("Enter a word to calculate its Scrabble score: ") score = calculate_scrabble_score(word) print(f"The Scrabble score for the word '{word}' is: {score}")
Explanation of the Program
In the program, we first define a dictionary scrabble_scores
where each key is a letter of the alphabet and the value is its corresponding point value in Scrabble.
The calculate_scrabble_score
function takes a word as input, converts it to uppercase (since Scrabble tiles are case-insensitive), and then iterates through each letter in the word. For each letter, it checks if it exists in the dictionary and adds its score to a running total. Finally, the total score is returned.
The program prompts the user to enter a word and calculates its Scrabble score using the function. The result is then printed to the console.
How to Run the Program
- Ensure you have Python installed on your machine. You can download it from the official Python website.
- Create a new Python file (e.g.,
scrabble_score.py
) and copy the code above into it. - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Run the program by typing
python scrabble_score.py
(orpython3 scrabble_score.py
on some systems) and press Enter. - Enter a word when prompted, and the program will calculate and display the Scrabble score for that word.