9. scrabble_score


#1

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):
    scores = 0
    word = raw_input("plese:")
    for char in word:
        scores = scores + score(char)
    return scores
        
scrabble_score(1)

Error:
Traceback (most recent call last):
File "python", line 14, in
File "python", line 11, in scrabble_score
TypeError: 'dict' object is not callable


#2

here:

scores = scores + score(char)

the parentheses make score a function call, to retrieve value from dictionary use square brackets

This will solve your current error, if you get a new one, please take a minute to think about it, if you then need more help, post an updated version of your code + error message + question


#3

I change some things in code.
So issue in that I write code in Python 3.6 in free IDE PyCharm

So, in IDE my code work nice. In codecademy IDE not working.

Code:

Summary

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):

    scores = 0
    word = input("user, pls:")
    for char in word:
        scores += int(score[char])
    return scores

print(scrabble_score(1))
#print(score["a"])

Error in Codecademy IDE:

user, pls: abc
Traceback (most recent call last):
  File "python", line 16, in <module>
  File "python", line 11, in scrabble_score
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined

Result in local IDE:

user, pls:abc
7

Process finished with exit code 0

#4

that is because the exercise will also call the function several times:

print scrabble_score('abc')
print scrabble_score('python')

and so on. The function calls should work correctly, even when nothing is entered into the prompt

If you want to prompt the user, do this outside/after the function and supply the user input as argument

furthermore, this:

print(scrabble_score(1))

is worrying, your function can't handle integers as argument

and finally, codecademy uses python2, in which input() is evaluated as code. use raw_input in python2.


#5

input instead of raw_input

Of course! How did I forget that?

def scrabble_score(word):
    scores = 0
    word = raw_input("user, pls:")
    for char in word:
        scores += int(score[char])
    return scores

Still not working

Oops, try again. Your function fails on scrabble_score("pie"). It returns "0" when it should return "5".

I dont know why.


#6

this is covered in the first section of my answer? The function calls should work correctly, there are not because prompt is inside the function


#7

I was almost "please just tell me answer" but some little more think-time and problem solved. Thank You!


#8

`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):
ttl = 0
for cnt in word:
ttl += int(score[cnt.lower()])
return ttl

print scrabble_score(raw_input("put something"))`


#9

This code is working


#10

I was testing different python versions and I found out that codecademy uses python 2.7
As you see python 2.7 uses print "Hello World!" and it doesn't use brackets (I mean it accepts brackets as well). As well codecademy uses raw_input() while python 3.6 uses: input() and print ("Hello wordl!")


#11

@byteninja32066, actually, this code will not work in python3

@arsenij_arsenij_baki, this are exactly the points @designsolver34940 in this topic? His topic was create to show how the code should look like in python3


#12

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