Ice Cream Round 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class IceCream(object):
    def __init__(self, flavor, numScoops, costPerScoop, remaining_icecream):
        self.flavor = flavor
        self.numScoops = numScoops
        self.costPerScoop = costPerScoop
        self.remaining_icecream = remaining_icecream

    def scoop(self):
        #scoops icecream and decreases the number of scoops left
        self.remaining_icecream -= self.numScoops
        return self.remaining_icecream

    def total_cost(self):
        #vanilla ice cream is sold at a discount of half off!
        if self.flavor == "vanilla":
            total_cost = self.numScoops * .5*self.costPerScoop

        else:
            total_cost = self.numScoops * self.costPerScoop

        return total_cost

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
def sizeA(self):
    if self.numScoops in range(1,3):
        print "small"
    elif self.numScoops in range(4,5):
        print "medium"
    else:
        print "large"

1
2
3
4
5
6
7
def sizeB(self):
    if self.numScoops in range(1,4):
        print "small"
    elif self.numScoops in range(4,6):
        print "medium"
    else:
        print "large"

1
2
3
4
5
6
7
def sizeC(self):
    if numScoops in range(1,4):
        print "small"
    elif numScoops in range(4,6):
        print "medium"
    else:
        print "large"

1
2
3
4
5
6
7
def sizeD(self):
    if scoops in range(1,4):
        print "small"
    elif scoops in range(4,6):
        print "medium"
    else:
        print "large"

(ignore the fact that these functions may return None along with the size printed)

sizeC sizeB sizeD sizeA

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.

1 solution

Karleigh Moore
Jun 14, 2016

The correct answer is sizeB.

sizeA will return the wrong answer for the input

1
2
chocolate = IceCream("chocolate",3, 1,6)
print chocolate.sizeA()

for example because the ranges in the if statements ignore the fact that the Python range function is exclusive, meaning that range(1,3) only includes 1 and 2. 3 is not included in the small range, and therefore, is taken care of under the else and named (incorrectly) a large .

sizeC lacks the self. parts of the variables. The function doesn't have access to a variable called numScoops , it can only access self.numScoops which is the number of scoops associated with that particular instance of IceCream , which is chocolate .

sizeD references the variable scoops which does not exist at all (there is a scoop 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.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...