Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

Define the term Working Capital Gap.

Answered: 1 week ago