Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a eval statement to run the numbers as floats

I have created a GUI calculator (a screen shot included) and when a button is pressed it adds text to the textctrl so that the equation is displayed to the user. when the user presses enter it takes that text and runs it usesing eval then prints using SetValue but if i run a problem in the texctrl like 5/6 it comes out as 0 how do i make that a float

def eenter(self,e):
    a=self.box.GetValue()
    answer=eval(a)
    ans=str(answer)
    self.box.SetValue(ans)
like image 239
user1108980 Avatar asked Feb 22 '23 23:02

user1108980


1 Answers

Place at the top of your file:

from __future__ import division

This redefines the meaning of / so it is always floating point division. (Integer division is //.)

For more information on what this means, see PEP 238.

like image 66
Greg Hewgill Avatar answered Mar 30 '23 00:03

Greg Hewgill