How to get an eval statement to run numbers as a float

I created a graphing calculator (screen shot is on), and when the button is pressed, it adds text to textctrl so that the equation is displayed to the user. when the user presses the enter button, he takes this text and starts it using eval and then prints using SetValue, but if I run the problem in texctrl, for example, 5/6, it turns out as 0, how can I make a float

def eenter(self,e): a=self.box.GetValue() answer=eval(a) ans=str(answer) self.box.SetValue(ans) 
+4
source share
3 answers

Just add the following line to the top of your program:

from __future__ import division

This will cause the divs to behave in Python 2.x, as in Python 3.x: with automatic casting for float, if integer operators result in a decimal number.

+2
source

Place at the top of the file:

 from __future__ import division 

This overrides the value of / , so it is always a floating point division. (Integer division // .)

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

+5
source

You can try changing your input:

 5/6.0 

The above converts the result to a float type

+1
source

Source: https://habr.com/ru/post/1389192/


All Articles