-1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Nhập mô tả hình ảnh Dưới đây một chương trình Python [với các khái niệm lớp] để tìm diện tích của tam giác bằng công thức dưới đây.
area = [s*[s-a]*[s-b]*[s-c]] ** 0.5
Chức năng lấy chiều dài của các cạnh của tam giác từ người dùng nên được xác định trong lớp cha và chức năng để tính diện tích nên được xác định trong lớp con.
Trong hình ảnh mã mà tôi đã thử. Nhưng gặp lỗi. xin vui lòng giúp đỡ.``
`
class poly:
def _init_[self,a,b,c]:
self.a = float[a]
self.b = float[b]
self.c = float[c]
a= input["a="]
b= input["b="]
c= input["c="]
class triangle[poly]:
def get_area[self]:
s = [a + b + c] / 2
return [s*[s-a]*[s-b]*[s-c]] ** 0.5
t = triangle[a,b,c]
print["area : {}".format[t.area[]]]
`
Hỏi ngày 11 tháng 4 năm 2020 lúc 6:23Apr 11, 2020 at 6:23
2
class poly:
def __init__[self,a,b,c]:
self.a = float[a]
self.b = float[b]
self.c = float[c]
a= int[input["a="]]
b= int[input["b="]]
c= int[input["c="]]
class triangle[poly]:
def __init__[self,a,b,c]:
super[].__init__[a,b,c]
def get_area[self]:
s = [a + b + c] / 2
return [s*[s-a]*[s-b]*[s-c]] ** 0.5
t = triangle[a,b,c]
print["area : {}".format[t.get_area[]]]
Tôi nhận thấy rất nhiều lỗi. Đầu tiên __init__ có hai dấu gạch dưới [_] chứ không phải một. Trong Python 3 đầu vào trả về một chuỗi để bạn cần typecast vào int trước khi thực hiện các hoạt động số học, nếu không bạn sẽ nhận được typeerror. Thứ ba bạn không gọi phương thức init để tham khảo siêu lớp. Cuối cùng chức năng bạn đã viết là get_area và không phải là khu vực.__init__ has two underscores[ _ ] and not one. In python 3 input returns a string so you need to typecast to int before doing arithmetic operations,else you will get TypeError. Third you did not call init method to reference the superclass. Last the function you wrote is get_area and not area.
Hy vọng vì vậy nó giải quyết vấn đề của bạn. Chào mừng bạn đến với Stackoverflow !!
Đã trả lời ngày 11 tháng 4 năm 2020 lúc 17:34Apr 11, 2020 at 17:34
1
w = 2
h = 4
area = 0.5 * [w * h]
print['the area of a triangle with a width of 2 and a height of 4 is ' + area]
Đó là cách làm cho Python tìm thấy khu vực của một tam giác
Đã trả lời ngày 11 tháng 4 năm 2020 lúc 7:43Apr 11, 2020 at 7:43
Thử cái này
class poly:
def __init__[self, a, b, c]:
self.a = float[a]
self.b = float[b]
self.c = float[c]
self.area = 0
class triangle[poly]:
def __init__[self, a, b, c]:
poly.__init__[self, a, b, c]
def calculate_area[self]:
s = [self.a + self.b + self.c] / 2
self.area = float[[s * [s - self.a] * [s - self.b] * [s - self.c]]] ** 0.5
def get_area[self]:
return self.area
a, b, c = input["a = "], input["b = "], input["c = "]
t = triangle[a, b, c]
t.calculate_area[]
print["area : {}".format[t.get_area[]]]
Đã trả lời ngày 11 tháng 4 năm 2020 lúc 17:36Apr 11, 2020 at 17:36
Deadshotdeadshotdeadshot
8,5374 Huy hiệu vàng17 Huy hiệu bạc37 Huy hiệu đồng4 gold badges17 silver badges37 bronze badges
1
Hãy thử cái này
class geometry:
def __init__[self, a, b, c]:
self.a = float[input["a = "]]
self.b = float[input["a = "]]
self.c = float[input["a = "]]
class triangle[geometry]:
def __init__[self, a, b, c]:
super[].__init__[a, b, c]
def area_triangle[self]:
s = [self.a + self.b + self.c] / 2
return float[[s * [s - self.a] * [s - self.b] * [s - self.c]]] ** 0.5
t = triangle[a, b, c]
print["area : {}".format[t.area_triangle[]]]
David Buck
3.60033 huy hiệu vàng29 Huy hiệu bạc34 Huy hiệu đồng33 gold badges29 silver badges34 bronze badges
Đã trả lời ngày 29 tháng 9 năm 2020 lúc 12:01Sep 29, 2020 at 12:01
class Sides:
def __init__[self,a, b, c]:
self.a = a
self.b = b
self.c = c
class A[Sides]:
def area[self]:
s = [self.a + self.b + self.c]/2
return [s*[s-self.a]*[s-self.b]*[s-self.c]] ** 0.5
t = A[20,30,40]
print["area of Triangle : {}".format[t.area[]]]
Đã trả lời ngày 3 tháng 11 năm 2020 lúc 17:34Nov 3, 2020 at 17:34
class sides:
def __init__[self,a,b,c]:
self.a=a
self.b=b
self.c=c
class triangle[sides]:
def area[self]:
s=[self.a+self.b+self.c]*0.5
return float[[s * [s - self.a] * [s - self.b] * [s - self.c]]] ** 0.5
a, b, c = float[input["a = "]], float[input["b = "]], float[input["c = "]]
t=triangle[a,b,c]
print['area of triangle is ', t.area[]]
Đã trả lời ngày 15 tháng 12 năm 2020 lúc 2:55Dec 15, 2020 at 2:55
1
class poly:
def __init__[self,a,b,c]:
self.a = a
self.b = b
self.c = c
class triangle[poly]:
def get_area[self]:
s = [self.a + self.b + self.c] / 2
return float[[s*[s-self.a]*[s-self.b]*[s-self.c]]] ** 0.5
a, b, c = float[input["a = "]], float[input["b = "]], float[input["c = "]]
t = triangle[a,b,c]
print["area : {}".format[t.get_area[]]]
Đã trả lời ngày 5 tháng 6 năm 2021 lúc 15:30Jun 5, 2021 at 15:30
1