#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Python syntax error ...not sure why?


    For input 1,000,000 in python shell 3.3.2 i got (1, 0, 0) as an output and i got the point that python interprets 1,000,000 as comma-separated sequence of integers.

    But for 6,09 i got syntax error. Why it is so?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    I'm not completely sure what your asking but for input() python takes the input as a string.(Atleas for me it does).
    x = input()
    1,000,000
    type(x)
    <class 'str'>

    y = input()
    6,09
    type(y)
    <class 'str')

    If you do need it to be integers you could split the numbers by using s.split(',') and then use int to convert to integer. Sorry if I'm not being helpful or gave bad advice I'm still pretty new to python.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    As Good News noted, input() returns a string. If you need something other than a string the only reliable way to make it so is to deliberately cast it yourself. Python is dynamically typed which means in many cases is has to guess what something means, and this isn't always reliable.

    To get an answer we need to see how you are using the retrieved value. You need to show us the context in which you are using input() for us to see what is causing the error.

    Most likely Python is trying to convert whatever the value is into a tuple of three integers for whatever reason, and so 1,000,000 is the valid input for the value (1, 0, 0) but 6,09 is not valid because it results in either (6, 9, None) or (None, 6, 9), but it can't tell which. The "for whatever reason" part is what is a mystery because you didn't show us how the value is being used.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Integers with leading 0 are interpreted in octal. 9 is an illegal octal digit.
    Code:
    >>> 09
      File "<stdin>", line 1
        09
         ^
    SyntaxError: invalid token
    >>> 07
    7
    >>>
    Executable Iverson notation, j, has a rich syntax for numeric constants.
    Code:
       2p1bz NB. z in base Tau   http://laughingsquid.com/pi-is-wrong/
    35
    35 times 2pi to the power of 0.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by b49P23TIvg
    Integers with leading 0 are interpreted in octal. 9 is an illegal octal digit.
    Code:
    >>> 09
      File "<stdin>", line 1
        09
         ^
    SyntaxError: invalid token
    >>> 07
    7
    >>>
    This is correct, in Python 2, but OP states Python 3.
    Code:
    >>> 09
      File "<stdin>", line 1
        09
         ^
    SyntaxError: invalid token
    >>> 07
      File "<stdin>", line 1
        07
         ^
    SyntaxError: invalid token
    This form for octal literals was removed in Python 3. To avoid confusion, leading zeroes were prohibited entirely rather than their meaning simply changing.

IMN logo majestic logo threadwatch logo seochat tools logo