evaluation question
Louis Krupp
lkrupp at invalid.pssw.com.invalid
Sat Jan 28 07:18:50 EST 2023
On 1/27/2023 9:37 AM, Muttley at dastardlyhq.com wrote:
> Hi
>
> This is probably a dumb newbie question but I've just started to learn
> python3 and eval() isn't behaving as I'd expect in that it works for
> some things and not others. eg:
>
>>>> eval("1+1")
> 2
>>>> eval("print(123)")
> 123
>>>> eval("for i in range(1,10): i")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<string>", line 1
> for i in range(1,10): i
> ^
> SyntaxError: invalid syntax
>
> Why did the 3rd one fail? Does it not handle complex expressions?
>
> Thanks for any help
>
>
>
This might -- or might not -- be useful:
eval( "print( [i for i in range(1, 10)] )" )
It prints a list, but you probably knew that:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Louis
More information about the Python-list
mailing list