简体   繁体   中英

Python - Scoring a list of words using a dictionary of Scrabble letter values

Sorry if this is easy all, I'm brand new to coding.

I'm given a list of words, ie

my_words = [ 'Apple", 'banana', 'Grape', 'orange' ]

I'm also given a dictionary of letter values,

letter_value = {'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':8, 'w':4, 'x':8, 'y':4, 'z':10}

I have to

create a function that takes a word and computes the scrabble score for that word, which I did:

def scrabblescore(words): 
     total = 0
     for letter in word: 
         total += letter_value[letters]
     return total 

when I try it out, print(scrabblescore("myscore") it is working properly

What I don't know how to do is

write a function that will take the whole list of words, iterate over the list, and use the first function to compute the score of each word in the list and make a dictionary such as

{'apple': 20, 'banana':10} etc.

Thanks in advance all

I'm not going to put the full code here (that's for you to figure out) but I will give you what you should be doing.

1: Set up a dictionary variable.

2: Iterate through the values. For every word in my_words, do:

3: Iterate through the letters. For every letter in word in my_words, do:

4: Find the value for the key in the dictionary for the letter. Add it to a temporary variable.

5: Set the word as the key and the temporary variable as the value.

First fix your method, regarding the name of the variables, and use .lower() to deal with uppercase letters

def scrabble_score(word):
    total = 0
    for letter in word:
        total += letter_value[letter.lower()]
    return total

Then for the list of word, you can either use a dict-comprehension or the classic for-loop

def scrabble_score_words(words):
    return {word: scrabble_score(word) for word in words}


def scrabble_score_words(words):
    result = {}
    for word in words:
        result[word] = scrabble_score(word)
    return result

这是一个 foreach 字符循环:

for element in string_name:
    

You can make it like:

def scrabble_score(word):
    total = 0
    for letter in word:
        # hit here
        # 1. added lower() to make the letter lowercase
        # 2. IMPORTANT use letter 
        total += letter_value[letter.lower()] 
    return total

def scrabble_score_list(words):
    """take a list of the words"""
    # Just a wrapper for the first function
    rd = {}
    for x in words:
        rd[x] = scrabble_score(x)
    return rd

I love doing homework assignments. I beg you to learn; not just copy.

""" Yet another SO homework assignment """

letter_value = {'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':8, 'w':4, 'x':8, 'y':4, 'z':10}


def scrabble_score(word):
    """
    For a specific word, calculate the scrabble score of that word.
    :returns: The numeric scrabble score of a word.
    """
    total = 0
    for letter in word.lower():
        total += letter_value[letter]
    return total


def scrabble_dict(word_list):
    """
    A function that will take the whole list of words, iterate over the list,
    and use the scrabblescore() to compute the score of each word in the list
    and make a dictionary like {'Apple': 20, 'Banana':10}
    """
    word_dict = {}
    for word in word_list:
        score = scrabble_score(word)
        word_entry = {word: score}
        word_dict.update(word_entry)

    return word_dict


def main():
    """ Test out the scrabble dictionary """
    scrabble_dictionary = {}
    word_set_1 = ['Apple', 'banana', 'Grape', 'orange']
    scrabble_dictionary.update(scrabble_dict(word_set_1))

    word_set_2 = ['Orange', 'Pear', 'Grape', 'Pineapple', 'Xerox']
    scrabble_dictionary.update(scrabble_dict(word_set_2))

    print(scrabble_dictionary)


if __name__ == "__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM