-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.2.py
More file actions
52 lines (40 loc) · 1.27 KB
/
11.2.py
File metadata and controls
52 lines (40 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Animal:
# 类属性
is_alive = True
health = "good"
# 实例属性
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
print("The name is {}".format(self.name))
print("The age is {}".format(self.age))
zebra = Animal("Jeffrey", 2)
zebra.description()
# class Square(object):
# def __init__(self):
# self.sides = 4
#
#
# my_square = Square()
# print(my_square.sides)
# 购物车
class ShoppingCart(object):
def __init__(self, customer_name):
self.customer_name = customer_name
self.items_in_cart = {}
def add_item(self, product, price):
if product not in self.items_in_cart:
self.items_in_cart[product] = price
print("{} is added into shopping cart".format(product))
else:
print("{} is already in the shoppingcart!".format(product))
def remove_item(self, product):
if product in self.items_in_cart:
del self.items_in_cart[product]
print("{} is removed from the shoppingcart!".format(product))
else:
print("{} is not in the shoppingcart!".format(product))
my_cart = ShoppingCart("Harry")
my_cart.add_item("iPhone", 9000)
my_cart.remove_item("ipad")