![]() ![]() Round floating point numbers to 3 decimal places with round(val, 3).Python constructors are defined with def _init_(self, arg1, arg2):.What is the length of the room in feet? 15 This exercise is similar to Exercise 2, Temperature Conversion and Exercise. Print(str(room.areaMeters()) + " square meters.") $ python3 exercises/Ex07_area_of_a_rectangular_room/rectangular_room.py Area, perimeter, volume, and surface area are straightforward calculations. Print(str(room.areaFeet()) + " square feet") Room = RectangularRoom(float(length), float(width)) Print("You entered dimensions of " + length + " feet by " + width + " feet.") Width = input("What is the width of the room in feet? ") Length = input("What is the length of the room in feet? ") The script functionality with user input is nothing new from the previous examples, just read in some data, If we know the width and height then, we can calculate the area of a rectangle using below formula. Before we step into the Python Program to find Area of a Rectangle example, Let see the definitions and formulas. Return round(self.areaFeet() * self.SQUARE_FEET_TO_SQUARE_METER_CONVERSION, 3) Write a Python Program to find Area of a Rectangle and Perimeter of a Rectangle with example. SQUARE_FEET_TO_SQUARE_METER_CONVERSION = 0.09290304 Nothing particularly tricky here other than having to figure out how to round floating point numbers toģ decimal places. Then a couple of functions are required to provide the area of the room in square feet and square meters. At this point I realise I don't know how to write a constructor for a PythonĬlass nor how to represent class variables, so some research is required and of course it turns out to be simple enough. Made a clear expectation that a RectangularRoom class is required and how I expect it to take Writing tests first is a nice way to approach a problem because it drives the design. Self.assertEqual(rectangularRoom.areaMeters(), 27.871) Self.assertEqual(rectangularRoom.areaFeet(), 300)ĭef test_given15FeetBy20Feet_thenArea27point871SquareMeters(self): RectangularRoom = RectangularRoom(15, 20) import unittestįrom exercises.Ex07_area_of_a_rectangular_room.rectangular_room import RectangularRoomĬlass TestRectangularRoom(unittest.TestCase):ĭef test_given15FeetBy20Feet_thenArea300SquareFeet(self): Of course starting with a couple of tests. The length and width values at construction time then it's easy to have accessor functions that provide theĪrea in square feet and square meters. This one was straight forward for me as it can be solved quite tidily with an Object. ![]() You entered dimensions of 15 feet by 20 feet. What is the width of the room in feet? 20 Example Output What is the length of the room in feet? 25 Then display the area in both square feet and square meters. Prompt the user for the length and width inįeet. Exercises for Programmers (Python): 7 of 57 - Area of a Rectangular Room | Tim Cooke Tim Cooke Exercises for Programmers (Python): 7 of 57 - Area of a Rectangular Room 2nd September 2020Įxercise 7 in book Exercises for Programmers: 57 Challenges to Develop Your Coding SkillsĬreate a program that calculates the area of a room. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |