Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have just started learning python and I am a bit confused. Why does this code not work? When I call pie, it returns 1 instead of 5. What am I not understanding here?

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):
     total = 0
     word = word.lower()
     for letter in word:
         total =+ score[letter]
     else:
         return total
share|improve this question

put on hold as off-topic by iCodez, vaultah, tobias_k, jh314, Kevin 19 hours ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – iCodez, vaultah, tobias_k, jh314, Kevin
If this question can be reworded to fit the rules in the help center, please edit the question.

11  
It should be += instead. –  vaultah 19 hours ago
2  
you can do this nicely as sum([score[letter] for letter in word.lower()]) –  Joran Beasley 19 hours ago
1  
I don't understand why there's an else clause without an if. Unindent the return statement and eliminate the else: –  mauve 19 hours ago
1  
If @vaultah's comment doesn't explain it: =+ means nothing, so total =+ score[letter] means total = (+ score[letter]). So, each time through the loop, you replace total with + score[letter]. The last letter scores 1, so you replace total with +1, which is 1. –  abarnert 19 hours ago
2  
Actually I am quite wrong per documentation. It's used for when the list is exhausted. –  KronoS 19 hours ago

1 Answer 1

up vote 1 down vote accepted

You have got the operator incorrectly. It should be +=, not =+.

share|improve this answer
1  
Why the downvote? This is the correct answer. –  KronoS 19 hours ago
1  
@KronoS This question is about a simple mistake. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. –  vaultah 19 hours ago
1  
@vaultah so close the question. don't penalize the answerer. –  KronoS 19 hours ago
2  
It's been useful to me. I learned about the "else" condition in a "for" loop. It's not very searchable, but it's a good exchange of information. –  mcduffee 19 hours ago
1  
@KronoS I regret the question was closed too late. That's just my notion: questions like this should not be answered if the issue was successfully resolved in comments. The poster has already got +18, what "penalizing" are you talking about? I didn't downvote the question, but I did downvote this answer. I make no apology for that. –  vaultah 19 hours ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.