Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dumb newbie question
#6
Just to write it in a more modern Python way so you can see how that look,and use the class.
class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def input_sides(self):
        self.sides = [float(input(f"Enter side {i+1}: ")) for i in range(self.n)]

    @property
    def disp_sides(self):
        for i in range(self.n):
            print(f"Side {i+1} is {self.sides[i]}")

class Triangle(Polygon):   
    # Could have set it as a default value in Polygon <no_of_sides=3>  
    def __init__(self):
         super().__init__(3)

    @property
    def find_area(self):
          a, b, c  = self.sides
          s = (a + b + c) / 2
          print(a, b, c)
          area = (s*(s-a) * (s-b) * (s-c)) ** 0.5
          print(f'The area of the triangle is {area:.2f}')
Usage.
>>> obj = Triangle()
>>> obj.n
3
>>> obj.sides
[0, 0, 0]
>>> 
>>> # Overwrite sides
>>> obj.input_sides()
Enter side 1: 5
Enter side 2: 4
Enter side 3: 3
>>> obj.sides
[5.0, 4.0, 3.0]
>>> 
>>> obj.disp_sides
Side 1 is 5.0
Side 2 is 4.0
Side 3 is 3.0
>>> 
>>> obj.find_area
5.0 4.0 3.0
The area of the triangle is 6.00
Reply


Messages In This Thread
Dumb newbie question - by JonEdward - Jul-22-2020, 07:32 PM
RE: Dumb newbie question - by menator01 - Jul-22-2020, 07:45 PM
RE: Dumb newbie question - by mlieqo - Jul-22-2020, 08:03 PM
RE: Dumb newbie question - by Yoriz - Jul-22-2020, 08:03 PM
RE: Dumb newbie question - by JonEdward - Jul-22-2020, 08:15 PM
RE: Dumb newbie question - by snippsat - Jul-22-2020, 10:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 756 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 737 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 668 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 1,029 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,241 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Am I dumb? aidh18 2 1,145 Mar-22-2022, 10:16 PM
Last Post: deanhystad
  Question from complete python's newbie Davicom 3 2,445 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,645 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,531 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  newbie question....importing a created class ridgerunnersjw 5 2,732 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw

Forum Jump:

User Panel Messages

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