Python Forum
Object oriented area of a triangle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object oriented area of a triangle
#1
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
Error:
Enter the value of the first side:4 Enter the value of the second side:5 Enter the value of the third side:6 Traceback (most recent call last): File "area2.py", line 17, in <module> print (area((a,b,c))) NameError: name 'area' is not defined
Reply
#2
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.
Reply
#3
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:
Error:
print(t.area()) eError: area() missing 2 required positional arguments: 'b' and 'c'
__________________-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
Output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\TOPEV\Documents\School\Programming Language>python area2.py Please enter the first side of a triangle: 2 Please enter the second side of a triangle: 3 Please enter the third side of a triangle: 3 2.8284271247461903
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with writing an objective oriented python assignment mikikiki 5 2,198 Feb-21-2023, 01:18 PM
Last Post: jefsummers
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 4,008 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,502 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 2,063 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,181 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Triangle function program m8jorp8yne 2 9,046 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 5,113 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,753 May-19-2019, 06:04 PM
Last Post: heiner55
  Help for my assignment - Object Oriented Programming denizkb 5 5,292 Jan-05-2019, 06:43 PM
Last Post: stullis
  Draw isosceles triangle fen1c5 4 13,177 Jul-07-2018, 10:20 AM
Last Post: fen1c5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020