1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Using Python, which of the following
methods
could be added to the
class
IceCream
to print out the size of the order based on the number of scoops where 1 to 3 scoops is a small, 4 to 5 is a medium, and more than that is
a large.
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
(ignore the fact that these functions may return
None
along with the size printed)
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The correct answer is sizeB.
sizeA will return the wrong answer for the input
for example because the ranges in the
if
statements ignore the fact that the Python range function is exclusive, meaning thatrange(1,3)
only includes 1 and 2. 3 is not included in thesmall
range, and therefore, is taken care of under theelse
and named (incorrectly) alarge
.sizeC lacks the
self.
parts of the variables. The function doesn't have access to a variable callednumScoops
, it can only accessself.numScoops
which is the number of scoops associated with that particular instance ofIceCream
, which ischocolate
.sizeD references the variable
scoops
which does not exist at all (there is ascoop
method, but this has nothing to do with this variable).If you run the code on this example, you will see that the correct answers are produced by sizeB, which has the correct ranges and references the correct variables.