On 12/19/2014 09:05 AM, Lei wrote:
Hi experts,



I want got a py-file named as try.py

The content is

--------------------------------

import sys


def cal(init_code):

     try:

         exec(init_code)

     except:

         print('OMG')

     try:

         if 'A0=' not in init_code:

             print('OMG')

             A0=A

             print('A0=',A0)

     except NameError as e:

         print(e)


init_code = ''

for statement in sys.argv[1:]:

     init_code += statement + '\n'


cal(init_code)

--------------------------------


When I run it via command line in Spyder, IPython console, (I am using
python 3.4)

I got error:

--------------------------------------------------------

In[1]: !python try.py A=10 p=5 n=730

OMG

name 'A' is not defined


--------------------------------------------------------

My idea is that the first try does not work porperly. Because in the
second try, the code "print('OMG')" works. But the code "A0=A" does not
work.

No I am pretty sure it is the first try that is throwing an exception and that is why A is never defined. That is the issue with a naked except, it will catch anything and you really don't know what failed. If you want to find out what is happening do:

except as e:
    print(e)

Note the 'as' this is way exceptions should be handled now. except, e is deprecated. See here:

https://docs.python.org/2/tutorial/errors.html

The bigger question is what are you trying to do?

And why not do it in interest.py, which the above seems to be based on?


Lastly, I would really suggest going through the Python tutorial:

https://docs.python.org/3/tutorial/


Could you please help me with this?

Thanks in advance!



--
Adrian Klaver
[email protected]

--
You received this message because you are subscribed to the Google Groups 
"spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.

Reply via email to