Question

Dag Hansteen d-hanst at online.no
Wed Aug 11 15:59:43 EDT 2004


I make a GUI calculator in wxPython.
How can I do the following without having to use eval?:

    def OnCalc(self, event):
        try:
            regnestykke = self.formel.GetValue()
            svar = eval(regnestykke)
            svar = str(svar)
            self.answer.SetValue(svar)
        except:
            self.answer.SetValue("Error! Check Syntax!")

"""
self.formel is defined as wxTextCtrl for the input from user to be calculated 
self.answer is defined as wxTextCtrl for the answer of the users input
"""




"Thomas Krüger" <thomas.krueger at gmx.net> skrev i melding news:cf8ehd$kuk$03$1 at news.t-online.com...
> Dag Hansteen wrote:
> 
> >>>> s = "2+2"
> >>>> answer = int(str(s))
> > Traceback (most recent call last):
> >   File "<pyshell#64>", line 1, in ?
> >     answer = int(str(s))
> > ValueError: invalid literal for int(): 2+2
> > 
> > How do I this ? I have a string with a such expression and I want to
> > return the answer in a variable.
> 
> eval('2+2')
> -> 4
> 
> Thomas
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040811/2ffc6ad5/attachment.html>


More information about the Python-list mailing list