Scabble Project in Py 3

Hi Team

I need some help with a project in Py 3. At the end of dictionary chapter is scrabble project. I have question regarding the last Task on it : “Make your defined dictionary that has upper case alphabets, handle lower case as well”

I can easily populate a new dictionary with lower case alphabets from the older dictionary, but is there a way that i can implement in dictionary comprehension where I could check my given alphabet to both upper and lower case using just one dictionary.
i mean i want to avoid generating another dictionary , just to cater to lower case scenario.

Thank you

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi, @rdb20 – Well, one way would be to make each key a tuple (“A”, “a”), (“B”, “b”), etc., but then you would need to modify score_word(), to poll the keys for the presence of the character: if letter in key:, etc.

What if you just convert inputs to upper case?

def scrabble_score(word):
    score = 0
    for letter in word.upper():
        score += scores(letter)

Thank you mtf. Loved the solution. That is fairly straightforward. :ok_hand:

1 Like

Hi Patrick

What does polling the keys means. Did you mean I should run a loop to check the word against both values in the key tuple?

@mtf’s solution is obviously the most straightforward approach.

I was under the impression that the problem was focused on modifying the dictionary. If that’s what you are trying to do, and you’re using tuples you can check like this:

def score_word(word):
    point_total = 0
    for letter in word:
        for key in letter_to_points:
            if letter in key:  # Just use "in"; no need to check tuple items separately
                point_total += letter_to_points[key]
    return point_total

Thank you Patrick. Two solutions is better than one. This seems easy too.
Thank you.

1 Like

Here is my code:

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} def scrabble_score(word): marks = 0 for letter in word: marks += score[letter.lower()] print (marks) return marks scrabble_score("nishant")