Why Python don't accept 03 as a number?

jfong at ms4.hinet.net jfong at ms4.hinet.net
Sat Dec 8 21:51:00 EST 2018


Grant Edwards於 2018年12月9日星期日 UTC+8上午12時52分04秒寫道:
> On 2018-12-08, Cameron Simpson <cs at cskk.id.au> wrote:
> > On 07Dec2018 20:24, Jach Fong <jfong at ms4.hinet.net> wrote:
> >>Ian at 2018/12/8 UTC+8 AM11:28:34 wrote:
> >>> What is it exactly that you're trying to accomplish with this? Perhaps
> >>> there's a better way than using eval.
> >>
> >>This problem comes from solving a word puzzle,
> >>    ab + aa + cd == ce
> >>Each character will be translate to a digit and evaluate the correctness,
> >>    03 + 00 + 15 == 18
> >
> > Then you should be evaluating the digits and assembling values from 
> > them. Not trying to shoehorn a string through something that _might_ 
> > accept this string and do what you want. In Python 2 it will accept your 
> > string and not do what you want; at least in Python 3 it doesn't accept 
> > your string.
> 
> Just to be clear: you do _not_ want to use eval on the string.
> 
> If you're not the one who created the string, it might wipe your hard
> drive or empty your bank account.  If you _are_ the one who created
> the string, then generate the desired result instead.
> 
> -- 
> Grant

I didn't evaluate the input string directly. It's the translated "digit" string been evaluated, so shouldn't have any danger on using eval().

--Jach


More information about the Python-list mailing list