Syntax error during job execution

For some reason, when I run

import sys
from fractions import Fraction
for i, j in zip(["a","b","c","d","e","f"], range(1,6)):
    eval("{0} = int(sys.argv[{1}])".format(i, j))
if a*d != c*b:
    x = (e*d-b*f)/(a*d-c*b) 
    y = (a*f-c*e)/(a*d-c*b)
    print "x = ", x , ", y = ", y
elif e*d-b*f == 0 and a*f-e*c == 0:
    print "Infinite solutions"
    print "Slope = ", Fraction(-a,b), ", Y-Intercept = ", Fraction(e,b)
else:
    print "No solution"

using python2 py.py 1 3 3 9 5 15, it gives me the following error

Traceback (most recent call last):
  File "py.py", line 4, in <module>
    eval("{0} = int(sys.argv[{1}])".format(i, j))
  File "<string>", line 1
    a = int(sys.argv[1])
      ^
SyntaxError: invalid syntax

Any ideas as to why this is happening? I'm sure this is the correct syntax, but maybe eval messed it up?

+4
source share
2 answers

This is useless use eval. Your code:

for i, j in zip(["a","b","c","d","e","f"], range(1,6)):
    eval("{0} = int(sys.argv[{1}])".format(i, j))

may be better expressed as:

a, b, c, d, e, f = map(int, sys.argv[1:7])

or how:

a, b, c, d, e, f = (int(x) for x in sys.argv[1:7])

[Note that the source code had the wrong range, it should be in the range (1,7)]

+2
source

( ), ( ). . Ned answer .

, :

https://docs.python.org/2/library/functions.html?highlight=eval#eval:

Python ( , ), locals .

: " Python". - , . exec .

+2

All Articles