Question: Exercise: modify04 Description Write a function that receives a Pencil object as a parameter. It should click the pencil until its current lead length is

Exercise: modify04

Description

Write a function that receives a Pencil object as a parameter. It should click the pencil until its current lead length is at the maximum possible value, then return the pencil. Assume that the given pencil will always have enough lead.

Function Name

modify04

Parameters

p: A Pencil object

Return Value

The Pencil object modified such that its current lead length is at the maximum possible length.

Examples

import pencil p = pencil.Pencil(5) p.click() print(p.get_current_lead_length()) # -> 9 p = modify04(p) print(p.get_current_lead_length()) # -> 10

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

umodifyo2.py * pencil.py X 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 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. 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. 7 8 9 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 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 > : 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

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!