Thanks a lot for your reply!
I tried your suggestions, but it did not work on my computer.
1. I think the except following the first try are not performed, since I
added a code in the except block, it was not shown in the output.
2. The "exec(init_code)" produced a "None" value, since I modified this
code to
-------------------------------------
print(exec(init_code))
-------------------------------------
In the output, the word "None" showed up. So I think, the reason the whole
try.py file not working is this code "exec(init_code)" just produces None.
3. For the except code following the first try, I changed to
--------------------------------------
except as e
--------------------------------------
but, my computer said it is invalid syntax.
4. The interest.py file contains much more things that are not directly
related to this question, so I think it would be clearer to make a
concentrated file.
5. The Python tutorial is wonderful, but it is too general. Not specific
for my question. I have already tried to find my answers in this tutorial,
but I could not find it.
On Saturday, December 20, 2014 12:55:50 AM UTC+1, Adrian Klaver wrote:
>
> 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] <javascript:>
>
--
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.