Long and short dashes

14 Apr, 2020

Jan vK writes:

here's an example from your book that gives an error:
count_down_by_twos = list(range(40, 10, −2))
SyntaxError: invalid character in identifier
please inform me how to solve this problem

It looks like you might have copied-and-pasted the code? Perhaps from the digital version of the book? It looks like the -2 in your example is actually a hyphen (i.e. a long dash −) instead of a minus (i.e. a short dash -). So if I try the version of the code you sent, I get the same error:

>>> count_down_by_twos = list(range(40, 10, −2))
  File "<stdin>", line 1
    count_down_by_twos = list(range(40, 10, −2))
                                             ^
SyntaxError: invalid character in identifier

However, if I try with the correct character, there's no error:

>>> count_down_by_twos = list(range(40, 10, -2))
>>> 

The next question you might ask is what does "invalid character in identifier" actually mean? An identifier is the name of something (the name of a keyword, a variable, a function or a class, and so on) -- valid identifiers are a sequence of letters (characters), digits and underscores. In effect you're getting that error message because python doesn't recognise "−2" (a long dash followed by 2) as any recognisable keyword, or variable, or anything resembling a valid identifier.

Hope that helps.