Question: Exercise: query02 Description Write a function that receives a Pencil object as a parameter and returns the number of leads in that pencil. Function Name
Exercise: query02
Description
Write a function that receives a Pencil object as a parameter and returns the number of leads in that pencil.
Function Name
query02
Parameters
p: A Pencil object
Return Value
The number of leads in the Pencil object p.
Example
import pencil p = pencil.Pencil(3) print(query02(p)) # -> 3

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