I do not understand something. I want to just withdraw the root from a
(code below):
def root (a):
S = ()
For i in Range (int (a), int (-a), 0.0000001):
X = A / I
if x == i: s.append (x)
ELSE: Continue.
Return S.
I get a mistake:
traceback (most recent call last):
File "& lt; pyshell # 12 & gt;", line 1, in & lt; module & gt;
Print (Math.Operation.ROot (2)
File "D: \ Pashka2 \ Programme \ Python \ Math.py", Line 40, In Root
For i in Range (int (a), int (-a), 0.0000001):
Typeerror: 'Float' Object Cannot Be Interpreted AS An Integer
Why does this exception occur, and how can I fix the error?
Thanks in advance!
Answer 1
Range
does not work with fractional parameters, only with integer. Alternatively, you can implement your Range
, which will be able to work with Float
, for example:
def frange (x, y, jump):
While X & LT; y:
Yield X.
X + = Jump
Step = 0.000001.
DEF ROOT (A):
s = []
For i in FRANGE (-A, A, STEP):
X = A / I
IF ABS (X - I) & LT; STEP:
S.APPEND (X)
# For more accurate result, it can be replaced with S.APPEND ((X + I) / 2)
Return S.
Print (Root (4))
Function FRANGE
Take from this answer: Range () for floats @kichik (crossed out sample code at the beginning of the answer)
Conclusion:
[- 2.000000000279556, 1.9999999998691407]
c S.APPEND ((X + I) / 2)
:
[- 2.0, 2.0]
Please note that I replaced the check for accurate equality to approximate
(| X-I | & lt; Step
, i.e. the difference between X and I by module is less than step), otherwise it will be returned to the empty list.