[Tutor] Invalid Token Problem

Ross Wilson rzzzwilson at gmail.com
Fri Jul 20 03:01:55 CEST 2012


On 20/07/12 10:45, Dave Angel wrote:
> On 07/19/2012 08:36 PM, Osemeka Osuagwu wrote:
>> <snip>...
>> 99 ninety nine
>> 100 onehundred
>> 101 one hundred and one
>> 102 one hundred and two
>> 103 one hundred and three
>> 104 one hundred and four
>> 105 one hundred and five
>> 106 one hundred and six
>> 107 one hundred and seven
>> 108
>>
>> Traceback (most recent call last):
>>    File "C:/Windows.old/Users/Abasiemeka/Abasiemeka/GOOGLE
>> University/Python/Python Code/MyCode/Project Euler code/Project Euler
>> answer 17.py", line 33, in <module>
>>      print i, num2word(i)
>>    File "C:/Windows.old/Users/Abasiemeka/Abasiemeka/GOOGLE
>> University/Python/Python Code/MyCode/Project Euler code/Project Euler
>> answer 17.py", line 18, in num2word
>>      wordlist = [units[eval(str(num)[-3])-1],'hundred', 'and',
>> num2word(eval(str(num)[-2:]))]
>>    File "<string>", line 1
>>      08
>>       ^
>> SyntaxError: invalid token
>>
>
> 08 isn't a valid literal.  Remove the leading zero.  That says that the
> following digits are to be interpreted as octal, and 8 isn't a valid
> octal digit.

Try to think of another way to convert an integer string into an integer 
value. hINT()

> Much better would be to eliminate the unnecessary use of eval().  It's
> dangerous, and sometimes doesn't do what you expect.

More specifically, eval() is dangerous if you try to evaluate a string 
supplied by someone else.  You really can't predict what will happen.

However, if you use eval() on strings that you create yourself, it can 
be a handy technique.  When you are starting out, it's best to ignore 
eval() until later.

Ross


More information about the Tutor mailing list