![]() |
Object oriented area of a triangle - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Object oriented area of a triangle (/thread-9638.html) |
Object oriented area of a triangle - xterakojede - Apr-20-2018 Please i need help, this code does not compute the area # An object-oriented programming style to calculate area of triangle (1/2 base times height) import sys import math a = input("Enter the value of the first side:") b = input("Enter the value of the second side:") c = input("Enter the value of the third side:") class triangle(): def __init__(self,a,b,c): self.a = a self.b = b self.c = c def area(a, b, c): s=(a + b + c)/2 area=math.sqrt(s*(s-a)*(s-b)*(s-c)) return area print (area((a,b,c))) ------this is the output
RE: Object oriented area of a triangle - stranac - Apr-20-2018 Your code is trying to use a global function area() , which doesn't exist.area() is a method of a triangle object, it should take no arguments (except self ), and should be used like so:t = triangle(a, b, c) print(t.area())You'll also need to convert your inputs to numbers (using int() or float() ), since input() returns strings on python 3.
RE: Object oriented area of a triangle - xterakojede - Apr-20-2018 Please can u explain further, at what lines should i make the corrections. i have done this: import sys import math a = 5 b = 6 c = 7 class triangle(): def __init__(self,a,b,c): self.a = a self.b = b self.c = c def area(a, b, c): s=(a + b + c)/2 area=math.sqrt(s*(s-a)*(s-b)*(s-c)) return area t = triangle(a, b, c) print(t.area())but it displays this error: __________________-This code finally worked, thanks for helping me learn the hard way.import sys import math a = int(input('Please enter the first side of a triangle: ')) b = int(input('Please enter the second side of a triangle: ')) c = int(input('Please enter the third side of a triangle: ')) class triangle(): def __init__(self,a,b,c): self.a = a self.b = b self.c = c def area(self): s=(a + b + c)/2 area=math.sqrt(s*(s-a)*(s-b)*(s-c)) return area t = triangle(a, b, c) print(t.area())_____Output
|