[Tutor] Need to convert 1,987,087,234,456 to an int
Alan Gauld
alan.gauld at btinternet.com
Thu Aug 16 10:07:08 CEST 2007
"Dick Moores" <rdm at rcblue.com> wrote
> I can't figure out how to do it for
> n = 1,987,087,234,456 # no quotes
> (I want to be able to type in big ints using commas--much easier to
> see that I've typed correctly)
I don't think you can. You are basically asking
"how can I break the syntax rules?"
http://docs.python.org/ref/integers.html
> >>>type(1,987,087,234,456)
> File "<input>", line 1
> type(1,987,087,234,456)
> ^
> SyntaxError: invalid token
> (My wild guess is that 087 is octal.)
Yes, see the reference above. An octal number is defined as starting
with zero and followed by valid octal digits. That's why your scheme
to use commas can't ever work - you are trying to change the language.
Its a bit like deciding that you don;t like commas as list seperators
and would prefer to use something else:
[ 1 ¬ 2 ¬ 3 ¬ 4]
Unfortunately that's not valid Python anmd it won't allow it.
Same with invalid octal numbers...
Sorry,
Alan G.
More information about the Tutor
mailing list