Scrabble score. I'm in dead end


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096#


Guys I'm done lol. Would anyone push me in right direction here ?


This is what I've got so far ...


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 = raw_input("Give me a word: ")
    if len.word < 1:
        print "You didn't type anything"
    else:
        for i in word:
            string.lower(i)
            for key in score:
                if i == key:


#2

Here's my code:

def scrabble_score(word):
    scrabbleScore = 0
    aList = []
    for letter in word:
        aList.append(letter.lower())
    for item in aList:
        scrabbleScore += score[item]
    return scrabbleScore

You don't need to have a raw_input function because the method itself already takes in the word.
Since the method needs to return a score for the word, you need to set up a scrabbleScore (set it to 0 for now).
Next, I used a list to store my data so it can be easily accessible
I set up a for loop to loop my data through the word and store it in the list
Then, for each item in the list, I add its value to scrabbleScore. By using the way of accessing dictionary values through their keys, I did scrabbleScore += score[item] which would add the score of each individual item to the scrabbleScore

At the end, I returned the scrabbleScore


#3

I was being dumb and I took the long route, so I just found a shortcut:

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

Same concept as before, except that we don't actually need to use a list at all--a few extra dumb steps.
Now, for every letter in the word, we can add its score (through accessing its value using score[letter]) to scrabbleScore, and then we return the value at the end. My bad.

PS I had to do letter.lower() because it wanted me to make sure uppercase and lowercase letters came through as well


#4

I chatted with advisor and he said my original code could've worked if there wasn't for the second for loop.


#5

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}
         
word = raw_input("Enter a word with no special chars or spaces")                    
def scrabble_score(word):
    points=0
    word = word.lower()
    for c in word:
        points += score[c]
    return points

print scrabble_score(word)

#6

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().replace(' ','')
total = 0
for elem in word:
total += score[elem]
return total
print scrabble_score('pie hh L')

and as a result:
Oops, try again. Your function crashed on pie as input because your function throws a "'int' object is not callable" error.

in what place I go wrong?


#7

There is no issue with your code, maybe you have just missed the indentation somewhere.


#8

No, the problem is not in the indentation


#9

Somewhere you have used total as a function but it is an int, hence the error. I think you have corrected it now. Please refresh the page and run your code again


#10

Thank you. You were right.


#11

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