Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError invalid token

I have a problem when I try to assign a value to a variable. The problem shows up when I try to put a date as a tuple or a list in this order: year, month, day.

>>> a = (2016,04,03)         # I try to put the date into variable 'a' as a tuple. SyntaxError: invalid token >>> a = [2016,04,03]         # I try to put the date into variable 'a' as a list. SyntaxError: invalid token 
  1. Why is this happing?

  2. How do I fix it?

  3. What does token mean in Python?

like image 822
zerocool Avatar asked Apr 03 '16 14:04

zerocool


People also ask

How do I fix an invalid token?

The “Invalid Token” message indicates that a link has either been used previously, or has expired. To generate a new link, reset your password again through the main login screen. If you continue to have trouble, ensure you are referencing the most current Password Reset link.

What is invalid token Python?

In python version 2.7, we gets error when we use 0 before any number and that number is invalid in octal number system. For e.g. if we use 08 or 09 then we will encounter the same error 'invalid token'. Python interpreter divides the entire script into various parts and those parts are called tokens.

How do I fix SyntaxError invalid syntax?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What causes invalid token error?

If you're trying to reset your password and you receive an error citing an “invalid token” or asking you for your token, it's likely that the link you clicked on to reset your password has expired. For security reasons, passwords are never sent out across the Internet.


1 Answers

In Python 3, leading zeros are not allowed on numbers. E.g:

05 0123 

Etc. are not allowed, but should be written as 5 and 123 instead.

In Python 2, however, the leading zero signifies that the number is an octal number (base eight), so 04 or 03 would mean 4 and 3 in octal, respectively, but 08 would be invalid as it is not a valid octal number.

In Python 3, the syntax for octals changed to this:

0o10 0o4 

(As well as allowing other bases such as binary and hexadecimal using the 0b or 0x prefixes.)

As for your other question, a token in Python is the way the Python interpreter splits up your code into chunks, so that it can understand it (see here). Here, when the tokenizer tries to split up your code it doesn't expect to see the zero there and so throws an error.

I would suggest (similarly to the other answers) that you drop the leading zero ((2016,4,3)) or represent these using strings (("2016","04","03")).

like image 121
Tuomas Laakkonen Avatar answered Sep 24 '22 13:09

Tuomas Laakkonen