Ice Cream Again

What will the program output when you run

1
2
vanilla = IceCream("vanilla", 4, 2, 10)
print vanilla.total_cost()

using the below code?

Try to walk through the program in your head to find the answer before you actually run the code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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):
                #shows the amount of ice cream remaining after an order is scooped
        #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

1.0 2.0 4.0 8.0

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 answer is 4.0

The number of scoops is 4 and it costs 2 dollars per scoop. Since vanilla is the flavor and vanilla is half off, the total cost of this order is .5(4 * 2) = 4.0

Run the code to verify.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...