Question: Exercise: query03 Description Write a function that receives a Pencil object as a parameter. It should return the total length of lead in the pencil.

Exercise: query03

Description

Write a function that receives a Pencil object as a parameter. It should return the total length of lead in the pencil. This includes the current lead length, as well as the full length of all other leads. Remember, the current lead is not included in the _num_leads count (see examples below).

Function Name

query03

Parameters

p: A Pencil object

Return Value

The total length of all leads in the Pencil object p.

Examples

p = Pencil(0) print(query03(p)) # -> 10

p1 = Pencil(1) print(query03(p1)) # -> 20

Exercise: query03 Description Write a function that receives a Pencil object as

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!