0

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

  • 1
    `b` would not be defined. It's local to the `check_digit()` function – Mangohero1 Dec 04 '17 at 17:24
  • Maybe your indentation is wrong? The for loop should belong to the check_digit function. – Daniel Dec 04 '17 at 17:24
  • Sorry, that was a mistake when I copied it into stack overflow. It's fixed now. – Pearse Traynor Dec 04 '17 at 17:25
  • Can you post the whole error traceback? So we can see in which line the error happens. – Mike Scotty Dec 04 '17 at 17:28
  • Code however you want, but if you're going to ask others to read your code and help you, it would be courteous to use more descriptive variable names. In this case it doesn't matter so much, but you'll be happier in the long run if you break the habit now. – colopop Dec 04 '17 at 17:28
  • I don't get an error: if I type in `0123456789` as input, it returns this : `123456789` –  Dec 04 '17 at 17:30
  • Note that when you cast a string as an int, leading zeroes are dropped. (e.g. `str(int("001"))` returns `"1"`.) Could be relevant. – colopop Dec 04 '17 at 17:31

4 Answers4

0

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.

yurib
  • 8,043
  • 3
  • 30
  • 55
0

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.

colopop
  • 441
  • 3
  • 13
  • @PearseTraynor my mistake, in python2.7 you should use `raw_input`. This will cause the interpreter to take input as a string in all cases, as opposed to `input`, which tries to deduce the input type. I've edited the answer. – colopop Dec 04 '17 at 17:38
0

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.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
0

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'>