2 def ta_h(x): ----> 3 return math.ta_h(x)
时间: 2023-12-15 07:03:44 浏览: 28
`math` 模块中并没有 `ta_h` 这个函数,所以你需要自己实现这个函数或者使用别的库来实现。如果你想要实现一个双曲正切函数,可以使用 `math.tanh()` 函数,它可以计算一个数的双曲正切值。你的代码可以这样修改:
```python
import math
def ta_h(x):
return math.tanh(x)
```
这样就可以使用 `ta_h()` 函数来计算一个数的双曲正切值了。
相关问题
import math class QE: __a = 0 __b = 0 __c = 0 def __init__(self,a,b,c): QE.__a = a QE.__b = b QE.__c = c def geta(self): return QE.__a def getb(self): return QE.__b def getc(self): return QE.__c def getD(self): return QE.__b^2-4*QE.__a*QE.__c def getRoot1(self): M = QE.getD() if M < 0: print("该方程无根!") elif M == 0: print("x1 = x2 = {:.2f}".format( (-QE.__b - math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a )) else: print("x1 = {:.2f}".format( (-QE.__b - math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a )) QE.getRoot2() def getRoot2(slef): print("x2 = {:.2f}".format((-QE.__b + math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a)) a = int(input("请输入a值")) b = int(input("请输入b值")) c = int(input("请输入c值")) B = QE(a,b,c) B.getRoot1()
出现这个错误是因为你在调用 `QE.getD()` 方法时,没有将实例对象作为参数传递给该方法。因为 `getD()` 方法是实例方法,需要使用 `self` 来引用实例对象的属性。你可以将 `QE.getD()` 改为 `self.getD()`,这样就能正确地引用实例对象了。此外,你在 `getRoot2()` 方法中拼写错误,应该是 `self` 而不是 `slef`。下面是修改后的代码:
import math
class QE:
__a = 0
__b = 0
__c = 0
def __init__(self,a,b,c):
QE.__a = a
QE.__b = b
QE.__c = c
def geta(self):
return QE.__a
def getb(self):
return QE.__b
def getc(self):
return QE.__c
def getD(self):
return QE.__b**2-4*QE.__a*QE.__c
def getRoot1(self):
M = self.getD()
if M < 0:
print("该方程无根!")
elif M == 0:
print("x1 = x2 = {:.2f}".format((-QE.__b - math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
else:
print("x1 = {:.2f}".format((-QE.__b - math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
self.getRoot2()
def getRoot2(self):
print("x2 = {:.2f}".format((-QE.__b + math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
a = int(input("请输入a值"))
b = int(input("请输入b值"))
c = int(input("请输入c值"))
B = QE(a,b,c)
B.getRoot1()
class Point: def __init__(self, x, y): self.__x = x self.__y = y def __add__(self, other): x = self.__x + other.__x y = self.__y + other.__y return Point(x, y) def __sub__(self, other): x = self.__x - other.__x y = self.__y - other.__y return Point(x, y) def __mul__(self, a): x = self.__x * a y = self.__y * a return Point(x, y) def __truediv__(self, a): x = self.__x / a y = self.__y / a return Point(x, y) r1 = Point(10, 20) r2 = Point(5, 5) print(r1+r2) print(r1-r2) print(r1*2) print(r1/2)代码改进,输出向量坐标
为了输出向量坐标,我们可以在 Point 类中添加一个方法来返回坐标的元组,如下所示:
```python
class Point:
def __init__(self, x, y):
self.__x = x
self.__y = y
def __add__(self, other):
x = self.__x + other.__x
y = self.__y + other.__y
return Point(x, y)
def __sub__(self, other):
x = self.__x - other.__x
y = self.__y - other.__y
return Point(x, y)
def __mul__(self, a):
x = self.__x * a
y = self.__y * a
return Point(x, y)
def __truediv__(self, a):
x = self.__x / a
y = self.__y / a
return Point(x, y)
def get_coordinates(self):
return (self.__x, self.__y)
```
然后我们就可以使用这个方法来输出向量坐标了,示例如下:
```python
r1 = Point(10, 20)
r2 = Point(5, 5)
print(r1+r2.get_coordinates()) # 输出 (15, 25)
print(r1-r2.get_coordinates()) # 输出 (5, 15)
print(r1*2).get_coordinates()) # 输出 (20, 40)
print(r1/2).get_coordinates()) # 输出 (5.0, 10.0)
```