Advertisement
karlakmkj

Encapsulating Methods - Exercise

Jan 4th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. '''
  2. Previously, you wrote a class called ExerciseSession that had three attributes: an exercise name, an intensity, and a duration.
  3. Add a new method to that class called calories_burned.
  4. calories_burned should have no parameters (besides self, as every method in a class has). It should return an integer representing the number of calories burned according to the following formula:
  5. If the intensity is "Low", 4 calories are burned per minute.
  6. If the intensity is "Moderate", 8 calories are burned per minute.
  7. If the intensity is "High", 12 calories are burned per minute.
  8. '''
  9.  
  10. #Add your code here!
  11. class ExerciseSession:
  12.     def __init__(self, exercise, intensity, duration):
  13.         self.exercise = exercise
  14.         self.intensity = intensity
  15.         self.duration = duration
  16.  
  17.     def get_exercise(self):
  18.         return self.exercise
  19.    
  20.     def get_intensity(self):
  21.         return self.intensity
  22.    
  23.     def get_duration(self):
  24.         return self.duration
  25.    
  26.     def set_exercise(self, name):
  27.         self.exercise = name
  28.        
  29.     def set_intensity(self, newintensity):
  30.         self.intensity = newintensity
  31.    
  32.     def set_duration(self, newduration):
  33.         self.duration = newduration
  34.  
  35.     def calories_burned(self):
  36.         if self.intensity == "Low":
  37.             calories = 4    # or return 4*self.duration
  38.         elif self.intensity == "Moderate":
  39.             calories = 8    # or return 8*self.duration
  40.         elif self.intensity == "High":
  41.             calories = 12   # or return 12*self.duration
  42.         return calories * self.duration
  43.     #in the model answer, should multiply the self.duration at each condition
  44.  
  45. #If your code is implemented correctly, the lines below will run error-free. They will result in the following output to the console:
  46. #240
  47. #360
  48. new_exercise = ExerciseSession("Running", "Low", 60)
  49. print(new_exercise.calories_burned())
  50. new_exercise.set_exercise("Swimming")
  51. new_exercise.set_intensity("High")
  52. new_exercise.set_duration(30)
  53. print(new_exercise.calories_burned())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement