Найти - Пользователи
Полная версия: Проверьте, пожалуйста, скрипт - что не так?
Начало » Python для новичков » Проверьте, пожалуйста, скрипт - что не так?
1
BlackRabbit
С целью постичь премудрости Питона сижу на буржуйском codeacademy.com Так вот только застрял на 19 теме “Введение в классы”. Задание такое:
“Create an instance of ShoppingCart called my_cart. Initialize it with any values you like, then use the add_item method to add an item to your cart”. В заготовку надо вписать свой код.
Скрипт прикладываю. Заранее спасибо.
alexsis
Вам нужно что-то такое?
class ShoppingCart(object):
    """Creates shopping cart objects
    for users of our fine website."""
    items_in_cart = {}
    def __init__(self, customer_name):
        self.customer_name = customer_name
    def add_item(self, product, price):
        """Add product to the cart."""
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price
            print product + " added."
        else:
            print product + " is already in the cart."
    def remove_item(self, product):
        """Remove product from the cart."""
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print product + " removed."
        else:
            print product + " is not in the cart."
my_cart = ShoppingCart("Eric")
my_cart.add_item("Ukelele", 10)
my_cart.remove_item("Ukelele")
Вот такой вывод:
Ukelele added.
Ukelele removed.
BlackRabbit
Спасибо! Теперь ясно, в чем была загвоздка
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB