Question: Exercise: create03 Description Write a function that receives one integer parameter. It should return a Pencil object constructed with the number of leads matching the
Exercise: create03
Description
Write a function that receives one integer parameter. It should return a Pencil object constructed with the number of leads matching the value of the parameter.
Function Name
create03
Parameters
num_leads : An integer (the number of leads to put into the pencil)
Return Value
A Pencil object constructed with num_leads leads.
Examples
p1 = create07(0) print(p1.get_num_leads()) # -> 0 print(p1.get_current_lead_length()) # -> 10
p2 = create07(4) print(p1.get_num_leads()) # -> 4 print(p1.get_current_lead_length()) # -> 10
p3 = create07(10) print(p1.get_num_leads()) # -> 5 print(p1.get_current_lead_length()) # -> 10

pencil.py 1 MAX_LEAD_LENGTH = 10 2 MAX_NUM_LEADS = 5 3 4 class Pencil: 5 "Represent a mechanical pencil with leads in its barrel." 6 7 def __init__(self, num_leads): 8 "Initialize a pencil. 9 10 Data members: 11 _num_leads -- the number of lead sticks in the barrel of the 12 pencil. 13 _current_lead_length -- the length of the lead stick extending 14 from the pencil. 15 16 self._num_leads - 17 self._current_lead_length = MAX_LEAD_LENGTH 18 self.add_leads(num_leads) 19 20 def get_num_leads(self): 21 "Return the number of leads in the pencil. 22 23 This does not count the lead that is extending from the pencil. 24 25 return self._num_leads 26 27 def get_current_lead_length(self): 28 "Return the length of the lead extending from the pencil." 29 return self._current_lead_length 30 31 def click(self): 32 "*"Click the pencil, reducing the current lead length by one. 33 34 If the current lead is used up, reduce the number of leads in 35 the pencil by one and set the current lead length to the max 36 lead length. 37 38 if self._current_lead_length > 0: 39 self._current_lead_length -= 1 40 if self._current_lead_length == 0 and self._num_leads > 0: 41 self._current_lead_length = MAX_LEAD_LENGTH 42 self._num_leads -= 1 43 return self._current_lead_length > 0 44 45 def add_leads(self, num_additional_leads): 46 "Add leads to the pencil. 47 48 Only add a positive number of leads, up to the maximum number 49 of leads. 50 51 if num_additional_leads > 0: 52 self._num_1 _leads += num_additional_leads 53 if self._num_leads > MAX_NUM_LEADS: 54 self._num_leads = MAX_NUM_LEADS 55 return self._num_leads 56 o oo NE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
