Invalid Token. Not accepting integers that start with 0

1.2k views Asked by At

So I'm trying to make a check digit function, and embarrassingly, I've already run into a snag early on that I've been trying to search the problem for, but most of the explanations I've run into are unfortunately beyond my comprehension. I'm hoping someone could give me a specific run-down as to what my problem is right now

mam = []

def check_digit():
    a = int(input("Please enter your 10 digit book number"))
    b = str(a)
    for c in b:
        mam.append(c)
        print(c)

check_digit()

Sorry about no set names for variables, I prefer coding this way even if it eventually gets awkward for others to read. Well anyway, every time I write in an integer which starts with 0, the Syntax error "Invalid token" appears and I don't know how to solve it. Could anyone help me? I would be grateful

Fixed. I really need to update Python, I'm running 2.7 at the moment.

raw_input solved the issue

4

There are 4 answers

1
colopop On BEST ANSWER

You cast a as an int, but never use it as an int. Try looping through the input string directly.

mam = []

def check_digit():
    a = raw_input("Please enter your 10 digit book number")
    for c in a:
        mam.append(c)
        print(c)

check_digit()

Other answers' explanations about the source of the problem make sense to me. This should fix the problem, since you never need to interpret your input as an integer at all.

4
yurib On

in python, numbers starting with 0 are in octal base, the 'invalid token' error you're getting must be on a digit greater than 7, which is illegal in a base 8 number. Try using raw_input() instead of input() so that the value won't be evaluated as a number.

3
GaryMBloom On

In Python, non-decimal numbers start with '0'. A decimal number cannot start with a '0'. Old-style octal numbers used to start with a '0', so '010' would be 8 in Python 2. Hex numbers started with '0x' and still do in Python 3. In Python 3, octal numbers now start with '0o'. You're throwing the error because you're inputting a string and then running int() on it. Don't do that. You can just check if the inputted string is all digits instead and make sure it's the right length. Or, if you have your heart set on checking for a conversion to int as a decimal, you should strip off the leading zeros with value.lstrip('0') before the conversion.

Alternatively, you can just input the mam and then check if mam.isdigit():... The .isdigit() method checks if the string consists only of numerical digits. This should be what you're looking for.

0
AudioBubble On

This fixed the problem of str versus int with me:

mam = []

def check_digit():
    a = str(input("Please enter your 10 digit book number: "))
    [mam.append(int(inputted)) for inputted in a]
    print(mam)

check_digit()
print(mam[0] + mam[1])
print(type(mam[0]))

input:

Please enter your 10 digit book number: 0123456789

output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1
<class 'int'>