Question for Scrabble_score


#1

Hello there,
I had a problem with this scrabble_score coding.
I found i was over complicating it when it was just simple code that worked in the end
Let me show you what i was trying to do.


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):
    word = word.lower()
    letters = []
    total = 0
    for i in word:
        letters.append(i)
        print letters 
       #This **print statement** was to show it was getting added to my list, which it was
        for item in letters:
            if item == score: 
                total += score[item]
            else:
                total = total
    return total

print scrabble_score("scrap")

I kept getting a none answer in the console from total, so obviously it wasn't working.
What I was wanting to do was add the word into a new list, with each letter its own index, that worked fine.
Then i went about trying to compare the lists indices to the score indices, if they were a match, add the score index value to total, if not, carry on.
This however was not working, erither i had not got the logic right, or i was trying to do something that couldn't be done.
(I am always overcomplicating the code, trying new things before i know how to actually do it, as you can well see)
Can you let me know if this could have worked? or if i was creating a huge problem lol

The code that did work for me was this:

def scrabble_score(word):
    word = word.lower()
    total = 0
    for i in word:
        total += score[i]
    return total
    
print scrabble_score("scrap")

This surprised me at how simple it was, and also annoyed me too, why is it this simple,
why does the score index values get added to total when the given word itself has not been split into indices.
So this is telling me that any given word gets run through however many times for each of its letters and they get evaluated?
I think i am confusing myself by thinking into it too much.


#2

once again you got nested loops:

for i in word:
    for item in letters:

This will make for many loop iterations. it would make sense to place the second for loop after the first:

for i in word:
        letters.append(i)
        print letters 
for item in letters:

this way, first the for loop will run, and then the second for loop will run after the first loop has finished running [quote="monkeyman1188, post:1, topic:77440"] Then i went about trying to compare the lists indices to the score indices, if they were a match, add the score index value to total, if not, carry on. [/quote] here is a flaw in your logic, lets look at the condition:

if item == score:

`item` contains a letter of which you want to get the score, so far so good. But `score` is a dictionary. How can a single letter equal a whole dictionary? If you where to leave this `if` condition out, you would be good. (assuming you fixed your loops) Its mentioned in the exercise, you only get valid input, so you don't have to validate the letter is present in dictionary. If you do want to, use `in`, not equal (`==`) `else` doesn't do much? You could just leave it out [quote="monkeyman1188, post:1, topic:77440"] why does the score index values get added to total when the given word itself has not been split into indices. [/quote] we can just loop over a string to get characters from a string, so we don't need to split it, look:

for letter in "test":
    print letter

see?


#3

Thank you for taking your time to read and reply,
I seemed to make a huff of the code and yet you describe how it should be so simple,
very much appreciated.
Rob


#4

guess how i manage to do this? Practice, practice and more practice. Like they say, practice makes perfect. Install python on your computer, build stuff, learn about error messages the python interpreter gives you.

You will get there in the end, when i did this section (practice makes perfect) the first time, i was really struggling to understand any of it :stuck_out_tongue:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.