Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise: modify03 Description Write a function that receives a Pencil object as a parameter. It should click the pencil 53 times then add two additional
Exercise: modify03
Description
Write a function that receives a Pencil object as a parameter. It should click the pencil 53 times then add two additional leads. Finally, it will click the pencil 13 more times and return the pencil.
Function Name
modify03
Parameters
p: A Pencil object
Return Value
The modified Pencil object.
Examples
import pencil p = pencil.Pencil(5) p = modify03(p) print(p.get_num_leads()) # -> 1 print(p.get_current_lead_length()) # -> 4
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 NEStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started