Invalid Token Error during Python Dictionaries Project

I was working on the Python Dictionaries project when I came across the following error:

  File "script.py", line 35
    medical_records["Valentina"] = {"Age": 52, "Sex": "Female", "BMI": 18.7, "Children": 01, "Smoker": "Non-smoker", "Insurance_cost": 6420.0}
                                                                                          ^
SyntaxError: invalid token

I went to that line, and noticed I forgotten a “,” so I corrected that but the error persisted. The only other thing out of place was I accidentally did 01 for children instead of 1. I changed it, not thinking that was the error, but that fixed the problem! Huzzah! However, I have no idea why that provoked the SyntaxError. Is 01 not an acceptable integer? I looked around the forums and the closest I got was an answer by mtk, but it was related to DateTime and it didn’t shed any light on why it doesn’t work with 01, but works with 1.

Thank you!

Hi,

You can test some of this behavior in a terminal:

>>> 1
1
>>> print(1)
1
>>> print(01)
  File "<stdin>", line 1
    print(01)

           ^
SyntaxError: invalid token
>>> int("01")
1
>>> test = {"Test": "01" }
>>> test
{'Test': '01'}
>>> test = {"Test": int("01") }
>>> test
{'Test': 1}

I believe it’s because starting with 0 is reserved for octal numbers but that’s hw for another day.

1 Like

Indeed:

>>> 0o11
9
>>> 0o12
10
>>> 0o13
11
>>> 0o01
1
>>> 0o10
8
1 Like

Thank you! That lead me down the right path. I see it is something reserved (Octal numbers, as you said). Good thing to know! I have used 000x for numbers before to preserve order when ordering in other programs, but I will not be able to use that in Python code.

You can! Just make sure it’s a string when you use it for that type of classification. Or have a function that processes it into a string.

That was my first impulse as well. :slight_smile: I might actually be learning stuff! This is great.