Scrabble_score, "if" condition error whatever i put as parameters


#1

def scrabble_score(word):
    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}
    total= 0
    lower= word.lower()
    for char in range(len(lower)):
        for key in score:
            print lower, lower[char] #debug
            print "char %d     key %d     score %d" %(char, key, score[key] #debug
            if lower[char] == key:
                total += score[key]
                break
    return total
    
scrabble_score("are")

error: File "python", line 13
if lower[char] == key:
^
SyntaxError: invalid syntax

i tried to change both parameters but still return this error. Pls don't give me hints on anything else or i won't learn much :slight_smile:


#2

1.I have deleted all these print statements so program look neat.
Actually they were creating bugs. (You can add these prints and all after you fix the real bugs)

2.Also as you can notice I made the dictionary score variable global. (can you explain why you added it as a local variable ?(inside the function)) ?

@fenix1782
Update**
I just tested both codes.
with break and without break and noticed few things.
break actually makes this code fast (as it helps code to skip unnecessary steps) . I ignored the fact that we're using two for loops and suggested to remove the break(which made it less efficient)

You can visualize both codes here..
http://www.pythontutor.com
put your code there and observe whole execution (quite long 147 steps and 178 steps without break ! :grinning: quite a lot of work there but It will help to understand ins and out of this code) with and without break.
These're some cool observations!
I won't put working code link as it can be copy and pasted by others.


#3

Working 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):
word=word.lower()
count=0
for i in word:
count+=score[i]
return count

scrabble_score("Helix")


#4

I moved the collection declaration within function as last attempt to fixe the error (thought it was relaterd to the collection's key).
I tend to do quite random stuff when i can't get on top of errors.... bad behaviour i know :frowning:
Anyway i removed the debug prints as u did and moved out again the collection declaration and code was marked as OK from checker :cold_sweat:

ore = {"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):
    total= 0
    lower= word.lower()
    for char in range(len(lower)):
        for key in score:
            if lower[char] == key:
                total += score[key]
                break
    print total
    return total
    
scrabble_score("helix")

Now two questions:
1) how the hell prints were screwing the code?
2) the return at the end don't "print" the outcome in the interpreter... i had to add "print total" right before it... why?

P.S.: the "break" was placed there to exit the 2nd loop returning "control" to the 1st one, thus moving "char" to the second character of the function's argument and indeed is working as intended


#5

That's even more simple lol

i'm doped by loops!!


#6

Also don't post your working code as It can be copy - pasted by others(cheating)
just edit it.


#7

Thank you for the link it's very useful!

i will avoid posting working code from now on but... what's the point of copying other's code without understanding it? it is good to see how other solved problems, helps to improve a lot!


#8

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