Python Forum
How do classes work? (rectangle)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do classes work? (rectangle)
#1
Hey just wanted to ask if you maybe know how to convert my current code that is below into one that uses classes im kinda confused about it.
it works with pass but im pretty sure its not the way it should be done ive tried with def __init__ but ive lost my self in it.
Im also a bit new to python.

import math


class Rectangle:
    pass
length = float(input('Please Enter the Length of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
# calculate the area
Area = length * height
# calculate the Perimeter
Perimeter = 2 * (length + height)
#calculate diagonal of rectangle
Diagonal = (length**2) + (height**2)
Diagonal = math.sqrt(Diagonal)
print(" Diagonal of rectangle: %.4f" %Diagonal)
print(" Length of rectangle: %.f" %length)
print(" Area of a Rectangle is: %.f" %Area)
print(" Perimeter of Rectangle is: %.f" %Perimeter)
#points calculation:
Point1x = float(input("Enter X coordinates of your first point/corner: "))
Point1y = float(input("Enter Y coordinates of your first point/corner: "))
Point4x = Point1x + length
Point4y = Point1y - height
Point2x = Point4x - length
Point2y = Point1y - height
Point3x = Point1x + length
Point3y = Point4y + height
Centerx = length / 2 
Centery = height / 2
print ('Top Left corner point coordinates : %.2f ' %Point1x,", %.2f"%Point1y)
print ('Top Right corner point coordinates : %.2f ' %Point2x,", %.2f"%Point2y)
print ('Botoom Left corner point coordinates : %.2f ' %Point3x,", %.2f"%Point3y)
print ('Botoom Right corner point coordinates : %.2f ' %Point4x,", %.2f"%Point4y)
print ('Center point of a rectngle : %.2f ' %Centerx,", %2f"%Centery)
Reply


Messages In This Thread
How do classes work? (rectangle) - by GFreenD - Sep-17-2019, 03:41 PM
RE: How do classes work? (rectangle) - by GFreenD - Sep-18-2019, 07:38 PM
RE: How do classes work? (rectangle) - by buran - Sep-18-2019, 07:56 PM
RE: How do classes work? (rectangle) - by GFreenD - Sep-19-2019, 05:44 AM

Forum Jump:

User Panel Messages

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