Build a Discount Calculator - Step 28

Tell us what’s happening:

I am stuck at return statement. I think I am correct,what mighr be the problem?

Your code so far

from abc import ABC, abstractmethod
from typing import List

class Product:
    def __init__(self, name: str, price: float) -> None:
        self.name = name
        self.price = price

    def __str__(self) -> str:
        return f'{self.name} - ${self.price}'

class DiscountStrategy(ABC):
    @abstractmethod
    def is_applicable(self, product: Product, user_tier: str) -> bool:
        pass

    @abstractmethod
    def apply_discount(self, product: Product) -> float:
        pass

class PercentageDiscount(DiscountStrategy):
    def __init__(self, percent: int) -> None:
        self.percent = percent

    def is_applicable(self, product: Product, user_tier: str) -> bool:
        return self.percent <= 70

    def apply_discount(self, product: Product) -> float:
        return product.price * (1 - self.percent / 100)

class FixedAmountDiscount(DiscountStrategy):
    def __init__(self, amount: int) -> None:
        self.amount = amount

    def is_applicable(self, product: Product, user_tier: str) -> bool:
        return product.price * 0.9 > self.amount

    def apply_discount(self, product: Product) -> float:
        return product.price - self.amount

class PremiumUserDiscount(DiscountStrategy):
    def is_applicable(self, product: Product, user_tier: str) -> bool:
        return user_tier.lower() == 'premium'

    def apply_discount(self, product: Product) -> float:
        return product.price * 0.8

class DiscountEngine:
    def __init__(self, strategies: List[DiscountStrategy]) -> None:
        self.strategies = strategies

    def calculate_best_price(self, product: Product, user_tier: str) -> float:
        prices = [product.price]

        for strategy in self.strategies:
            if strategy.is_applicable(product, user_tier):
                discounted = strategy.apply_discount(product)
                prices.append(discounted)

# User Editable Region

        return prices.min()

# User Editable Region

product = Product('Wireless Mouse', 50.0)
print(product)

discount = PercentageDiscount(10)
print(discount.apply_discount(product))

fixed_discount = FixedAmountDiscount(5)
print(fixed_discount.apply_discount(product))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Discount Calculator - Step 28

What data type is prices ?

Does it have a min() method?

data type is a list but question is asking for min function

Step 28

Finally, return the minimum value from the prices list using the min() function. This will give you the best (lowest) price after considering all applicable discounts.

What you’ve done is usually called a “method” but the instructions are referring to a function

Do lists have a min() method?

no they don’t but they have a min function

How can you confirm that?

What’s the difference between a method and a function?

how i understand methods, they are defined in a class with def while functions are like len(),min(),max()

How do you call a function?

How do you call a method?

How would use you use len()?

calling a function: like len() e.g

list_1=[1,2,3,4]

length_of_list_1=len(list_1)

method:

e.g class Dog:

def __init__(self,animal):

self.animal=animal

Ok how are you going to use min()?

min(prices) because its a function

1 Like

that’s not what you are writing here