Python learning-stage Comprehensive Exercises 2. python Comprehensive Exercises
Based on the previous class learning, do the following example exercises: (do not read the code first, and try to write the code first. The code is for reference only. There are multiple implementation methods)
1. Triangle & Equilateral
1) create a class Triangle to indicate a Triangle. It contains three attribute values: angle1, angle2, and angle3;
Class Method check_angles (): If the three angles are added = 180, return True; if not, return False
1 class Triangle (object): 2 def _ init _ (self, angle1, angle2, angle3): 3 self. angle1 = angle1 4 self. angle2 = angle2 5 self. angle3 = angle3 6 7 def checkAngles (self): 8 if (self. angle1 + self. angle2 + self. angle3) = 180: 9 return True10 else: 11 return False12 13 t1 = Triangle (, 90) 14 print (t1.angle1, t1.angle2, t1.angle3) 15 print (t1.checkAngles ()) 16 t2 = Triangle (40, 50, 91) 17 print (t2.checkAngles ())Triangle
2) create a class Equilateral to inherit the Triangle in the previous example 1, which indicates an equi-edge Triangle. The difference is that the three corners of the attribute value are 60, and the corresponding check_angles () always returns True.
1 class Equilateral (Triangle): 2 def _ init _ (self, angle1 = 60, angle2 = 60, angle3 = 60): 3 self. angle1 = angle14 self. angle2 = angle25 self. angle3 = angle36 7 t3 = Equilateral () 8 print (t3.angle1, t3.angle2, t3.angle3) 9 print (t3.checkAngles ())Equilateral 1
The code in the example above can meet the conditions, but it is better to call the parent class constructor and override check_angles () to always return True. See the code below
1 class Equilateral (Triangle): 2 def _ init _ (self, angle1 = 60, angle2 = 60, angle3 = 60): 3 Triangle. _ init _ (self, angle1, angle2, angle3) 4 5 def checkAngles (self): 6 return True 7 8 t3 = Equilateral () 9 print (t3.angle1, t3.angle2, t3.angle3) 10 print (t3.checkAngles ())Equilateral 2
2. Car & ElectricCar
1) Create the class Car member variable condition = "new", which contains three construction attributes: model, color, and mpg;
Class Method displayCar () print concatenated string This is a {color} {model} car with {mpg} MPG. For example, "This is a blue Xmodel car with 40 MPG ."
Class Method driveCar () changes the member variable condition = "used"
1 class Car (object): 2 condition = "new" 3 def _ init _ (self, model, color, mpg): 4 self. model = model 5 self. color = color 6 self. mpg = mpg 7 8 def displayCar (self): 9 print ("This is a {s. color} {s. model} car with {s. mpg} MPG. ". format (s = self) 10 11 def driveCar (self): 12 self. condition = "used" 13 14 car1 = Car ("DeLorean", "silver", 88) 15 car1.displayCar () 16 print (Car. condition) 17 print (car1.condition) 18 car1.driveCar () 19 print (car1.condition)Car
2) create a class ElectricCar to inherit the Car and add an attribute variable battery_type. Rewrite the driveCar () function and change condition = "like new"
1 class ElectricCar (Car): 2 def _ init _ (self, model, color, mpg, battery_type): 3 Car. _ init _ (self, model, color, mpg) 4 self. battery_type = battery_type 5 6 def driveCar (self): 7 self. condition = "like new" 8 9 car2 = ElectricCar ("dd", "Red", 88, "molten salt") 10 print (car2.battery _ type, car2.condition) 11 car2.displayCar () # inherit the Car Method 12 car2.driveCar () # Call the overwritten Method 13 print (car2.condition)ElectricCar
3. Point3D
Creates class Point3D, which represents a point in three-dimensional coordinates. It contains three attribute variables: x, y, and z.
The class _ repr _ method is displayed as (x, y, z)
Class method distance () returns the distance from the point to the origin (0, 0)
Python class method _ repr _ rewrite print class_name display, refer to the Code to understand
1 import math 2 class Point3D (object): 3 def _ init _ (self, x, y, z): 4 self. x = x 5 self. y = y 6 self. z = z 7 def _ repr _ (self): 8 return ("({s. x}, {s. y}, {s. z })". format (s = self) 9 10 def distance (self): 11 d = math. sqrt (self. x ** 2 + self. y ** 2 + self. z ** 2) 12 return d13 14 point1 = Point3D (3, 4, 0) 15 print (point1) 16 print (point1.distance ())Point3D
4. Employee & PartTimeEmployee
1) Create class Employee, including the member variable hour_wage = 20, including the property variable: name;
Class Method calculateWage () calculates the current day's work money, passing the parameter hours, return hours * hour_wage
1 class Employee (object): 2 hour_wage = 20 3 def _ init _ (self, name): 4 self. name = name 5 6 def calculateWage (self, hours): 7 return self. hour_wage * hours 8 9 Peter = Employee ("Peter") 10 print (Peter. calculateWage (5 ))Employee
2) create a class PartTimeEmployee to inherit from the Employee. The member variables hour_wage = 18, parttime_wage = 15 and the property variables are the same as those of the Employee.
Class Method calculateWage () override. If hour> = 8, return hour_wage * hours; if hour <8, return parttime_wage * hours
1 class PartTimeEmployee (Employee): 2 hour_wage = 18 3 parttime_wage = 15 4 5 def calculateWage (self, hours): 6 if hours> = 8: 7 return self. hour_wage * hours 8 else: 9 return self. parttime_wage * hours10 11 May = PartTimeEmployee ("May") 12 print (May. calculateWage (5) 13 print (May. calculateWage (8 ))PartTimeEmployee