Obviously, the title has code that can’t be interpreted. An uninformative
SyntaxError: invalid syntax
stderr message didn’t tell me much, so I threw it in line by line, using:
[dict.get(['val','val','foo'])]
and have since discovered that dict.get() and iterables don’t play well together.
If you haven’t already guessed, I’m in the exciting field of building toy programs for learning and amusement. Here’s my code:
def wordpoints(word):
points = 0
for char in word:
points += dict.get(char)
return points
Here’s what I want:
def wordpoints(word):
return sum[dict.get(char) for char in word]
For clarification, dict is named tile_p in my code, and stands for tile points. There is another def that looks up the scrabble player’s dictionary for True|False results in a MySQL table, and passes it to this function if it cursor.fectchone() has something in it.
Use parentheses, not brackets, to call a function:
The parser actually told you a bit more than just “invalid syntax”:
See the little
^? It points to the token that cause parsing to fail. Up to this point, all is fine for the parser: It sees a name (sum) followed by the subscription operator[]. Inside the brackets, it expects an arbitrary expression. Up todict.get(char), this is a valid expression, but the following tokenfordoes not make sense to the parser — hence the syntax error at this point.