I tried to create a code that calls a class. But I get
TypeError: EulerMethod() takes no arguments
The main code that calls the class is;
import numpy as np
import HW_3_euler as eum
x = input('define independent variable:\t')
y = input('define dependent variable:\t')
h = float(input('define the iteration value:\t'))
range_ = np.asarray(np.linspace(float(input('define the dependent variable range:\t')),float(input()),6))
equation_str = input('define the equation:\t')
locals()[x] = float(input('define x_0 value:\t'))
locals()[y] = float(input('define y value at x=0:\t'))
result_ = eum.EulerMethod(x, y, h, equation_str, range_)
print(result_)
and the class is;
class EulerMethod:
"""Euler Method approach"""
#dervf = 3 * x**2 * y - 5.4 * x * y 0.15 * y**2
def __init__(self, chi, ups, dlt, equation_str, range_):
self.chi = chi
self.ups = ups
self.dlt = dlt
self.range_ = range_
self.equation_str = equation_str
def func(self):
i = 0
results_ = self.range_
results_[0] = self.ups
for i in (range(len(self.range_))-1):
results_[i 1] = self.ups self.dlt * eval(self.equation_str)
self.chi = self.range_[i 1]
self.ups = results_[i 1]
return EulerMethod()
the purpose of to create this code is to find initial value problem by Euler Method. the main code sends the parameter 'x', 'y', 'delta x (declared as 'h')', equation in string and 'x' values in array. I predict that class return 'y' values in array format.
CodePudding user response:
may not be the problem, but the code as it is pasted has an indentation error that would explain the Exception
.
it should be:
class EulerMethod:
"""Euler Method approach"""
#dervf = 3 * x**2 * y - 5.4 * x * y 0.15 * y**2
def __init__(self, chi, ups, dlt, equation_str, range_):
self.chi = chi
self.ups = ups
self.dlt = dlt
self.range_ = range_
self.equation_str = equation_str
def func(self):
i = 0
results_ = self.range_
results_[0] = self.ups
for i in (range(len(self.range_))-1):
results_[i 1] = self.ups self.dlt * eval(self.equation_str)
self.chi = self.range_[i 1]
self.ups = results_[i 1]
return EulerMethod()
CodePudding user response:
TypeError: EulerMethod() takes no arguments
means the class doesn't have any def __int
inside it. Looking at your code, I can see that your def __init__
's indent is wrong. Since your indent is wrong, the whole class can't take any argument.
Change to this should fix your issue.
class EulerMethod:
"""Euler Method approach"""
#dervf = 3 * x**2 * y - 5.4 * x * y 0.15 * y**2
def __init__(self, chi, ups, dlt, equation_str, range_):
self.chi = chi
self.ups = ups
self.dlt = dlt
self.range_ = range_
self.equation_str = equation_str
def func(self):
i = 0
results_ = self.range_
results_[0] = self.ups
for i in (range(len(self.range_))-1):
results_[i 1] = self.ups self.dlt * eval(self.equation_str)
self.chi = self.range_[i 1]
self.ups = results_[i 1]
return EulerMethod()