Question: Exercise: create01 Description Write a function that receives no parameters and returns a Pencil object constructed with 3 leads. Refer to the Pencil documentation below.
Exercise: create01 Description Write a function that receives no parameters and returns a Pencil object constructed with 3 leads. Refer to the Pencil documentation below. Function Name create01 Parameters None Return Value A Pencil object with 3 leads. Example This is one example of how we might use your function: p = create01() print(p.get_num_leads()) # -> 3 print(p.get_current_lead_length()) # -> 10
\

1 import Pencil 2 def create01(): 3 return Pencil.Pencil(3,10) 4 p=create01() 5 print(p.get_num_leads()) 6 print(p.get_current_lead_length()) 7 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
