Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle(): | n | 2 | class Circle: |
3 | def __init__(self, radius): | 3 | def __init__(self): | ||
4 | self.radius = radius | 4 | self.radius=radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius**2 | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__ == '__main__': | n | 9 | if __name__=='__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter())) | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | class ItemToPurchase: | ||||
15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
16 | self.item_name=name | 15 | self.item_name=name | ||
17 | self.item_description=description | 16 | self.item_description=description | ||
18 | self.item_price=price | 17 | self.item_price=price | ||
19 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
20 | def print_item_description(self): | 19 | def print_item_description(self): | ||
21 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
22 | class ShoppingCart: | 21 | class ShoppingCart: | ||
23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
24 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 24 | self.current_date = current_date | ||
26 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
27 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
28 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
29 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
30 | tremove_item = False | 29 | tremove_item = False | ||
31 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 33 | tremove_item = True | ||
35 | break | 34 | break | ||
36 | if not tremove_item: | 35 | if not tremove_item: | ||
37 | print('Item not found in the cart. Nothing removed') | 36 | print('Item not found in the cart. Nothing removed') | ||
38 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 38 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 41 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 43 | break | ||
45 | if not tmodify_item: | 44 | if not tmodify_item: | ||
46 | print('Item not found in the cart. Nothing modified') | 45 | print('Item not found in the cart. Nothing modified') | ||
47 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 47 | num_items = 0 | ||
49 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 50 | return num_items | ||
52 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 52 | total_cost = 0 | ||
54 | cost = 0 | 53 | cost = 0 | ||
55 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 56 | total_cost += cost | ||
58 | return total_cost | 57 | return total_cost | ||
59 | def print_total(self): | 58 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 62 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
75 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from the cart\n' | 81 | 'r - Remove item from the cart\n' | ||
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
84 | "i - Output item's descriptions\n" | 83 | "i - Output item's descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
90 | command = input('Choose an option:') | 89 | command = input('Choose an option:') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
102 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
109 | itemName = input('Enter the name of the item to remove :\n') | 108 | itemName = input('Enter the name of the item to remove :\n') | ||
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
113 | itemName = input('Enter the name of the item :\n') | 112 | itemName = input('Enter the name of the item :\n') | ||
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 123 | print_menu(newCart)from math import sqrt | t | 122 | print_menu(newCart)from math import sqrt |
124 | class pt3d: | 123 | class pt3d: | ||
125 | def __init__(self, x, y, z): | 124 | def __init__(self, x, y, z): | ||
126 | self.x = x | 125 | self.x = x | ||
127 | self.y = y | 126 | self.y = y | ||
128 | self.z = z | 127 | self.z = z | ||
129 | def __add__(self, other): | 128 | def __add__(self, other): | ||
130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
131 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
133 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
134 | return self.x == other.x & self.y == other.y & self.z == other.z | 133 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
135 | def __str__(self): | 134 | def __str__(self): | ||
136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
137 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
138 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
139 | print(p1 + p2) | 138 | print(p1 + p2) | ||
140 | print(p1 - p2) | 139 | print(p1 - p2) | ||
141 | print(p1 == p2) | 140 | print(p1 == p2) | ||
142 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
143 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self): | 3 | def __init__(self, radius): | ||
4 | self.radius=radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*(self.radius**2) | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__=='__main__': | n | 9 | if __name__ == '__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter())) | ||
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from the cart\n' | 82 | 'r - Remove item from the cart\n' | ||
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output item's descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
89 | command = input('Choose an option:') | 90 | command = input('Choose an option:') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 122 | print_menu(newCart)from math import sqrt | t | 123 | print_menu(newCart)from math import sqrt |
123 | class pt3d: | 124 | class pt3d: | ||
124 | def __init__(self, x, y, z): | 125 | def __init__(self, x, y, z): | ||
125 | self.x = x | 126 | self.x = x | ||
126 | self.y = y | 127 | self.y = y | ||
127 | self.z = z | 128 | self.z = z | ||
128 | def __add__(self, other): | 129 | def __add__(self, other): | ||
129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
130 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
132 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
133 | return self.x == other.x & self.y == other.y & self.z == other.z | 134 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
134 | def __str__(self): | 135 | def __str__(self): | ||
135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
136 | p1 = pt3d(1, 1, 1) | 137 | p1 = pt3d(1, 1, 1) | ||
137 | p2 = pt3d(2, 2, 2) | 138 | p2 = pt3d(2, 2, 2) | ||
138 | print(p1 + p2) | 139 | print(p1 + p2) | ||
139 | print(p1 - p2) | 140 | print(p1 - p2) | ||
140 | print(p1 == p2) | 141 | print(p1 == p2) | ||
141 | print(p1+p1 == p2) | 142 | print(p1+p1 == p2) | ||
142 | print(p1==p2+pt3d(-1, -1, -1)) | 143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, r): | n | 3 | def __init__(self,r): |
4 | self.radius = r | 4 | self.radius = r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.radius**2)*(math.pi) | n | 6 | return (self.radius**2)*(math.pi) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*(math.pi)*(self.radius) | n | 8 | return 2*(math.pi)*(self.radius) |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | ||
> | em_description = "none"): | > | em_description = "none"): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | 20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | ||
> | .item_price) + " = $" + | > | .item_price) + " = $" + | ||
21 | str(self.item_price * self.item_quantity)) | 21 | str(self.item_price * self.item_quantity)) | ||
22 | def print_item_description(self): | 22 | def print_item_description(self): | ||
23 | print(self.item_name + ": " + str(self.item_description)) | 23 | print(self.item_name + ": " + str(self.item_description)) | ||
24 | class ShoppingCart: | 24 | class ShoppingCart: | ||
25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | ||
26 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 27 | self.current_date = current_date | ||
28 | self.cart_items = [] | 28 | self.cart_items = [] | ||
29 | def add_item(self, ItemToPurchase): | 29 | def add_item(self, ItemToPurchase): | ||
30 | self.cart_items.append(ItemToPurchase) | 30 | self.cart_items.append(ItemToPurchase) | ||
31 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
32 | RemoveIt = False | 32 | RemoveIt = False | ||
33 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 35 | self.cart_items.remove(item) | ||
36 | RemoveIt = True | 36 | RemoveIt = True | ||
37 | break | 37 | break | ||
38 | if not RemoveIt: | 38 | if not RemoveIt: | ||
39 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
n | 41 | ModifyIt = False | n | 41 | Modify_It = False |
42 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 44 | ModifyIt = True | n | 44 | Modify_It = True |
45 | if ( | 45 | if ( | ||
46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | 46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | ||
> | uantity == 0 and itemToPurchase.item_description == 'none'): | > | uantity == 0 and itemToPurchase.item_description == 'none'): | ||
47 | break | 47 | break | ||
48 | else: | 48 | else: | ||
49 | if (itemToPurchase.item_price != 0): | 49 | if (itemToPurchase.item_price != 0): | ||
50 | self.cart_items[i].item_price = itemToPurchase.item_pric | 50 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||
> | e | > | e | ||
51 | if (itemToPurchase.item_quantity != 0): | 51 | if (itemToPurchase.item_quantity != 0): | ||
52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | 52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | > | uantity | ||
53 | if (itemToPurchase.item_description != 'none'): | 53 | if (itemToPurchase.item_description != 'none'): | ||
54 | self.cart_items[i].item_description = itemToPurchase.ite | 54 | self.cart_items[i].item_description = itemToPurchase.ite | ||
> | m_description | > | m_description | ||
55 | break | 55 | break | ||
n | 56 | if not ModifyIt: | n | 56 | if not Modify_It: |
57 | print('Item not found in cart. Nothing modified.') | 57 | print('Item not found in cart. Nothing modified.') | ||
58 | def get_num_items_in_cart(self): | 58 | def get_num_items_in_cart(self): | ||
59 | num_items = 0 | 59 | num_items = 0 | ||
60 | for item in self.cart_items: | 60 | for item in self.cart_items: | ||
61 | num_items = num_items + item.item_quantity | 61 | num_items = num_items + item.item_quantity | ||
62 | return num_items | 62 | return num_items | ||
63 | def get_cost_of_cart(self): | 63 | def get_cost_of_cart(self): | ||
64 | total_cost = 0 | 64 | total_cost = 0 | ||
65 | cost = 0 | 65 | cost = 0 | ||
66 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
67 | cost = (item.item_quantity * item.item_price) | 67 | cost = (item.item_quantity * item.item_price) | ||
68 | total_cost += cost | 68 | total_cost += cost | ||
69 | return total_cost | 69 | return total_cost | ||
70 | def print_total(self): | 70 | def print_total(self): | ||
71 | total_cost = self.get_cost_of_cart() | 71 | total_cost = self.get_cost_of_cart() | ||
72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | 73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | ||
74 | for item in self.cart_items: | 74 | for item in self.cart_items: | ||
75 | item.print_item_cost() | 75 | item.print_item_cost() | ||
76 | if (total_cost == 0): | 76 | if (total_cost == 0): | ||
77 | print('SHOPPING CART IS EMPTY') | 77 | print('SHOPPING CART IS EMPTY') | ||
78 | print('\nTotal: $%d' % (total_cost)) | 78 | print('\nTotal: $%d' % (total_cost)) | ||
79 | def print_descriptions(self): | 79 | def print_descriptions(self): | ||
80 | if len(self.cart_items) == 0: | 80 | if len(self.cart_items) == 0: | ||
81 | print('SHOPPING CART IS EMPTY') | 81 | print('SHOPPING CART IS EMPTY') | ||
82 | else: | 82 | else: | ||
83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
84 | print('\nItem Descriptions') | 84 | print('\nItem Descriptions') | ||
85 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
86 | item.print_item_description() | 86 | item.print_item_description() | ||
87 | def print_menu(): | 87 | def print_menu(): | ||
88 | print('MENU\n' | 88 | print('MENU\n' | ||
89 | 'a - Add item to cart\n' | 89 | 'a - Add item to cart\n' | ||
90 | 'r - Remove item from cart\n' | 90 | 'r - Remove item from cart\n' | ||
91 | 'c - Change item quantity\n' | 91 | 'c - Change item quantity\n' | ||
92 | "i - Output items' descriptions\n" | 92 | "i - Output items' descriptions\n" | ||
93 | 'o - Output shopping cart\n' | 93 | 'o - Output shopping cart\n' | ||
94 | 'q - Quit\n') | 94 | 'q - Quit\n') | ||
95 | def execute_menu(command, my_cart): | 95 | def execute_menu(command, my_cart): | ||
96 | customer_Cart = my_cart | 96 | customer_Cart = my_cart | ||
97 | if command == 'a': | 97 | if command == 'a': | ||
98 | print("\nADD ITEM TO CART") | 98 | print("\nADD ITEM TO CART") | ||
99 | item_name = input('Enter the item name:\n') | 99 | item_name = input('Enter the item name:\n') | ||
100 | item_description = input('Enter the item description:\n') | 100 | item_description = input('Enter the item description:\n') | ||
101 | item_price = int(input('Enter the item price:\n')) | 101 | item_price = int(input('Enter the item price:\n')) | ||
102 | item_quantity = int(input('Enter the item quantity:\n')) | 102 | item_quantity = int(input('Enter the item quantity:\n')) | ||
103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
104 | customer_Cart.add_item(itemtoPurchase) | 104 | customer_Cart.add_item(itemtoPurchase) | ||
105 | elif command == 'o': | 105 | elif command == 'o': | ||
106 | print('OUTPUT SHOPPING CART') | 106 | print('OUTPUT SHOPPING CART') | ||
107 | customer_Cart.print_total() | 107 | customer_Cart.print_total() | ||
108 | elif command == 'i': | 108 | elif command == 'i': | ||
109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
110 | customer_Cart.print_descriptions() | 110 | customer_Cart.print_descriptions() | ||
111 | elif command == 'r': | 111 | elif command == 'r': | ||
112 | print('REMOVE ITEM FROM CART') | 112 | print('REMOVE ITEM FROM CART') | ||
113 | itemName = input('Enter name of item to remove:\n') | 113 | itemName = input('Enter name of item to remove:\n') | ||
114 | customer_Cart.remove_item(itemName) | 114 | customer_Cart.remove_item(itemName) | ||
115 | elif command == 'c': | 115 | elif command == 'c': | ||
116 | print('\nCHANGE ITEM QUANTITY') | 116 | print('\nCHANGE ITEM QUANTITY') | ||
117 | itemName = input('Enter the item name:\n') | 117 | itemName = input('Enter the item name:\n') | ||
118 | qty = int(input('Enter the new quantity:\n')) | 118 | qty = int(input('Enter the new quantity:\n')) | ||
119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
120 | customer_Cart.modify_item(itemToPurchase) | 120 | customer_Cart.modify_item(itemToPurchase) | ||
121 | if __name__ == "__main__": | 121 | if __name__ == "__main__": | ||
122 | customer_name = input("Enter customer's name:\n") | 122 | customer_name = input("Enter customer's name:\n") | ||
123 | current_date = input("Enter today's date:\n") | 123 | current_date = input("Enter today's date:\n") | ||
124 | print("\nCustomer name: %s" % customer_name) | 124 | print("\nCustomer name: %s" % customer_name) | ||
125 | print("Today's date: %s" % current_date) | 125 | print("Today's date: %s" % current_date) | ||
126 | newCart = ShoppingCart(customer_name, current_date) | 126 | newCart = ShoppingCart(customer_name, current_date) | ||
127 | command = '' | 127 | command = '' | ||
128 | while command != 'q': | 128 | while command != 'q': | ||
129 | print() | 129 | print() | ||
130 | print_menu() | 130 | print_menu() | ||
131 | command = input('Choose an option:\n') | 131 | command = input('Choose an option:\n') | ||
132 | while command != 'a' and command != 'o' and command != 'i' and command ! | 132 | while command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c': | > | = 'q' and command != 'r' and command != 'c': | ||
133 | command = input('Choose an option:\n') | 133 | command = input('Choose an option:\n') | ||
134 | execute_menu(command, newCart)import math | 134 | execute_menu(command, newCart)import math | ||
135 | class pt3d: | 135 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
137 | self.x= x | 137 | self.x= x | ||
138 | self.y= y | 138 | self.y= y | ||
139 | self.z= z | 139 | self.z= z | ||
140 | def __add__(self, other): | 140 | def __add__(self, other): | ||
141 | x = self.x + other.x | 141 | x = self.x + other.x | ||
142 | y = self.y + other.y | 142 | y = self.y + other.y | ||
143 | z = self.z + other.z | 143 | z = self.z + other.z | ||
144 | return pt3d(x, y,z) | 144 | return pt3d(x, y,z) | ||
145 | def __sub__(self, other): | 145 | def __sub__(self, other): | ||
146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
147 | def __eq__(self, other): | 147 | def __eq__(self, other): | ||
148 | return other.x==self.x and other.y==self.y and other.z==self.z | 148 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
149 | def __str__(self): | 149 | def __str__(self): | ||
150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
151 | if __name__ == '__main__': | 151 | if __name__ == '__main__': | ||
t | 152 | p1 = pt3d(1, 1, 1) | t | 152 | p_1 = pt3d(1, 1, 1) |
153 | p2 = pt3d(2, 2, 2) | 153 | p_2 = pt3d(2, 2, 2) | ||
154 | print(p1+p2) | 154 | print(p_1+p_2) | ||
155 | print(p1-p2) | 155 | print(p_1-p_2) | ||
156 | print(p1==p2) | 156 | print(p_1==p_2) | ||
157 | print(p1+p1==p2) | 157 | print(p_1+p_1==p_2) | ||
158 | print(p1==p2+pt3d(-1,-1,-1)) | 158 | print(p_1==p_2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,r): | n | 3 | def __init__(self, r): |
4 | self.radius = r | 4 | self.radius = r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.radius**2)*(math.pi) | n | 6 | return (self.radius**2)*(math.pi) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*(math.pi)*(self.radius) | n | 8 | return 2*(math.pi)*(self.radius) |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | ||
> | em_description = "none"): | > | em_description = "none"): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | 20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | ||
> | .item_price) + " = $" + | > | .item_price) + " = $" + | ||
21 | str(self.item_price * self.item_quantity)) | 21 | str(self.item_price * self.item_quantity)) | ||
22 | def print_item_description(self): | 22 | def print_item_description(self): | ||
23 | print(self.item_name + ": " + str(self.item_description)) | 23 | print(self.item_name + ": " + str(self.item_description)) | ||
24 | class ShoppingCart: | 24 | class ShoppingCart: | ||
25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | ||
26 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 27 | self.current_date = current_date | ||
28 | self.cart_items = [] | 28 | self.cart_items = [] | ||
29 | def add_item(self, ItemToPurchase): | 29 | def add_item(self, ItemToPurchase): | ||
30 | self.cart_items.append(ItemToPurchase) | 30 | self.cart_items.append(ItemToPurchase) | ||
31 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
32 | RemoveIt = False | 32 | RemoveIt = False | ||
33 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 35 | self.cart_items.remove(item) | ||
36 | RemoveIt = True | 36 | RemoveIt = True | ||
37 | break | 37 | break | ||
38 | if not RemoveIt: | 38 | if not RemoveIt: | ||
39 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
n | 41 | Modify_It = False | n | 41 | ModifyIt = False |
42 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 44 | Modify_It = True | n | 44 | ModifyIt = True |
45 | if ( | 45 | if ( | ||
46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | 46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | ||
> | uantity == 0 and itemToPurchase.item_description == 'none'): | > | uantity == 0 and itemToPurchase.item_description == 'none'): | ||
47 | break | 47 | break | ||
48 | else: | 48 | else: | ||
49 | if (itemToPurchase.item_price != 0): | 49 | if (itemToPurchase.item_price != 0): | ||
50 | self.cart_items[i].item_price = itemToPurchase.item_pric | 50 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||
> | e | > | e | ||
51 | if (itemToPurchase.item_quantity != 0): | 51 | if (itemToPurchase.item_quantity != 0): | ||
52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | 52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | > | uantity | ||
53 | if (itemToPurchase.item_description != 'none'): | 53 | if (itemToPurchase.item_description != 'none'): | ||
54 | self.cart_items[i].item_description = itemToPurchase.ite | 54 | self.cart_items[i].item_description = itemToPurchase.ite | ||
> | m_description | > | m_description | ||
55 | break | 55 | break | ||
n | 56 | if not Modify_It: | n | 56 | if not ModifyIt: |
57 | print('Item not found in cart. Nothing modified.') | 57 | print('Item not found in cart. Nothing modified.') | ||
58 | def get_num_items_in_cart(self): | 58 | def get_num_items_in_cart(self): | ||
59 | num_items = 0 | 59 | num_items = 0 | ||
60 | for item in self.cart_items: | 60 | for item in self.cart_items: | ||
61 | num_items = num_items + item.item_quantity | 61 | num_items = num_items + item.item_quantity | ||
62 | return num_items | 62 | return num_items | ||
63 | def get_cost_of_cart(self): | 63 | def get_cost_of_cart(self): | ||
64 | total_cost = 0 | 64 | total_cost = 0 | ||
65 | cost = 0 | 65 | cost = 0 | ||
66 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
67 | cost = (item.item_quantity * item.item_price) | 67 | cost = (item.item_quantity * item.item_price) | ||
68 | total_cost += cost | 68 | total_cost += cost | ||
69 | return total_cost | 69 | return total_cost | ||
70 | def print_total(self): | 70 | def print_total(self): | ||
71 | total_cost = self.get_cost_of_cart() | 71 | total_cost = self.get_cost_of_cart() | ||
72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | 73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | ||
74 | for item in self.cart_items: | 74 | for item in self.cart_items: | ||
75 | item.print_item_cost() | 75 | item.print_item_cost() | ||
76 | if (total_cost == 0): | 76 | if (total_cost == 0): | ||
77 | print('SHOPPING CART IS EMPTY') | 77 | print('SHOPPING CART IS EMPTY') | ||
78 | print('\nTotal: $%d' % (total_cost)) | 78 | print('\nTotal: $%d' % (total_cost)) | ||
79 | def print_descriptions(self): | 79 | def print_descriptions(self): | ||
80 | if len(self.cart_items) == 0: | 80 | if len(self.cart_items) == 0: | ||
81 | print('SHOPPING CART IS EMPTY') | 81 | print('SHOPPING CART IS EMPTY') | ||
82 | else: | 82 | else: | ||
83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
84 | print('\nItem Descriptions') | 84 | print('\nItem Descriptions') | ||
85 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
86 | item.print_item_description() | 86 | item.print_item_description() | ||
87 | def print_menu(): | 87 | def print_menu(): | ||
88 | print('MENU\n' | 88 | print('MENU\n' | ||
89 | 'a - Add item to cart\n' | 89 | 'a - Add item to cart\n' | ||
90 | 'r - Remove item from cart\n' | 90 | 'r - Remove item from cart\n' | ||
91 | 'c - Change item quantity\n' | 91 | 'c - Change item quantity\n' | ||
92 | "i - Output items' descriptions\n" | 92 | "i - Output items' descriptions\n" | ||
93 | 'o - Output shopping cart\n' | 93 | 'o - Output shopping cart\n' | ||
94 | 'q - Quit\n') | 94 | 'q - Quit\n') | ||
95 | def execute_menu(command, my_cart): | 95 | def execute_menu(command, my_cart): | ||
96 | customer_Cart = my_cart | 96 | customer_Cart = my_cart | ||
97 | if command == 'a': | 97 | if command == 'a': | ||
98 | print("\nADD ITEM TO CART") | 98 | print("\nADD ITEM TO CART") | ||
99 | item_name = input('Enter the item name:\n') | 99 | item_name = input('Enter the item name:\n') | ||
100 | item_description = input('Enter the item description:\n') | 100 | item_description = input('Enter the item description:\n') | ||
101 | item_price = int(input('Enter the item price:\n')) | 101 | item_price = int(input('Enter the item price:\n')) | ||
102 | item_quantity = int(input('Enter the item quantity:\n')) | 102 | item_quantity = int(input('Enter the item quantity:\n')) | ||
103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
104 | customer_Cart.add_item(itemtoPurchase) | 104 | customer_Cart.add_item(itemtoPurchase) | ||
105 | elif command == 'o': | 105 | elif command == 'o': | ||
106 | print('OUTPUT SHOPPING CART') | 106 | print('OUTPUT SHOPPING CART') | ||
107 | customer_Cart.print_total() | 107 | customer_Cart.print_total() | ||
108 | elif command == 'i': | 108 | elif command == 'i': | ||
109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
110 | customer_Cart.print_descriptions() | 110 | customer_Cart.print_descriptions() | ||
111 | elif command == 'r': | 111 | elif command == 'r': | ||
112 | print('REMOVE ITEM FROM CART') | 112 | print('REMOVE ITEM FROM CART') | ||
113 | itemName = input('Enter name of item to remove:\n') | 113 | itemName = input('Enter name of item to remove:\n') | ||
114 | customer_Cart.remove_item(itemName) | 114 | customer_Cart.remove_item(itemName) | ||
115 | elif command == 'c': | 115 | elif command == 'c': | ||
116 | print('\nCHANGE ITEM QUANTITY') | 116 | print('\nCHANGE ITEM QUANTITY') | ||
117 | itemName = input('Enter the item name:\n') | 117 | itemName = input('Enter the item name:\n') | ||
118 | qty = int(input('Enter the new quantity:\n')) | 118 | qty = int(input('Enter the new quantity:\n')) | ||
119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
120 | customer_Cart.modify_item(itemToPurchase) | 120 | customer_Cart.modify_item(itemToPurchase) | ||
121 | if __name__ == "__main__": | 121 | if __name__ == "__main__": | ||
122 | customer_name = input("Enter customer's name:\n") | 122 | customer_name = input("Enter customer's name:\n") | ||
123 | current_date = input("Enter today's date:\n") | 123 | current_date = input("Enter today's date:\n") | ||
124 | print("\nCustomer name: %s" % customer_name) | 124 | print("\nCustomer name: %s" % customer_name) | ||
125 | print("Today's date: %s" % current_date) | 125 | print("Today's date: %s" % current_date) | ||
126 | newCart = ShoppingCart(customer_name, current_date) | 126 | newCart = ShoppingCart(customer_name, current_date) | ||
127 | command = '' | 127 | command = '' | ||
128 | while command != 'q': | 128 | while command != 'q': | ||
129 | print() | 129 | print() | ||
130 | print_menu() | 130 | print_menu() | ||
131 | command = input('Choose an option:\n') | 131 | command = input('Choose an option:\n') | ||
132 | while command != 'a' and command != 'o' and command != 'i' and command ! | 132 | while command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c': | > | = 'q' and command != 'r' and command != 'c': | ||
133 | command = input('Choose an option:\n') | 133 | command = input('Choose an option:\n') | ||
134 | execute_menu(command, newCart)import math | 134 | execute_menu(command, newCart)import math | ||
135 | class pt3d: | 135 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
137 | self.x= x | 137 | self.x= x | ||
138 | self.y= y | 138 | self.y= y | ||
139 | self.z= z | 139 | self.z= z | ||
140 | def __add__(self, other): | 140 | def __add__(self, other): | ||
141 | x = self.x + other.x | 141 | x = self.x + other.x | ||
142 | y = self.y + other.y | 142 | y = self.y + other.y | ||
143 | z = self.z + other.z | 143 | z = self.z + other.z | ||
144 | return pt3d(x, y,z) | 144 | return pt3d(x, y,z) | ||
145 | def __sub__(self, other): | 145 | def __sub__(self, other): | ||
146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
147 | def __eq__(self, other): | 147 | def __eq__(self, other): | ||
148 | return other.x==self.x and other.y==self.y and other.z==self.z | 148 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
149 | def __str__(self): | 149 | def __str__(self): | ||
150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
151 | if __name__ == '__main__': | 151 | if __name__ == '__main__': | ||
t | 152 | p_1 = pt3d(1, 1, 1) | t | 152 | p1 = pt3d(1, 1, 1) |
153 | p_2 = pt3d(2, 2, 2) | 153 | p2 = pt3d(2, 2, 2) | ||
154 | print(p_1+p_2) | 154 | print(p1+p2) | ||
155 | print(p_1-p_2) | 155 | print(p1-p2) | ||
156 | print(p_1==p_2) | 156 | print(p1==p2) | ||
157 | print(p_1+p_1==p_2) | 157 | print(p1+p1==p2) | ||
158 | print(p_1==p_2+pt3d(-1,-1,-1)) | 158 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*(self.radius**2) | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__=='__main__': | n | 9 | if __name__ == '__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
n | 36 | print('Item not found in the cart. Nothing removed.') | n | 37 | print('Item not found in the cart. Nothing removed') |
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
n | 45 | print('Item not found in the cart. Nothing modified.') | n | 46 | print('Item not found in the cart. Nothing modified') |
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
n | 81 | 'r - Remove item from cart\n' | n | 82 | 'r - Remove item from the cart\n' |
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
n | 83 | "i - Output items' descriptions\n" | n | 84 | "i - Output item's descriptions\n" |
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:\n') | n | 90 | command = input('Choose an option:') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
n | 108 | itemName = input('Enter name of item to remove :\n') | n | 109 | itemName = input('Enter the name of the item to remove :\n') |
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter name of item :\n') | n | 113 | itemName = input('Enter the name of the item :\n') |
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart)from math import sqrt | 123 | print_menu(newCart)from math import sqrt | ||
123 | class pt3d: | 124 | class pt3d: | ||
124 | def __init__(self, x, y, z): | 125 | def __init__(self, x, y, z): | ||
125 | self.x = x | 126 | self.x = x | ||
126 | self.y = y | 127 | self.y = y | ||
127 | self.z = z | 128 | self.z = z | ||
128 | def __add__(self, other): | 129 | def __add__(self, other): | ||
129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
130 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
132 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
t | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | t | 134 | return self.x == other.x & self.y == other.y & self.z == other.z |
134 | def __str__(self): | 135 | def __str__(self): | ||
135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
136 | p1 = pt3d(1, 1, 1) | 137 | p1 = pt3d(1, 1, 1) | ||
137 | p2 = pt3d(2, 2, 2) | 138 | p2 = pt3d(2, 2, 2) | ||
138 | print(p1 + p2) | 139 | print(p1 + p2) | ||
139 | print(p1 - p2) | 140 | print(p1 - p2) | ||
140 | print(p1 == p2) | 141 | print(p1 == p2) | ||
141 | print(p1+p1 == p2) | 142 | print(p1+p1 == p2) | ||
142 | print(p1==p2+pt3d(-1, -1, -1)) | 143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self, rad): | 3 | def __init__(self, rad): | ||
n | 4 | self.radius = rad | n | 4 | self.r = rad |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.radius**2)*math.pi | n | 6 | return (self.r**2)*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return self.radius*2*math.pi | n | 8 | return self.r*2*math.pi |
9 | if __name__ == '__main__': | 9 | if __name__ == '__main__': | ||
t | 10 | x = int(input("")) | t | 10 | i = int(input("")) |
11 | NewCircle = circle(x) | 11 | NewCircle = Circle(i) | ||
12 | print(NewCircle.area()) | 12 | print(NewCircle.area()) | ||
13 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 72 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
76 | item.print_item_description() | 76 | item.print_item_description() | ||
77 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
86 | command = '' | 86 | command = '' | ||
87 | while(command != 'q'): | 87 | while(command != 'q'): | ||
88 | print(menu) | 88 | print(menu) | ||
89 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 92 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
101 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
109 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter name of item :\n') | 112 | itemName = input('Enter name of item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) import math | 122 | print_menu(newCart) import math | ||
123 | class pt3d: | 123 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
125 | self.x= x | 125 | self.x= x | ||
126 | self.y= y | 126 | self.y= y | ||
127 | self.z= z | 127 | self.z= z | ||
128 | def __add__(self, other): | 128 | def __add__(self, other): | ||
129 | x = self.x + other.x | 129 | x = self.x + other.x | ||
130 | y = self.y + other.y | 130 | y = self.y + other.y | ||
131 | z = self.z + other.z | 131 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 137 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
139 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
140 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 142 | print(p1+p2) | ||
143 | print(p1-p2) | 143 | print(p1-p2) | ||
144 | print(p1==p2) | 144 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self, rad): | 3 | def __init__(self, rad): | ||
n | 4 | self.r = rad | n | 4 | self.radius = rad |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.r**2)*math.pi | n | 6 | return (self.radius**2)*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return self.r*2*math.pi | n | 8 | return self.radius*2*math.pi |
9 | if __name__ == '__main__': | 9 | if __name__ == '__main__': | ||
t | 10 | i = int(input("")) | t | 10 | x = int(input("")) |
11 | NewCircle = Circle(i) | 11 | NewCircle = circle(x) | ||
12 | print(NewCircle.area()) | 12 | print(NewCircle.area()) | ||
13 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 72 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
76 | item.print_item_description() | 76 | item.print_item_description() | ||
77 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
86 | command = '' | 86 | command = '' | ||
87 | while(command != 'q'): | 87 | while(command != 'q'): | ||
88 | print(menu) | 88 | print(menu) | ||
89 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 92 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
101 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
109 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter name of item :\n') | 112 | itemName = input('Enter name of item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) import math | 122 | print_menu(newCart) import math | ||
123 | class pt3d: | 123 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
125 | self.x= x | 125 | self.x= x | ||
126 | self.y= y | 126 | self.y= y | ||
127 | self.z= z | 127 | self.z= z | ||
128 | def __add__(self, other): | 128 | def __add__(self, other): | ||
129 | x = self.x + other.x | 129 | x = self.x + other.x | ||
130 | y = self.y + other.y | 130 | y = self.y + other.y | ||
131 | z = self.z + other.z | 131 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 137 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
139 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
140 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 142 | print(p1+p2) | ||
143 | print(p1-p2) | 143 | print(p1-p2) | ||
144 | print(p1==p2) | 144 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle(): | n | 2 | class Circle: |
3 | def __init__(self, r): | 3 | def __init__(self, r): | ||
4 | self.radius = r | 4 | self.radius = r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius**2*math.pi | n | 6 | return (self.radius**2)*(math.pi) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*math.pi | n | 8 | return 2*(math.pi)*(self.radius) |
9 | if __name__=='__main__': | ||||
10 | x = int(input()) | ||||
9 | NewCircle = Circle(8) | 11 | NewCircle = Circle(x) | ||
10 | print(NewCircle.area()) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
11 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
12 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | ||
> | em_description = "none"): | > | em_description = "none"): | ||
13 | self.item_name = item_name | 15 | self.item_name = item_name | ||
14 | self.item_price = item_price | 16 | self.item_price = item_price | ||
15 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
16 | self.item_description = item_description | 18 | self.item_description = item_description | ||
17 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
18 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | 20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | ||
> | .item_price) + " = $" + | > | .item_price) + " = $" + | ||
19 | str(self.item_price * self.item_quantity)) | 21 | str(self.item_price * self.item_quantity)) | ||
20 | def print_item_description(self): | 22 | def print_item_description(self): | ||
21 | print(self.item_name + ": " + str(self.item_description)) | 23 | print(self.item_name + ": " + str(self.item_description)) | ||
22 | class ShoppingCart: | 24 | class ShoppingCart: | ||
23 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | ||
24 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 27 | self.current_date = current_date | ||
26 | self.cart_items = [] | 28 | self.cart_items = [] | ||
27 | def add_item(self, ItemToPurchase): | 29 | def add_item(self, ItemToPurchase): | ||
28 | self.cart_items.append(ItemToPurchase) | 30 | self.cart_items.append(ItemToPurchase) | ||
29 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
30 | RemoveIt = False | 32 | RemoveIt = False | ||
31 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 35 | self.cart_items.remove(item) | ||
34 | RemoveIt = True | 36 | RemoveIt = True | ||
35 | break | 37 | break | ||
36 | if not RemoveIt: | 38 | if not RemoveIt: | ||
37 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
38 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
39 | ModifyIt = False | 41 | ModifyIt = False | ||
40 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | ModifyIt = True | 44 | ModifyIt = True | ||
43 | if ( | 45 | if ( | ||
44 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | 46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | ||
> | uantity == 0 and itemToPurchase.item_description == 'none'): | > | uantity == 0 and itemToPurchase.item_description == 'none'): | ||
45 | break | 47 | break | ||
46 | else: | 48 | else: | ||
47 | if (itemToPurchase.item_price != 0): | 49 | if (itemToPurchase.item_price != 0): | ||
48 | self.cart_items[i].item_price = itemToPurchase.item_pric | 50 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||
> | e | > | e | ||
49 | if (itemToPurchase.item_quantity != 0): | 51 | if (itemToPurchase.item_quantity != 0): | ||
50 | self.cart_items[i].item_quantity = itemToPurchase.item_q | 52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | > | uantity | ||
51 | if (itemToPurchase.item_description != 'none'): | 53 | if (itemToPurchase.item_description != 'none'): | ||
52 | self.cart_items[i].item_description = itemToPurchase.ite | 54 | self.cart_items[i].item_description = itemToPurchase.ite | ||
> | m_description | > | m_description | ||
53 | break | 55 | break | ||
54 | if not ModifyIt: | 56 | if not ModifyIt: | ||
55 | print('Item not found in cart. Nothing modified.') | 57 | print('Item not found in cart. Nothing modified.') | ||
n | n | 58 | def get_num_items_in_cart(self): | ||
59 | num_items = 0 | ||||
60 | for item in self.cart_items: | ||||
61 | num_items = num_items + item.item_quantity | ||||
62 | return num_items | ||||
56 | def get_cost_of_cart(self): | 63 | def get_cost_of_cart(self): | ||
57 | total_cost = 0 | 64 | total_cost = 0 | ||
58 | cost = 0 | 65 | cost = 0 | ||
59 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
60 | cost = (item.item_quantity * item.item_price) | 67 | cost = (item.item_quantity * item.item_price) | ||
61 | total_cost += cost | 68 | total_cost += cost | ||
62 | return total_cost | 69 | return total_cost | ||
n | 63 | def get_num_items_in_cart(self): | n | ||
64 | num_items = 0 | ||||
65 | for item in self.cart_items: | ||||
66 | num_items = num_items + item.item_quantity | ||||
67 | return num_items | ||||
68 | def print_total(self): | 70 | def print_total(self): | ||
69 | total_cost = self.get_cost_of_cart() | 71 | total_cost = self.get_cost_of_cart() | ||
70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
71 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | 73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | ||
72 | for item in self.cart_items: | 74 | for item in self.cart_items: | ||
73 | item.print_item_cost() | 75 | item.print_item_cost() | ||
74 | if (total_cost == 0): | 76 | if (total_cost == 0): | ||
75 | print('SHOPPING CART IS EMPTY') | 77 | print('SHOPPING CART IS EMPTY') | ||
76 | print('\nTotal: $%d' % (total_cost)) | 78 | print('\nTotal: $%d' % (total_cost)) | ||
77 | def print_descriptions(self): | 79 | def print_descriptions(self): | ||
78 | if len(self.cart_items) == 0: | 80 | if len(self.cart_items) == 0: | ||
79 | print('SHOPPING CART IS EMPTY') | 81 | print('SHOPPING CART IS EMPTY') | ||
80 | else: | 82 | else: | ||
81 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
82 | print('\nItem Descriptions') | 84 | print('\nItem Descriptions') | ||
83 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
84 | item.print_item_description() | 86 | item.print_item_description() | ||
85 | def print_menu(): | 87 | def print_menu(): | ||
86 | print('MENU\n' | 88 | print('MENU\n' | ||
87 | 'a - Add item to cart\n' | 89 | 'a - Add item to cart\n' | ||
88 | 'r - Remove item from cart\n' | 90 | 'r - Remove item from cart\n' | ||
89 | 'c - Change item quantity\n' | 91 | 'c - Change item quantity\n' | ||
90 | "i - Output items' descriptions\n" | 92 | "i - Output items' descriptions\n" | ||
91 | 'o - Output shopping cart\n' | 93 | 'o - Output shopping cart\n' | ||
92 | 'q - Quit\n') | 94 | 'q - Quit\n') | ||
93 | def execute_menu(command, my_cart): | 95 | def execute_menu(command, my_cart): | ||
94 | customer_Cart = my_cart | 96 | customer_Cart = my_cart | ||
95 | if command == 'a': | 97 | if command == 'a': | ||
96 | print("\nADD ITEM TO CART") | 98 | print("\nADD ITEM TO CART") | ||
97 | item_name = input('Enter the item name:\n') | 99 | item_name = input('Enter the item name:\n') | ||
98 | item_description = input('Enter the item description:\n') | 100 | item_description = input('Enter the item description:\n') | ||
99 | item_price = int(input('Enter the item price:\n')) | 101 | item_price = int(input('Enter the item price:\n')) | ||
100 | item_quantity = int(input('Enter the item quantity:\n')) | 102 | item_quantity = int(input('Enter the item quantity:\n')) | ||
101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
102 | customer_Cart.add_item(itemtoPurchase) | 104 | customer_Cart.add_item(itemtoPurchase) | ||
n | n | 105 | elif command == 'o': | ||
106 | print('OUTPUT SHOPPING CART') | ||||
107 | customer_Cart.print_total() | ||||
108 | elif command == 'i': | ||||
109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||||
110 | customer_Cart.print_descriptions() | ||||
103 | elif command == 'r': | 111 | elif command == 'r': | ||
104 | print('REMOVE ITEM FROM CART') | 112 | print('REMOVE ITEM FROM CART') | ||
105 | itemName = input('Enter name of item to remove:\n') | 113 | itemName = input('Enter name of item to remove:\n') | ||
106 | customer_Cart.remove_item(itemName) | 114 | customer_Cart.remove_item(itemName) | ||
n | 107 | elif command == 'o': | n | ||
108 | print('OUTPUT SHOPPING CART') | ||||
109 | customer_Cart.print_total() | ||||
110 | elif command == 'c': | 115 | elif command == 'c': | ||
111 | print('\nCHANGE ITEM QUANTITY') | 116 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the item name:\n') | 117 | itemName = input('Enter the item name:\n') | ||
113 | qty = int(input('Enter the new quantity:\n')) | 118 | qty = int(input('Enter the new quantity:\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
t | 115 | customer_Cart.modify_item(itemToPurchase) | t | 120 | customer_Cart.modify_item(itemToPurchase) |
116 | elif command == 'i': | ||||
117 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||||
118 | customer_Cart.print_descriptions() | ||||
119 | if __name__ == "__main__": | 121 | if __name__ == "__main__": | ||
120 | customer_name = input("Enter customer's name:\n") | 122 | customer_name = input("Enter customer's name:\n") | ||
121 | current_date = input("Enter today's date:\n") | 123 | current_date = input("Enter today's date:\n") | ||
122 | print("\nCustomer name: %s" % customer_name) | 124 | print("\nCustomer name: %s" % customer_name) | ||
123 | print("Today's date: %s" % current_date) | 125 | print("Today's date: %s" % current_date) | ||
124 | newCart = ShoppingCart(customer_name, current_date) | 126 | newCart = ShoppingCart(customer_name, current_date) | ||
125 | command = '' | 127 | command = '' | ||
126 | while command != 'q': | 128 | while command != 'q': | ||
127 | print() | 129 | print() | ||
128 | print_menu() | 130 | print_menu() | ||
129 | command = input('Choose an option:\n') | 131 | command = input('Choose an option:\n') | ||
130 | while command != 'a' and command != 'o' and command != 'i' and command ! | 132 | while command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c': | > | = 'q' and command != 'r' and command != 'c': | ||
131 | command = input('Choose an option:\n') | 133 | command = input('Choose an option:\n') | ||
132 | execute_menu(command, newCart)import math | 134 | execute_menu(command, newCart)import math | ||
133 | class pt3d: | 135 | class pt3d: | ||
134 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
135 | self.x= x | 137 | self.x= x | ||
136 | self.y= y | 138 | self.y= y | ||
137 | self.z= z | 139 | self.z= z | ||
138 | def __add__(self, other): | 140 | def __add__(self, other): | ||
139 | x = self.x + other.x | 141 | x = self.x + other.x | ||
140 | y = self.y + other.y | 142 | y = self.y + other.y | ||
141 | z = self.z + other.z | 143 | z = self.z + other.z | ||
142 | return pt3d(x, y,z) | 144 | return pt3d(x, y,z) | ||
143 | def __sub__(self, other): | 145 | def __sub__(self, other): | ||
144 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
145 | def __eq__(self, other): | 147 | def __eq__(self, other): | ||
146 | return other.x==self.x and other.y==self.y and other.z==self.z | 148 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
147 | def __str__(self): | 149 | def __str__(self): | ||
148 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
149 | if __name__ == '__main__': | 151 | if __name__ == '__main__': | ||
150 | p1 = pt3d(1, 1, 1) | 152 | p1 = pt3d(1, 1, 1) | ||
151 | p2 = pt3d(2, 2, 2) | 153 | p2 = pt3d(2, 2, 2) | ||
152 | print(p1+p2) | 154 | print(p1+p2) | ||
153 | print(p1-p2) | 155 | print(p1-p2) | ||
154 | print(p1==p2) | 156 | print(p1==p2) | ||
155 | print(p1+p1==p2) | 157 | print(p1+p1==p2) | ||
156 | print(p1==p2+pt3d(-1,-1,-1)) | 158 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self,radius=0): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius ** 2 | n | 6 | return self.radius**2*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return 2*self.radius*math.pi |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) | n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: |
14 | class ItemToPurchase: | ||||
15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
16 | self.item_name=name | 15 | self.item_name=name | ||
17 | self.item_description=description | 16 | self.item_description=description | ||
18 | self.item_price=price | 17 | self.item_price=price | ||
19 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
20 | def print_item_description(self): | 19 | def print_item_description(self): | ||
21 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
22 | class ShoppingCart: | 21 | class ShoppingCart: | ||
23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
24 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 24 | self.current_date = current_date | ||
26 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
27 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
28 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
29 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
30 | tremove_item = False | 29 | tremove_item = False | ||
31 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 33 | tremove_item = True | ||
35 | break | 34 | break | ||
36 | if not tremove_item: | 35 | if not tremove_item: | ||
37 | print('Item not found in the cart. Nothing removed') | 36 | print('Item not found in the cart. Nothing removed') | ||
38 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 38 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 41 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 43 | break | ||
45 | if not tmodify_item: | 44 | if not tmodify_item: | ||
46 | print('Item not found in the cart. Nothing modified') | 45 | print('Item not found in the cart. Nothing modified') | ||
47 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 47 | num_items = 0 | ||
49 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 50 | return num_items | ||
52 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 52 | total_cost = 0 | ||
54 | cost = 0 | 53 | cost = 0 | ||
55 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 56 | total_cost += cost | ||
58 | return total_cost | 57 | return total_cost | ||
59 | def print_total(self): | 58 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 62 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
75 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from the cart\n' | 81 | 'r - Remove item from the cart\n' | ||
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
84 | "i - Output item's descriptions\n" | 83 | "i - Output item's descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
90 | command = input('Choose an option:') | 89 | command = input('Choose an option:') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
102 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
109 | itemName = input('Enter the name of the item to remove :\n') | 108 | itemName = input('Enter the name of the item to remove :\n') | ||
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
113 | itemName = input('Enter the name of the item :\n') | 112 | itemName = input('Enter the name of the item :\n') | ||
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
123 | print_menu(newCart) | 122 | print_menu(newCart) | ||
124 | import math | 123 | import math | ||
125 | from math import sqrt | 124 | from math import sqrt | ||
126 | class pt3d(): | 125 | class pt3d(): | ||
127 | def __init__(self,x,y,z): | 126 | def __init__(self,x,y,z): | ||
n | 128 | self.x = x | n | 127 | self.x= x |
129 | self.y = y | 128 | self.y= y | ||
130 | self.z = z | 129 | self.z= z | ||
131 | def __add__(self, other): | 130 | def __add__(self, other): | ||
t | 132 | sumX = self.x + other.x | t | 131 | sumx = self.x + other.x |
133 | sumY = self.y + other.y | 132 | sumy = self.y + other.y | ||
134 | sumZ = self.z + other.z | 133 | sumz = self.z + other.z | ||
135 | return pt3d(sumX, sumY,sumZ) | 134 | return pt3d(sumx, sumy,sumz) | ||
136 | def __sub__(self, other): | 135 | def __sub__(self, other): | ||
137 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | 136 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | ||
> | self.z)**2))) | > | self.z)**2))) | ||
138 | def __eq__(self, other): | 137 | def __eq__(self, other): | ||
139 | equalcheck = True | 138 | equalcheck = True | ||
140 | if self.x != other.x and self.y != other.y and self.z != other.z: | 139 | if self.x != other.x and self.y != other.y and self.z != other.z: | ||
141 | equalcheck = False | 140 | equalcheck = False | ||
142 | else: | 141 | else: | ||
143 | equalcheck = True | 142 | equalcheck = True | ||
144 | return equalcheck | 143 | return equalcheck | ||
145 | def __str__(self): | 144 | def __str__(self): | ||
146 | return f'<{self.x},{self.y},{self.z}>' | 145 | return f'<{self.x},{self.y},{self.z}>' | ||
147 | def main(): | 146 | def main(): | ||
148 | a = pt3d(1, 1, 1) | 147 | a = pt3d(1, 1, 1) | ||
149 | b = pt3d(2, 2, 2) | 148 | b = pt3d(2, 2, 2) | ||
150 | print(a+b) | 149 | print(a+b) | ||
151 | print(b-a) | 150 | print(b-a) | ||
152 | a==b | 151 | a==b | ||
153 | a+a==b | 152 | a+a==b | ||
154 | a==b+pt3d(-1,-1,-1) | 153 | a==b+pt3d(-1,-1,-1) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius=0): | n | 3 | def __init__(self,radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius**2*math.pi | n | 6 | return math.pi * self.radius ** 2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*math.pi | n | 8 | return 2 * math.pi * self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from the cart\n' | 82 | 'r - Remove item from the cart\n' | ||
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output item's descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
89 | command = input('Choose an option:') | 90 | command = input('Choose an option:') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) | 123 | print_menu(newCart) | ||
123 | import math | 124 | import math | ||
124 | from math import sqrt | 125 | from math import sqrt | ||
125 | class pt3d(): | 126 | class pt3d(): | ||
126 | def __init__(self,x,y,z): | 127 | def __init__(self,x,y,z): | ||
n | 127 | self.x= x | n | 128 | self.x = x |
128 | self.y= y | 129 | self.y = y | ||
129 | self.z= z | 130 | self.z = z | ||
130 | def __add__(self, other): | 131 | def __add__(self, other): | ||
t | 131 | sumx = self.x + other.x | t | 132 | sumX = self.x + other.x |
132 | sumy = self.y + other.y | 133 | sumY = self.y + other.y | ||
133 | sumz = self.z + other.z | 134 | sumZ = self.z + other.z | ||
134 | return pt3d(sumx, sumy,sumz) | 135 | return pt3d(sumX, sumY,sumZ) | ||
135 | def __sub__(self, other): | 136 | def __sub__(self, other): | ||
136 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | 137 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | ||
> | self.z)**2))) | > | self.z)**2))) | ||
137 | def __eq__(self, other): | 138 | def __eq__(self, other): | ||
138 | equalcheck = True | 139 | equalcheck = True | ||
139 | if self.x != other.x and self.y != other.y and self.z != other.z: | 140 | if self.x != other.x and self.y != other.y and self.z != other.z: | ||
140 | equalcheck = False | 141 | equalcheck = False | ||
141 | else: | 142 | else: | ||
142 | equalcheck = True | 143 | equalcheck = True | ||
143 | return equalcheck | 144 | return equalcheck | ||
144 | def __str__(self): | 145 | def __str__(self): | ||
145 | return f'<{self.x},{self.y},{self.z}>' | 146 | return f'<{self.x},{self.y},{self.z}>' | ||
146 | def main(): | 147 | def main(): | ||
147 | a = pt3d(1, 1, 1) | 148 | a = pt3d(1, 1, 1) | ||
148 | b = pt3d(2, 2, 2) | 149 | b = pt3d(2, 2, 2) | ||
149 | print(a+b) | 150 | print(a+b) | ||
150 | print(b-a) | 151 | print(b-a) | ||
151 | a==b | 152 | a==b | ||
152 | a+a==b | 153 | a+a==b | ||
153 | a==b+pt3d(-1,-1,-1) | 154 | a==b+pt3d(-1,-1,-1) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self): | n | 3 | def __init__(self, radius): |
4 | self.radius = radius | ||||
4 | def area(self): | 5 | def area(self): | ||
n | n | 6 | return math.pi*(self.radius**2) | ||
5 | def perimeter(self): | 7 | def perimeter(self): | ||
n | n | 8 | return 2*math.pi*self.radius | ||
6 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
7 | x = int(input()) | 10 | x = int(input()) | ||
8 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
9 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
10 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
11 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
12 | self.item_name=name | 15 | self.item_name=name | ||
13 | self.item_description=description | 16 | self.item_description=description | ||
14 | self.item_price=price | 17 | self.item_price=price | ||
15 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
16 | def print_item_description(self): | 19 | def print_item_description(self): | ||
17 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
18 | class ShoppingCart: | 21 | class ShoppingCart: | ||
19 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
20 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
21 | self.current_date = current_date | 24 | self.current_date = current_date | ||
22 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
23 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
24 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
25 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
26 | tremove_item = False | 29 | tremove_item = False | ||
27 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
28 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
29 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
30 | tremove_item = True | 33 | tremove_item = True | ||
31 | break | 34 | break | ||
32 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 33 | print('Item not found in the cart. Nothing removed') | n | 36 | print('Item not found in the cart. Nothing removed.') |
34 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
35 | tmodify_item = False | 38 | tmodify_item = False | ||
36 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
37 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
38 | tmodify_item = True | 41 | tmodify_item = True | ||
39 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
40 | break | 43 | break | ||
41 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 42 | print('Item not found in the cart. Nothing modified') | n | 45 | print('Item not found in the cart. Nothing modified.') |
43 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
44 | num_items = 0 | 47 | num_items = 0 | ||
45 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
46 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
47 | return num_items | 50 | return num_items | ||
48 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
49 | total_cost = 0 | 52 | total_cost = 0 | ||
50 | cost = 0 | 53 | cost = 0 | ||
51 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
52 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
53 | total_cost += cost | 56 | total_cost += cost | ||
54 | return total_cost | 57 | return total_cost | ||
55 | def print_total(self): | 58 | def print_total(self): | ||
56 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
57 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
58 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
59 | else: | 62 | else: | ||
60 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
61 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
62 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
63 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
64 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
65 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
66 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
67 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
68 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
69 | else: | 72 | else: | ||
70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
71 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
72 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
73 | item.print_item_description() | 76 | item.print_item_description() | ||
74 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
75 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
76 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
77 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
78 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
79 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
80 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
81 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
82 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
83 | command = '' | 86 | command = '' | ||
84 | while(command != 'q'): | 87 | while(command != 'q'): | ||
85 | print(menu) | 88 | print(menu) | ||
86 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
87 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
88 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
89 | if(command == 'a'): | 92 | if(command == 'a'): | ||
90 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
91 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
92 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
93 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
94 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
95 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
96 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
97 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
98 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
99 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
100 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
101 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
102 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
103 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
104 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
105 | itemName = input('Enter name of item to remove :\n') | 108 | itemName = input('Enter name of item to remove :\n') | ||
106 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
107 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
108 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
109 | itemName = input('Enter name of item :\n') | 112 | itemName = input('Enter name of item :\n') | ||
n | 110 | qty = int(input('Enter new quantity :\n')) | n | 113 | qty = int(input('Enter the new quantity :\n')) |
111 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
112 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
113 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
114 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
115 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
116 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
117 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
118 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 119 | print_menu(newCart) class pt3d: | n | 122 | print_menu(newCart)from math import sqrt |
123 | class pt3d: | ||||
120 | def __init__(self, x, y, z): | 124 | def __init__(self, x, y, z): | ||
121 | self.x = x | 125 | self.x = x | ||
122 | self.y = y | 126 | self.y = y | ||
123 | self.z = z | 127 | self.z = z | ||
124 | def __add__(self, other): | 128 | def __add__(self, other): | ||
125 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
126 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
127 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
128 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
t | 129 | return self.x == other.x & self.y == other.y & self.z == other.z | t | 133 | return self.x == other.x and self.y == other.y and self.z == other.z |
130 | def __str__(self): | 134 | def __str__(self): | ||
131 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
132 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
133 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
134 | print(p1 + p2) | 138 | print(p1 + p2) | ||
135 | print(p1 - p2) | 139 | print(p1 - p2) | ||
136 | print(p1 == p2) | 140 | print(p1 == p2) | ||
137 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
138 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle : | n | 2 | class Circle: |
3 | def __init__(self,radius): | 3 | def __init__(self, rad): | ||
4 | self.radius = radius | 4 | self.radius = rad | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*self.radius*self.radius | n | 6 | return (self.radius**2)*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 8 | return self.radius*2*math.pi |
9 | if __name__ == '__main__': | 9 | if __name__ == '__main__': | ||
n | 10 | x = int(input()) | n | 10 | x = int(input("")) |
11 | NewCircle = Circle(x) | 11 | NewCircle = circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print(NewCircle.area()) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter())) | 13 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | class ItemToPurchase: | ||||
15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
16 | self.item_name=name | 15 | self.item_name=name | ||
17 | self.item_description=description | 16 | self.item_description=description | ||
18 | self.item_price=price | 17 | self.item_price=price | ||
19 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
20 | def print_item_description(self): | 19 | def print_item_description(self): | ||
21 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
22 | class ShoppingCart: | 21 | class ShoppingCart: | ||
23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
24 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 24 | self.current_date = current_date | ||
26 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
27 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
28 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
29 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
30 | tremove_item = False | 29 | tremove_item = False | ||
31 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 33 | tremove_item = True | ||
35 | break | 34 | break | ||
36 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 37 | print('Item not found in the cart. Nothing removed') | n | 36 | print('Item not found in cart. Nothing removed.') |
38 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 38 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 41 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 43 | break | ||
45 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 46 | print('Item not found in the cart. Nothing modified') | n | 45 | print('Item not found in cart. Nothing modified.') |
47 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 47 | num_items = 0 | ||
49 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 50 | return num_items | ||
52 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 52 | total_cost = 0 | ||
54 | cost = 0 | 53 | cost = 0 | ||
55 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 56 | total_cost += cost | ||
58 | return total_cost | 57 | return total_cost | ||
59 | def print_total(self): | 58 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 62 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
75 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
84 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
90 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
n | 102 | print('\nOUTPUT SHOPPING CART') | n | 101 | print('OUTPUT SHOPPING CART') |
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 109 | itemName = input('Enter the name of the item to remove :\n') | n | 108 | itemName = input('Enter name of item to remove:\n') |
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 113 | itemName = input('Enter the name of the item :\n') | n | 112 | itemName = input('Enter name of item :\n') |
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 123 | print_menu(newCart)import math | t | 122 | print_menu(newCart) import math |
124 | class pt3d: | 123 | class pt3d: | ||
125 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
126 | self.x= x | 125 | self.x= x | ||
127 | self.y= y | 126 | self.y= y | ||
128 | self.z= z | 127 | self.z= z | ||
129 | def __add__(self, other): | 128 | def __add__(self, other): | ||
130 | x = self.x + other.x | 129 | x = self.x + other.x | ||
131 | y = self.y + other.y | 130 | y = self.y + other.y | ||
132 | z = self.z + other.z | 131 | z = self.z + other.z | ||
133 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
134 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
136 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
137 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
138 | def __str__(self): | 137 | def __str__(self): | ||
139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
140 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
141 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
142 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
143 | print(p1+p2) | 142 | print(p1+p2) | ||
144 | print(p1-p2) | 143 | print(p1-p2) | ||
145 | print(p1==p2) | 144 | print(p1==p2) | ||
146 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
147 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,r): | n | 3 | def __init__(self, rad): |
4 | self.rad=r | 4 | self.radius = rad | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.rad*self.rad*3.14159265359 | n | 6 | return (self.radius**2)*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
t | 8 | return self.rad*2*3.14159265359 | t | 8 | return self.radius*2*math.pi |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input("")) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print(NewCircle.area()) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 72 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
76 | item.print_item_description() | 76 | item.print_item_description() | ||
77 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
86 | command = '' | 86 | command = '' | ||
87 | while(command != 'q'): | 87 | while(command != 'q'): | ||
88 | print(menu) | 88 | print(menu) | ||
89 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 92 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
101 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
109 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter name of item :\n') | 112 | itemName = input('Enter name of item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) import math | 122 | print_menu(newCart) import math | ||
123 | class pt3d: | 123 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
125 | self.x= x | 125 | self.x= x | ||
126 | self.y= y | 126 | self.y= y | ||
127 | self.z= z | 127 | self.z= z | ||
128 | def __add__(self, other): | 128 | def __add__(self, other): | ||
129 | x = self.x + other.x | 129 | x = self.x + other.x | ||
130 | y = self.y + other.y | 130 | y = self.y + other.y | ||
131 | z = self.z + other.z | 131 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 137 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
139 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
140 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 142 | print(p1+p2) | ||
143 | print(p1-p2) | 143 | print(p1-p2) | ||
144 | print(p1==p2) | 144 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius): | n | 3 | def __init__(self): |
4 | self.radius = int(radius) | 4 | self.radius=radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return round(math.pi*self.radius*self.radius,3) | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return round(2*math.pi*self.radius,3) | n | 8 | return 2*math.pi*self.radius |
9 | circle=Circle(2) | 9 | if __name__=='__main__': | ||
10 | print("Area of circle=",circle.area()) | 10 | x = int(input()) | ||
11 | print("Perimeter of circle=",circle.perimeter())class ItemToPurchase: | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | ||||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||||
12 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
13 | self.item_name=name | 15 | self.item_name=name | ||
14 | self.item_description=description | 16 | self.item_description=description | ||
15 | self.item_price=price | 17 | self.item_price=price | ||
16 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
17 | def print_item_description(self): | 19 | def print_item_description(self): | ||
18 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
19 | class ShoppingCart: | 21 | class ShoppingCart: | ||
20 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
21 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
22 | self.current_date = current_date | 24 | self.current_date = current_date | ||
23 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
24 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
25 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
26 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
27 | tremove_item = False | 29 | tremove_item = False | ||
28 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
29 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
30 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
31 | tremove_item = True | 33 | tremove_item = True | ||
32 | break | 34 | break | ||
33 | if not tremove_item: | 35 | if not tremove_item: | ||
34 | print('Item not found in the cart. Nothing removed') | 36 | print('Item not found in the cart. Nothing removed') | ||
35 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
36 | tmodify_item = False | 38 | tmodify_item = False | ||
37 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
38 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
39 | tmodify_item = True | 41 | tmodify_item = True | ||
40 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
41 | break | 43 | break | ||
42 | if not tmodify_item: | 44 | if not tmodify_item: | ||
43 | print('Item not found in the cart. Nothing modified') | 45 | print('Item not found in the cart. Nothing modified') | ||
44 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
45 | num_items = 0 | 47 | num_items = 0 | ||
46 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
47 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
48 | return num_items | 50 | return num_items | ||
49 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
50 | total_cost = 0 | 52 | total_cost = 0 | ||
51 | cost = 0 | 53 | cost = 0 | ||
52 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
53 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
54 | total_cost += cost | 56 | total_cost += cost | ||
55 | return total_cost | 57 | return total_cost | ||
56 | def print_total(self): | 58 | def print_total(self): | ||
57 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
58 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
59 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
60 | else: | 62 | else: | ||
61 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
62 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
63 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
64 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
65 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
66 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
67 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
68 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
69 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
70 | else: | 72 | else: | ||
71 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
72 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
73 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
74 | item.print_item_description() | 76 | item.print_item_description() | ||
75 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
76 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
77 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
78 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
79 | 'r - Remove item from the cart\n' | 81 | 'r - Remove item from the cart\n' | ||
80 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
81 | "i - Output item's descriptions\n" | 83 | "i - Output item's descriptions\n" | ||
82 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
83 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
84 | command = '' | 86 | command = '' | ||
85 | while(command != 'q'): | 87 | while(command != 'q'): | ||
86 | print(menu) | 88 | print(menu) | ||
87 | command = input('Choose an option:') | 89 | command = input('Choose an option:') | ||
88 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
89 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
90 | if(command == 'a'): | 92 | if(command == 'a'): | ||
91 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
92 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
93 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
94 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
95 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
96 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
97 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
98 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
99 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
100 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
101 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
102 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
103 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
104 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
105 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
106 | itemName = input('Enter the name of the item to remove :\n') | 108 | itemName = input('Enter the name of the item to remove :\n') | ||
107 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
108 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
109 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
110 | itemName = input('Enter the name of the item :\n') | 112 | itemName = input('Enter the name of the item :\n') | ||
111 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
112 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
113 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
114 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
115 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
116 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
117 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
118 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
119 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 120 | print_menu(newCart)from math import sqrt | t | 122 | print_menu(newCart)from math import sqrt |
121 | class pt3d: | 123 | class pt3d: | ||
122 | def __init__(self, x, y, z): | 124 | def __init__(self, x, y, z): | ||
123 | self.x = x | 125 | self.x = x | ||
124 | self.y = y | 126 | self.y = y | ||
125 | self.z = z | 127 | self.z = z | ||
126 | def __add__(self, other): | 128 | def __add__(self, other): | ||
127 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
128 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
129 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
130 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
131 | return self.x == other.x & self.y == other.y & self.z == other.z | 133 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
132 | def __str__(self): | 134 | def __str__(self): | ||
133 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
134 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
135 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
136 | print(p1 + p2) | 138 | print(p1 + p2) | ||
137 | print(p1 - p2) | 139 | print(p1 - p2) | ||
138 | print(p1 == p2) | 140 | print(p1 == p2) | ||
139 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
140 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class circle(): | n | 2 | class Circle: |
3 | def __init__(self,radius): | 3 | def __init__(self, radius): | ||
4 | self.radius=radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
n | 11 | NewCircle = circle(x) | n | 11 | NewCircle = Circle(x) |
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) | n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: |
14 | class ItemToPurchase: | ||||
15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
16 | self.item_name=name | 15 | self.item_name=name | ||
17 | self.item_description=description | 16 | self.item_description=description | ||
18 | self.item_price=price | 17 | self.item_price=price | ||
19 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
20 | def print_item_description(self): | 19 | def print_item_description(self): | ||
21 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
22 | class ShoppingCart: | 21 | class ShoppingCart: | ||
23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
24 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 24 | self.current_date = current_date | ||
26 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
27 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
28 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
29 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
30 | tremove_item = False | 29 | tremove_item = False | ||
31 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 33 | tremove_item = True | ||
35 | break | 34 | break | ||
36 | if not tremove_item: | 35 | if not tremove_item: | ||
37 | print('Item not found in the cart. Nothing removed.') | 36 | print('Item not found in the cart. Nothing removed.') | ||
38 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 38 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 41 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 43 | break | ||
45 | if not tmodify_item: | 44 | if not tmodify_item: | ||
46 | print('Item not found in the cart. Nothing modified.') | 45 | print('Item not found in the cart. Nothing modified.') | ||
47 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 47 | num_items = 0 | ||
49 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 50 | return num_items | ||
52 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 52 | total_cost = 0 | ||
54 | cost = 0 | 53 | cost = 0 | ||
55 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 56 | total_cost += cost | ||
58 | return total_cost | 57 | return total_cost | ||
59 | def print_total(self): | 58 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 62 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
75 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
84 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
90 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
102 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 109 | itemName = input('Enter name of item to remove:\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 113 | itemName = input('Enter the name of the item :\n') | n | 112 | itemName = input('Enter name of item :\n') |
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 123 | print_menu(newCart) from math import sqrt | n | 122 | print_menu(newCart)from math import sqrt |
124 | class pt3d: | 123 | class pt3d: | ||
n | 125 | def __init__(self,x,y,z): | n | 124 | def __init__(self, x, y, z): |
126 | self.x = x | 125 | self.x = x | ||
127 | self.y = y | 126 | self.y = y | ||
128 | self.z = z | 127 | self.z = z | ||
129 | def __add__(self, other): | 128 | def __add__(self, other): | ||
130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
131 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
133 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
134 | return self.x == other.x and self.y == other.y and self.z == other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
135 | def __str__(self): | 134 | def __str__(self): | ||
t | 136 | myStr = "<"+str(self.x) +","+ str(self.y) + ","+ str(self.z)+">" | t | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
137 | return myStr | ||||
138 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
139 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
140 | print(p1 + p2) | 138 | print(p1 + p2) | ||
141 | print(p1 - p2) | 139 | print(p1 - p2) | ||
142 | print(p1 == p2) | 140 | print(p1 == p2) | ||
143 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
144 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | class circle(): | n | 1 | class Circle(): |
2 | def __init__(self, radius): | 2 | def __init__(self, radius): | ||
3 | self.radius = radius | 3 | self.radius = radius | ||
4 | def area(self): | 4 | def area(self): | ||
5 | return 3.14 * self.radius * self.radius | 5 | return 3.14 * self.radius * self.radius | ||
6 | def perimeter(self): | 6 | def perimeter(self): | ||
n | 7 | return 2 * 3.14 * self.radius | n | 7 | return 2*self.radius*3.14 |
8 | if __name__ == '__main__': | ||||
9 | x = int(input("")) | ||||
10 | NewCircle = circle(x) | 8 | NewCircle = Circle(8) | ||
11 | print(NewCircle.area()) | 9 | print(NewCircle.area()) | ||
12 | print(NewCircle.perimeter())class ItemToPurchase: | 10 | print(NewCircle.perimeter()) | ||
11 | class ItemToPurchase: | ||||
13 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 12 | def __init__ (self, name= "none", price = 0, quantity = 0, description="none" | ||
> | ): | ||||
14 | self.item_name=name | 13 | self.item_name=name | ||
15 | self.item_description=description | 14 | self.item_description=description | ||
16 | self.item_price=price | 15 | self.item_price=price | ||
17 | self.item_quantity=quantity | 16 | self.item_quantity=quantity | ||
18 | def print_item_description(self): | 17 | def print_item_description(self): | ||
19 | print('%s: %s' % (self.item_name, self.item_description)) | 18 | print('%s: %s' % (self.item_name, self.item_description)) | ||
20 | class ShoppingCart: | 19 | class ShoppingCart: | ||
21 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 20 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
22 | self.customer_name = customer_name | 21 | self.customer_name = customer_name | ||
23 | self.current_date = current_date | 22 | self.current_date = current_date | ||
24 | self.cart_items = cart_items | 23 | self.cart_items = cart_items | ||
25 | def add_item(self, itemToPurchase): | 24 | def add_item(self, itemToPurchase): | ||
26 | self.cart_items.append(itemToPurchase) | 25 | self.cart_items.append(itemToPurchase) | ||
27 | def remove_item(self, itemName): | 26 | def remove_item(self, itemName): | ||
28 | tremove_item = False | 27 | tremove_item = False | ||
29 | for item in self.cart_items: | 28 | for item in self.cart_items: | ||
30 | if item.item_name == itemName: | 29 | if item.item_name == itemName: | ||
31 | self.cart_items.remove(item) | 30 | self.cart_items.remove(item) | ||
32 | tremove_item = True | 31 | tremove_item = True | ||
33 | break | 32 | break | ||
34 | if not tremove_item: | 33 | if not tremove_item: | ||
35 | print('Item not found in the cart. Nothing removed') | 34 | print('Item not found in the cart. Nothing removed') | ||
36 | def modify_item(self, itemToPurchase): | 35 | def modify_item(self, itemToPurchase): | ||
37 | tmodify_item = False | 36 | tmodify_item = False | ||
38 | for i in range(len(self.cart_items)): | 37 | for i in range(len(self.cart_items)): | ||
39 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 38 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
40 | tmodify_item = True | 39 | tmodify_item = True | ||
41 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 40 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
42 | break | 41 | break | ||
43 | if not tmodify_item: | 42 | if not tmodify_item: | ||
44 | print('Item not found in the cart. Nothing modified') | 43 | print('Item not found in the cart. Nothing modified') | ||
45 | def get_num_items_in_cart(self): | 44 | def get_num_items_in_cart(self): | ||
46 | num_items = 0 | 45 | num_items = 0 | ||
47 | for item in self.cart_items: | 46 | for item in self.cart_items: | ||
48 | num_items = num_items + item.item_quantity | 47 | num_items = num_items + item.item_quantity | ||
49 | return num_items | 48 | return num_items | ||
50 | def get_cost_of_cart(self): | 49 | def get_cost_of_cart(self): | ||
51 | total_cost = 0 | 50 | total_cost = 0 | ||
52 | cost = 0 | 51 | cost = 0 | ||
53 | for item in self.cart_items: | 52 | for item in self.cart_items: | ||
54 | cost = (item.item_quantity * item.item_price) | 53 | cost = (item.item_quantity * item.item_price) | ||
55 | total_cost += cost | 54 | total_cost += cost | ||
56 | return total_cost | 55 | return total_cost | ||
57 | def print_total(self): | 56 | def print_total(self): | ||
58 | total_cost = self.get_cost_of_cart() | 57 | total_cost = self.get_cost_of_cart() | ||
59 | if (total_cost == 0): | 58 | if (total_cost == 0): | ||
60 | print('SHOPPING CART IS EMPTY') | 59 | print('SHOPPING CART IS EMPTY') | ||
61 | else: | 60 | else: | ||
62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 61 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 62 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
64 | for item in self.cart_items: | 63 | for item in self.cart_items: | ||
65 | total = item.item_price * item.item_quantity | 64 | total = item.item_price * item.item_quantity | ||
66 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 65 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
67 | print('\nTotal: $%d' %(total_cost)) | 66 | print('\nTotal: $%d' %(total_cost)) | ||
68 | def print_descriptions(self): | 67 | def print_descriptions(self): | ||
69 | if len(self.cart_items) == 0: | 68 | if len(self.cart_items) == 0: | ||
70 | print('SHOPPING CART IS EMPTY') | 69 | print('SHOPPING CART IS EMPTY') | ||
71 | else: | 70 | else: | ||
72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 71 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
73 | print('\nItem Descriptions') | 72 | print('\nItem Descriptions') | ||
74 | for item in self.cart_items: | 73 | for item in self.cart_items: | ||
75 | item.print_item_description() | 74 | item.print_item_description() | ||
76 | def print_menu(newCart): | 75 | def print_menu(newCart): | ||
77 | customer_Cart = newCart | 76 | customer_Cart = newCart | ||
78 | menu = ('\nMENU\n' | 77 | menu = ('\nMENU\n' | ||
79 | 'a - Add item to cart\n' | 78 | 'a - Add item to cart\n' | ||
80 | 'r - Remove item from the cart\n' | 79 | 'r - Remove item from the cart\n' | ||
81 | 'c - Change item quantity\n' | 80 | 'c - Change item quantity\n' | ||
82 | "i - Output item's descriptions\n" | 81 | "i - Output item's descriptions\n" | ||
83 | 'o - Output shopping cart\n' | 82 | 'o - Output shopping cart\n' | ||
84 | 'q - Quit\n') | 83 | 'q - Quit\n') | ||
85 | command = '' | 84 | command = '' | ||
86 | while(command != 'q'): | 85 | while(command != 'q'): | ||
87 | print(menu) | 86 | print(menu) | ||
88 | command = input('Choose an option:') | 87 | command = input('Choose an option:') | ||
89 | while(command != 'a' and command != 'o' and command != 'i' and command != | 88 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
90 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
91 | if(command == 'a'): | 90 | if(command == 'a'): | ||
92 | print("\nADD ITEM TO CART") | 91 | print("\nADD ITEM TO CART") | ||
93 | item_name = input('Enter the item name:\n') | 92 | item_name = input('Enter the item name:\n') | ||
94 | item_description = input('Enter the item description:\n') | 93 | item_description = input('Enter the item description:\n') | ||
95 | item_price = int(input('Enter the item price:\n')) | 94 | item_price = int(input('Enter the item price:\n')) | ||
96 | item_quantity = int(input('Enter the item quantity:\n')) | 95 | item_quantity = int(input('Enter the item quantity:\n')) | ||
97 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 96 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
98 | customer_Cart.add_item(itemtoPurchase) | 97 | customer_Cart.add_item(itemtoPurchase) | ||
99 | elif(command == 'o'): | 98 | elif(command == 'o'): | ||
100 | print('\nOUTPUT SHOPPING CART') | 99 | print('\nOUTPUT SHOPPING CART') | ||
101 | customer_Cart.print_total() | 100 | customer_Cart.print_total() | ||
102 | elif(command == 'i'): | 101 | elif(command == 'i'): | ||
103 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 102 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
104 | customer_Cart.print_descriptions() | 103 | customer_Cart.print_descriptions() | ||
105 | elif(command == 'r'): | 104 | elif(command == 'r'): | ||
106 | print('REMOVE ITEM FROM CART') | 105 | print('REMOVE ITEM FROM CART') | ||
107 | itemName = input('Enter the name of the item to remove :\n') | 106 | itemName = input('Enter the name of the item to remove :\n') | ||
108 | customer_Cart.remove_item(itemName) | 107 | customer_Cart.remove_item(itemName) | ||
109 | elif(command == 'c'): | 108 | elif(command == 'c'): | ||
110 | print('\nCHANGE ITEM QUANTITY') | 109 | print('\nCHANGE ITEM QUANTITY') | ||
111 | itemName = input('Enter the name of the item :\n') | 110 | itemName = input('Enter the name of the item :\n') | ||
112 | qty = int(input('Enter the new quantity :\n')) | 111 | qty = int(input('Enter the new quantity :\n')) | ||
113 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 112 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
114 | customer_Cart.modify_item(itemToPurchase) | 113 | customer_Cart.modify_item(itemToPurchase) | ||
115 | if __name__ == "__main__": | 114 | if __name__ == "__main__": | ||
116 | customer_name = input("Enter customer's name:\n") | 115 | customer_name = input("Enter customer's name:\n") | ||
117 | current_date = input("Enter today's date:\n") | 116 | current_date = input("Enter today's date:\n") | ||
118 | print("\nCustomer name: %s" %customer_name) | 117 | print("\nCustomer name: %s" %customer_name) | ||
119 | print("Today's date: %s" %current_date) | 118 | print("Today's date: %s" %current_date) | ||
120 | newCart = ShoppingCart(customer_name, current_date) | 119 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 121 | print_menu(newCart) | t | 120 | print_menu(newCart) import math |
122 | import math | ||||
123 | class pt3d: | 121 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 122 | def __init__(self,x=0,y=0,z=0): | ||
125 | self.x= x | 123 | self.x= x | ||
126 | self.y= y | 124 | self.y= y | ||
127 | self.z= z | 125 | self.z= z | ||
128 | def __add__(self, other): | 126 | def __add__(self, other): | ||
129 | x = self.x + other.x | 127 | x = self.x + other.x | ||
130 | y = self.y + other.y | 128 | y = self.y + other.y | ||
131 | z = self.z + other.z | 129 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 130 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 132 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 134 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 135 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 136 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
139 | if __name__ == '__main__': | 137 | if __name__ == '__main__': | ||
140 | p1 = pt3d(1, 1, 1) | 138 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 139 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 140 | print(p1+p2) | ||
143 | print(p1-p2) | 141 | print(p1-p2) | ||
144 | print(p1==p2) | 142 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 143 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 144 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | class Circle(): | n | 1 | class circle(): |
2 | def __init__(self, radius): | 2 | def __init__(self, radius): | ||
3 | self.radius = radius | 3 | self.radius = radius | ||
4 | def area(self): | 4 | def area(self): | ||
5 | return 3.14 * self.radius * self.radius | 5 | return 3.14 * self.radius * self.radius | ||
6 | def perimeter(self): | 6 | def perimeter(self): | ||
n | 7 | return 2*self.radius*3.14 | n | 7 | return 2 * 3.14 * self.radius |
8 | if __name__ == '__main__': | ||||
9 | x = int(input("")) | ||||
8 | NewCircle = Circle(8) | 10 | NewCircle = circle(x) | ||
9 | print(NewCircle.area()) | 11 | print(NewCircle.area()) | ||
10 | print(NewCircle.perimeter()) | 12 | print(NewCircle.perimeter())class ItemToPurchase: | ||
11 | class ItemToPurchase: | ||||
12 | def __init__ (self, name= "none", price = 0, quantity = 0, description="none" | 13 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
> | ): | ||||
13 | self.item_name=name | 14 | self.item_name=name | ||
14 | self.item_description=description | 15 | self.item_description=description | ||
15 | self.item_price=price | 16 | self.item_price=price | ||
16 | self.item_quantity=quantity | 17 | self.item_quantity=quantity | ||
17 | def print_item_description(self): | 18 | def print_item_description(self): | ||
18 | print('%s: %s' % (self.item_name, self.item_description)) | 19 | print('%s: %s' % (self.item_name, self.item_description)) | ||
19 | class ShoppingCart: | 20 | class ShoppingCart: | ||
20 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 21 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
21 | self.customer_name = customer_name | 22 | self.customer_name = customer_name | ||
22 | self.current_date = current_date | 23 | self.current_date = current_date | ||
23 | self.cart_items = cart_items | 24 | self.cart_items = cart_items | ||
24 | def add_item(self, itemToPurchase): | 25 | def add_item(self, itemToPurchase): | ||
25 | self.cart_items.append(itemToPurchase) | 26 | self.cart_items.append(itemToPurchase) | ||
26 | def remove_item(self, itemName): | 27 | def remove_item(self, itemName): | ||
27 | tremove_item = False | 28 | tremove_item = False | ||
28 | for item in self.cart_items: | 29 | for item in self.cart_items: | ||
29 | if item.item_name == itemName: | 30 | if item.item_name == itemName: | ||
30 | self.cart_items.remove(item) | 31 | self.cart_items.remove(item) | ||
31 | tremove_item = True | 32 | tremove_item = True | ||
32 | break | 33 | break | ||
33 | if not tremove_item: | 34 | if not tremove_item: | ||
34 | print('Item not found in the cart. Nothing removed') | 35 | print('Item not found in the cart. Nothing removed') | ||
35 | def modify_item(self, itemToPurchase): | 36 | def modify_item(self, itemToPurchase): | ||
36 | tmodify_item = False | 37 | tmodify_item = False | ||
37 | for i in range(len(self.cart_items)): | 38 | for i in range(len(self.cart_items)): | ||
38 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 39 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
39 | tmodify_item = True | 40 | tmodify_item = True | ||
40 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 41 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
41 | break | 42 | break | ||
42 | if not tmodify_item: | 43 | if not tmodify_item: | ||
43 | print('Item not found in the cart. Nothing modified') | 44 | print('Item not found in the cart. Nothing modified') | ||
44 | def get_num_items_in_cart(self): | 45 | def get_num_items_in_cart(self): | ||
45 | num_items = 0 | 46 | num_items = 0 | ||
46 | for item in self.cart_items: | 47 | for item in self.cart_items: | ||
47 | num_items = num_items + item.item_quantity | 48 | num_items = num_items + item.item_quantity | ||
48 | return num_items | 49 | return num_items | ||
49 | def get_cost_of_cart(self): | 50 | def get_cost_of_cart(self): | ||
50 | total_cost = 0 | 51 | total_cost = 0 | ||
51 | cost = 0 | 52 | cost = 0 | ||
52 | for item in self.cart_items: | 53 | for item in self.cart_items: | ||
53 | cost = (item.item_quantity * item.item_price) | 54 | cost = (item.item_quantity * item.item_price) | ||
54 | total_cost += cost | 55 | total_cost += cost | ||
55 | return total_cost | 56 | return total_cost | ||
56 | def print_total(self): | 57 | def print_total(self): | ||
57 | total_cost = self.get_cost_of_cart() | 58 | total_cost = self.get_cost_of_cart() | ||
58 | if (total_cost == 0): | 59 | if (total_cost == 0): | ||
59 | print('SHOPPING CART IS EMPTY') | 60 | print('SHOPPING CART IS EMPTY') | ||
60 | else: | 61 | else: | ||
61 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
62 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
63 | for item in self.cart_items: | 64 | for item in self.cart_items: | ||
64 | total = item.item_price * item.item_quantity | 65 | total = item.item_price * item.item_quantity | ||
65 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 66 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
66 | print('\nTotal: $%d' %(total_cost)) | 67 | print('\nTotal: $%d' %(total_cost)) | ||
67 | def print_descriptions(self): | 68 | def print_descriptions(self): | ||
68 | if len(self.cart_items) == 0: | 69 | if len(self.cart_items) == 0: | ||
69 | print('SHOPPING CART IS EMPTY') | 70 | print('SHOPPING CART IS EMPTY') | ||
70 | else: | 71 | else: | ||
71 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
72 | print('\nItem Descriptions') | 73 | print('\nItem Descriptions') | ||
73 | for item in self.cart_items: | 74 | for item in self.cart_items: | ||
74 | item.print_item_description() | 75 | item.print_item_description() | ||
75 | def print_menu(newCart): | 76 | def print_menu(newCart): | ||
76 | customer_Cart = newCart | 77 | customer_Cart = newCart | ||
77 | menu = ('\nMENU\n' | 78 | menu = ('\nMENU\n' | ||
78 | 'a - Add item to cart\n' | 79 | 'a - Add item to cart\n' | ||
79 | 'r - Remove item from the cart\n' | 80 | 'r - Remove item from the cart\n' | ||
80 | 'c - Change item quantity\n' | 81 | 'c - Change item quantity\n' | ||
81 | "i - Output item's descriptions\n" | 82 | "i - Output item's descriptions\n" | ||
82 | 'o - Output shopping cart\n' | 83 | 'o - Output shopping cart\n' | ||
83 | 'q - Quit\n') | 84 | 'q - Quit\n') | ||
84 | command = '' | 85 | command = '' | ||
85 | while(command != 'q'): | 86 | while(command != 'q'): | ||
86 | print(menu) | 87 | print(menu) | ||
87 | command = input('Choose an option:') | 88 | command = input('Choose an option:') | ||
88 | while(command != 'a' and command != 'o' and command != 'i' and command != | 89 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
89 | command = input('Choose an option:\n') | 90 | command = input('Choose an option:\n') | ||
90 | if(command == 'a'): | 91 | if(command == 'a'): | ||
91 | print("\nADD ITEM TO CART") | 92 | print("\nADD ITEM TO CART") | ||
92 | item_name = input('Enter the item name:\n') | 93 | item_name = input('Enter the item name:\n') | ||
93 | item_description = input('Enter the item description:\n') | 94 | item_description = input('Enter the item description:\n') | ||
94 | item_price = int(input('Enter the item price:\n')) | 95 | item_price = int(input('Enter the item price:\n')) | ||
95 | item_quantity = int(input('Enter the item quantity:\n')) | 96 | item_quantity = int(input('Enter the item quantity:\n')) | ||
96 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 97 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
97 | customer_Cart.add_item(itemtoPurchase) | 98 | customer_Cart.add_item(itemtoPurchase) | ||
98 | elif(command == 'o'): | 99 | elif(command == 'o'): | ||
99 | print('\nOUTPUT SHOPPING CART') | 100 | print('\nOUTPUT SHOPPING CART') | ||
100 | customer_Cart.print_total() | 101 | customer_Cart.print_total() | ||
101 | elif(command == 'i'): | 102 | elif(command == 'i'): | ||
102 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 103 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
103 | customer_Cart.print_descriptions() | 104 | customer_Cart.print_descriptions() | ||
104 | elif(command == 'r'): | 105 | elif(command == 'r'): | ||
105 | print('REMOVE ITEM FROM CART') | 106 | print('REMOVE ITEM FROM CART') | ||
106 | itemName = input('Enter the name of the item to remove :\n') | 107 | itemName = input('Enter the name of the item to remove :\n') | ||
107 | customer_Cart.remove_item(itemName) | 108 | customer_Cart.remove_item(itemName) | ||
108 | elif(command == 'c'): | 109 | elif(command == 'c'): | ||
109 | print('\nCHANGE ITEM QUANTITY') | 110 | print('\nCHANGE ITEM QUANTITY') | ||
110 | itemName = input('Enter the name of the item :\n') | 111 | itemName = input('Enter the name of the item :\n') | ||
111 | qty = int(input('Enter the new quantity :\n')) | 112 | qty = int(input('Enter the new quantity :\n')) | ||
112 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 113 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
113 | customer_Cart.modify_item(itemToPurchase) | 114 | customer_Cart.modify_item(itemToPurchase) | ||
114 | if __name__ == "__main__": | 115 | if __name__ == "__main__": | ||
115 | customer_name = input("Enter customer's name:\n") | 116 | customer_name = input("Enter customer's name:\n") | ||
116 | current_date = input("Enter today's date:\n") | 117 | current_date = input("Enter today's date:\n") | ||
117 | print("\nCustomer name: %s" %customer_name) | 118 | print("\nCustomer name: %s" %customer_name) | ||
118 | print("Today's date: %s" %current_date) | 119 | print("Today's date: %s" %current_date) | ||
119 | newCart = ShoppingCart(customer_name, current_date) | 120 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 120 | print_menu(newCart) import math | t | 121 | print_menu(newCart) |
122 | import math | ||||
121 | class pt3d: | 123 | class pt3d: | ||
122 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
123 | self.x= x | 125 | self.x= x | ||
124 | self.y= y | 126 | self.y= y | ||
125 | self.z= z | 127 | self.z= z | ||
126 | def __add__(self, other): | 128 | def __add__(self, other): | ||
127 | x = self.x + other.x | 129 | x = self.x + other.x | ||
128 | y = self.y + other.y | 130 | y = self.y + other.y | ||
129 | z = self.z + other.z | 131 | z = self.z + other.z | ||
130 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
131 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
132 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
133 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
134 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
135 | def __str__(self): | 137 | def __str__(self): | ||
136 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
137 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
138 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
139 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
140 | print(p1+p2) | 142 | print(p1+p2) | ||
141 | print(p1-p2) | 143 | print(p1-p2) | ||
142 | print(p1==p2) | 144 | print(p1==p2) | ||
143 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
144 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,x): | 3 | def __init__(self,radius): | ||
4 | self.x = x | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*x**2 | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*x | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
n | 81 | 'r - Remove item from the cart\n' | n | 82 | 'r - Remove item from cart\n' |
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
n | 83 | "i - Output item's descriptions\n" | n | 84 | "i - Output items' descriptions\n" |
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:') | n | 90 | command = input('Choose an option:\n') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 122 | print_menu(newCart) import math | n | 123 | print_menu(newCart)import math |
123 | class pt3d: | 124 | class pt3d: | ||
n | 124 | def __init__(self,x,y,z): | n | 125 | def __init__(self,x=0,y=0,z=0): |
125 | self.x= x | 126 | self.x= x | ||
126 | self.y= y | 127 | self.y= y | ||
127 | self.z= z | 128 | self.z= z | ||
t | 128 | def __add__(self, other): | t | 129 | def __add__(self, other): |
129 | x = self.x + other.x | 130 | x = self.x + other.x | ||
130 | y = self.y + other.y | 131 | y = self.y + other.y | ||
131 | z = self.z + other.z | 132 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 133 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 134 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, 2)+m | 135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | ath.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 136 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 137 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 138 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
139 | if __name__ == '__main__': | 140 | if __name__ == '__main__': | ||
140 | p1 = pt3d(1, 1, 1) | 141 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 142 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 143 | print(p1+p2) | ||
143 | print(p1-p2) | 144 | print(p1-p2) | ||
144 | print(p1==p2) | 145 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 146 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 147 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self,radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi * self.radius**2 | 6 | return math.pi * self.radius**2 | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
n | 77 | def print_menu(new_cart): | n | 78 | def print_menu(newCart): |
78 | customer_cart = new_cart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
n | 80 | "a - Add item to cart\n" | n | 81 | 'a - Add item to cart\n' |
81 | "r - Remove item from cart\n" | 82 | 'r - Remove item from the cart\n' | ||
82 | "c - Change item quantity\n" | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
84 | "o - Output shopping cart\n" | 85 | 'o - Output shopping cart\n' | ||
85 | "q - Quit\n") | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:\n') | n | 90 | command = input('Choose an option:') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
n | 102 | customer_cart.print_total() | n | 103 | customer_Cart.print_total() |
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
n | 105 | customer_cart.print_descriptions() | n | 106 | customer_Cart.print_descriptions() |
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
n | 109 | customer_cart.remove_item(itemName) | n | 110 | customer_Cart.remove_item(itemName) |
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
n | 115 | customer_cart.modify_item(itemToPurchase) | n | 116 | customer_Cart.modify_item(itemToPurchase) |
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
t | 121 | new_cart = ShoppingCart(customer_name, current_date) | t | 122 | newCart = ShoppingCart(customer_name, current_date) |
122 | print_menu(new_cart) | 123 | print_menu(newCart)from math import sqrt | ||
123 | from math import sqrt | ||||
124 | class pt3d: | 124 | class pt3d: | ||
125 | def __init__(self, x, y, z): | 125 | def __init__(self, x, y, z): | ||
126 | self.x = x | 126 | self.x = x | ||
127 | self.y = y | 127 | self.y = y | ||
128 | self.z = z | 128 | self.z = z | ||
129 | def __add__(self, other): | 129 | def __add__(self, other): | ||
130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
131 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
133 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
134 | return self.x == other.x & self.y == other.y & self.z == other.z | 134 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
135 | def __str__(self): | 135 | def __str__(self): | ||
136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
137 | p1 = pt3d(1, 1, 1) | 137 | p1 = pt3d(1, 1, 1) | ||
138 | p2 = pt3d(2, 2, 2) | 138 | p2 = pt3d(2, 2, 2) | ||
139 | print(p1 + p2) | 139 | print(p1 + p2) | ||
140 | print(p1 - p2) | 140 | print(p1 - p2) | ||
141 | print(p1 == p2) | 141 | print(p1 == p2) | ||
142 | print(p1+p1 == p2) | 142 | print(p1+p1 == p2) | ||
143 | print(p1==p2+pt3d(-1, -1, -1)) | 143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class circle(): | n | 2 | class Circle: |
3 | def __init__(self,radius): | 3 | def __init__(self, radius): | ||
4 | self.radius=radius | 4 | self.radius = int(radius) | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*(self.radius**2) | n | 6 | return round(math.pi*self.radius*self.radius,3) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 8 | return round(2*math.pi*self.radius,3) |
9 | r=int(input("Enter radius of circle: ")) | 9 | circle=Circle(2) | ||
10 | obj=circle(r) | ||||
11 | print("Area of circle:",round(obj.area(),2)) | 10 | print("Area of circle=",circle.area()) | ||
12 | print("Perimeter of circle:",round(obj.perimeter(),2))class ItemToPurchase: | 11 | print("Perimeter of circle=",circle.perimeter())class ItemToPurchase: | ||
13 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 12 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
14 | self.item_name=name | 13 | self.item_name=name | ||
15 | self.item_description=description | 14 | self.item_description=description | ||
16 | self.item_price=price | 15 | self.item_price=price | ||
17 | self.item_quantity=quantity | 16 | self.item_quantity=quantity | ||
18 | def print_item_description(self): | 17 | def print_item_description(self): | ||
19 | print('%s: %s' % (self.item_name, self.item_description)) | 18 | print('%s: %s' % (self.item_name, self.item_description)) | ||
20 | class ShoppingCart: | 19 | class ShoppingCart: | ||
21 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 20 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
22 | self.customer_name = customer_name | 21 | self.customer_name = customer_name | ||
23 | self.current_date = current_date | 22 | self.current_date = current_date | ||
24 | self.cart_items = cart_items | 23 | self.cart_items = cart_items | ||
25 | def add_item(self, itemToPurchase): | 24 | def add_item(self, itemToPurchase): | ||
26 | self.cart_items.append(itemToPurchase) | 25 | self.cart_items.append(itemToPurchase) | ||
27 | def remove_item(self, itemName): | 26 | def remove_item(self, itemName): | ||
28 | tremove_item = False | 27 | tremove_item = False | ||
29 | for item in self.cart_items: | 28 | for item in self.cart_items: | ||
30 | if item.item_name == itemName: | 29 | if item.item_name == itemName: | ||
31 | self.cart_items.remove(item) | 30 | self.cart_items.remove(item) | ||
32 | tremove_item = True | 31 | tremove_item = True | ||
33 | break | 32 | break | ||
34 | if not tremove_item: | 33 | if not tremove_item: | ||
35 | print('Item not found in the cart. Nothing removed') | 34 | print('Item not found in the cart. Nothing removed') | ||
36 | def modify_item(self, itemToPurchase): | 35 | def modify_item(self, itemToPurchase): | ||
37 | tmodify_item = False | 36 | tmodify_item = False | ||
38 | for i in range(len(self.cart_items)): | 37 | for i in range(len(self.cart_items)): | ||
39 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 38 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
40 | tmodify_item = True | 39 | tmodify_item = True | ||
41 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 40 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
42 | break | 41 | break | ||
43 | if not tmodify_item: | 42 | if not tmodify_item: | ||
44 | print('Item not found in the cart. Nothing modified') | 43 | print('Item not found in the cart. Nothing modified') | ||
45 | def get_num_items_in_cart(self): | 44 | def get_num_items_in_cart(self): | ||
46 | num_items = 0 | 45 | num_items = 0 | ||
47 | for item in self.cart_items: | 46 | for item in self.cart_items: | ||
48 | num_items = num_items + item.item_quantity | 47 | num_items = num_items + item.item_quantity | ||
49 | return num_items | 48 | return num_items | ||
50 | def get_cost_of_cart(self): | 49 | def get_cost_of_cart(self): | ||
51 | total_cost = 0 | 50 | total_cost = 0 | ||
52 | cost = 0 | 51 | cost = 0 | ||
53 | for item in self.cart_items: | 52 | for item in self.cart_items: | ||
54 | cost = (item.item_quantity * item.item_price) | 53 | cost = (item.item_quantity * item.item_price) | ||
55 | total_cost += cost | 54 | total_cost += cost | ||
56 | return total_cost | 55 | return total_cost | ||
57 | def print_total(self): | 56 | def print_total(self): | ||
58 | total_cost = self.get_cost_of_cart() | 57 | total_cost = self.get_cost_of_cart() | ||
59 | if (total_cost == 0): | 58 | if (total_cost == 0): | ||
60 | print('SHOPPING CART IS EMPTY') | 59 | print('SHOPPING CART IS EMPTY') | ||
61 | else: | 60 | else: | ||
62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 61 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 62 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
64 | for item in self.cart_items: | 63 | for item in self.cart_items: | ||
65 | total = item.item_price * item.item_quantity | 64 | total = item.item_price * item.item_quantity | ||
66 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 65 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
67 | print('\nTotal: $%d' %(total_cost)) | 66 | print('\nTotal: $%d' %(total_cost)) | ||
68 | def print_descriptions(self): | 67 | def print_descriptions(self): | ||
69 | if len(self.cart_items) == 0: | 68 | if len(self.cart_items) == 0: | ||
70 | print('SHOPPING CART IS EMPTY') | 69 | print('SHOPPING CART IS EMPTY') | ||
71 | else: | 70 | else: | ||
72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 71 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
73 | print('\nItem Descriptions') | 72 | print('\nItem Descriptions') | ||
74 | for item in self.cart_items: | 73 | for item in self.cart_items: | ||
75 | item.print_item_description() | 74 | item.print_item_description() | ||
76 | def print_menu(newCart): | 75 | def print_menu(newCart): | ||
77 | customer_Cart = newCart | 76 | customer_Cart = newCart | ||
78 | menu = ('\nMENU\n' | 77 | menu = ('\nMENU\n' | ||
79 | 'a - Add item to cart\n' | 78 | 'a - Add item to cart\n' | ||
n | 80 | 'r - Remove item from cart\n' | n | 79 | 'r - Remove item from the cart\n' |
81 | 'c - Change item quantity\n' | 80 | 'c - Change item quantity\n' | ||
n | 82 | "i - Output items' descriptions\n" | n | 81 | "i - Output item's descriptions\n" |
83 | 'o - Output shopping cart\n' | 82 | 'o - Output shopping cart\n' | ||
84 | 'q - Quit\n') | 83 | 'q - Quit\n') | ||
85 | command = '' | 84 | command = '' | ||
86 | while(command != 'q'): | 85 | while(command != 'q'): | ||
87 | print(menu) | 86 | print(menu) | ||
n | 88 | command = input('Choose an option:\n') | n | 87 | command = input('Choose an option:') |
89 | while(command != 'a' and command != 'o' and command != 'i' and command != | 88 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
90 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
91 | if(command == 'a'): | 90 | if(command == 'a'): | ||
92 | print("\nADD ITEM TO CART") | 91 | print("\nADD ITEM TO CART") | ||
93 | item_name = input('Enter the item name:\n') | 92 | item_name = input('Enter the item name:\n') | ||
94 | item_description = input('Enter the item description:\n') | 93 | item_description = input('Enter the item description:\n') | ||
95 | item_price = int(input('Enter the item price:\n')) | 94 | item_price = int(input('Enter the item price:\n')) | ||
96 | item_quantity = int(input('Enter the item quantity:\n')) | 95 | item_quantity = int(input('Enter the item quantity:\n')) | ||
97 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 96 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
98 | customer_Cart.add_item(itemtoPurchase) | 97 | customer_Cart.add_item(itemtoPurchase) | ||
99 | elif(command == 'o'): | 98 | elif(command == 'o'): | ||
100 | print('\nOUTPUT SHOPPING CART') | 99 | print('\nOUTPUT SHOPPING CART') | ||
101 | customer_Cart.print_total() | 100 | customer_Cart.print_total() | ||
102 | elif(command == 'i'): | 101 | elif(command == 'i'): | ||
103 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 102 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
104 | customer_Cart.print_descriptions() | 103 | customer_Cart.print_descriptions() | ||
105 | elif(command == 'r'): | 104 | elif(command == 'r'): | ||
106 | print('REMOVE ITEM FROM CART') | 105 | print('REMOVE ITEM FROM CART') | ||
107 | itemName = input('Enter the name of the item to remove :\n') | 106 | itemName = input('Enter the name of the item to remove :\n') | ||
108 | customer_Cart.remove_item(itemName) | 107 | customer_Cart.remove_item(itemName) | ||
109 | elif(command == 'c'): | 108 | elif(command == 'c'): | ||
110 | print('\nCHANGE ITEM QUANTITY') | 109 | print('\nCHANGE ITEM QUANTITY') | ||
111 | itemName = input('Enter the name of the item :\n') | 110 | itemName = input('Enter the name of the item :\n') | ||
112 | qty = int(input('Enter the new quantity :\n')) | 111 | qty = int(input('Enter the new quantity :\n')) | ||
113 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 112 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
114 | customer_Cart.modify_item(itemToPurchase) | 113 | customer_Cart.modify_item(itemToPurchase) | ||
115 | if __name__ == "__main__": | 114 | if __name__ == "__main__": | ||
116 | customer_name = input("Enter customer's name:\n") | 115 | customer_name = input("Enter customer's name:\n") | ||
117 | current_date = input("Enter today's date:\n") | 116 | current_date = input("Enter today's date:\n") | ||
118 | print("\nCustomer name: %s" %customer_name) | 117 | print("\nCustomer name: %s" %customer_name) | ||
119 | print("Today's date: %s" %current_date) | 118 | print("Today's date: %s" %current_date) | ||
120 | newCart = ShoppingCart(customer_name, current_date) | 119 | newCart = ShoppingCart(customer_name, current_date) | ||
t | 121 | print_menu(newCart) | t | 120 | print_menu(newCart)from math import sqrt |
122 | from math import sqrt | ||||
123 | class pt3d: | 121 | class pt3d: | ||
124 | def __init__(self, x, y, z): | 122 | def __init__(self, x, y, z): | ||
125 | self.x = x | 123 | self.x = x | ||
126 | self.y = y | 124 | self.y = y | ||
127 | self.z = z | 125 | self.z = z | ||
128 | def __add__(self, other): | 126 | def __add__(self, other): | ||
129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 127 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
130 | def __sub__(self, other): | 128 | def __sub__(self, other): | ||
131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 129 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
132 | def __eq__(self, other): | 130 | def __eq__(self, other): | ||
133 | return self.x == other.x & self.y == other.y & self.z == other.z | 131 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
134 | def __str__(self): | 132 | def __str__(self): | ||
135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 133 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
136 | p1 = pt3d(1, 1, 1) | 134 | p1 = pt3d(1, 1, 1) | ||
137 | p2 = pt3d(2, 2, 2) | 135 | p2 = pt3d(2, 2, 2) | ||
138 | print(p1 + p2) | 136 | print(p1 + p2) | ||
139 | print(p1 - p2) | 137 | print(p1 - p2) | ||
140 | print(p1 == p2) | 138 | print(p1 == p2) | ||
141 | print(p1+p1 == p2) | 139 | print(p1+p1 == p2) | ||
142 | print(p1==p2+pt3d(-1, -1, -1)) | 140 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi * self.radius ** 2 | 6 | return math.pi * self.radius ** 2 | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2 * math.pi * self.radius | 8 | return 2 * math.pi * self.radius | ||
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
n | 77 | def print_menu(new_cart): | n | 78 | def print_menu(newCart): |
78 | customer_cart = new_cart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
n | 80 | "a - Add item to cart\n" | n | 81 | 'a - Add item to cart\n' |
81 | "r - Remove item from cart\n" | 82 | 'r - Remove item from the cart\n' | ||
82 | "c - Change item quantity\n" | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
84 | "o - Output shopping cart\n" | 85 | 'o - Output shopping cart\n' | ||
85 | "q - Quit\n") | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:\n') | n | 90 | command = input('Choose an option:') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
n | 102 | customer_cart.print_total() | n | 103 | customer_Cart.print_total() |
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
n | 105 | customer_cart.print_descriptions() | n | 106 | customer_Cart.print_descriptions() |
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
n | 109 | customer_cart.remove_item(itemName) | n | 110 | customer_Cart.remove_item(itemName) |
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
n | 115 | customer_cart.modify_item(itemToPurchase) | n | 116 | customer_Cart.modify_item(itemToPurchase) |
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
n | 121 | new_cart = ShoppingCart(customer_name, current_date) | n | 122 | newCart = ShoppingCart(customer_name, current_date) |
122 | print_menu(new_cart) | 123 | print_menu(newCart) | ||
123 | import math | 124 | import math | ||
n | n | 125 | from math import sqrt | ||
124 | class pt3d(): | 126 | class pt3d(): | ||
125 | def __init__(self,x,y,z): | 127 | def __init__(self,x,y,z): | ||
t | 126 | self.x= x | t | 128 | self.x = x |
127 | self.y= y | 129 | self.y = y | ||
128 | self.z= z | 130 | self.z = z | ||
129 | def __add__(self, other): | 131 | def __add__(self, other): | ||
130 | sumX = self.x + other.x | 132 | sumX = self.x + other.x | ||
131 | sumY = self.y + other.y | 133 | sumY = self.y + other.y | ||
132 | sumZ = self.z + other.z | 134 | sumZ = self.z + other.z | ||
133 | return pt3d(sumX, sumY,sumZ) | 135 | return pt3d(sumX, sumY,sumZ) | ||
134 | def __sub__(self, other): | 136 | def __sub__(self, other): | ||
135 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | 137 | return math.sqrt((((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z- | ||
> | self.z)**2))) | > | self.z)**2))) | ||
136 | def __eq__(self, other): | 138 | def __eq__(self, other): | ||
137 | equalcheck = True | 139 | equalcheck = True | ||
138 | if self.x != other.x and self.y != other.y and self.z != other.z: | 140 | if self.x != other.x and self.y != other.y and self.z != other.z: | ||
139 | equalcheck = False | 141 | equalcheck = False | ||
140 | else: | 142 | else: | ||
141 | equalcheck = True | 143 | equalcheck = True | ||
142 | return equalcheck | 144 | return equalcheck | ||
143 | def __str__(self): | 145 | def __str__(self): | ||
144 | return f'<{self.x},{self.y},{self.z}>' | 146 | return f'<{self.x},{self.y},{self.z}>' | ||
145 | def main(): | 147 | def main(): | ||
146 | a = pt3d(1, 1, 1) | 148 | a = pt3d(1, 1, 1) | ||
147 | b = pt3d(2, 2, 2) | 149 | b = pt3d(2, 2, 2) | ||
148 | print(a+b) | 150 | print(a+b) | ||
149 | print(b-a) | 151 | print(b-a) | ||
150 | a==b | 152 | a==b | ||
151 | a+a==b | 153 | a+a==b | ||
152 | a==b+pt3d(-1,-1,-1) | 154 | a==b+pt3d(-1,-1,-1) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, r): | n | 3 | def __init__(self, radius): |
4 | self.radius = r | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*self.radius**2 | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*math.pi | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
n | 10 | x = int(input("")) | n | 10 | x = int(input()) |
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
n | 12 | print(NewCircle.area()) | n | 12 | print('{:.5f}'.format(NewCircle.area())) |
13 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
n | 22 | def __init__(self, customer_name = 'none', current_date = 'March 06, 2022', c | n | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', |
> | art_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
n | 29 | remove_item = False | n | 29 | tremove_item = False |
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
n | 35 | if not remove_item: | n | 35 | if not tremove_item: |
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in the cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
n | 38 | modify_item = False | n | 38 | tmodify_item = False |
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 41 | modify_item = True | n | 41 | tmodify_item = True |
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
n | 44 | if not modify_item: | n | 44 | if not tmodify_item: |
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in the cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
n | 71 | print('SHOPPING CART IS EMPTY\n') | n | 71 | print('SHOPPING CART IS EMPTY') |
72 | else: | 72 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
76 | item.print_item_description() | 76 | item.print_item_description() | ||
77 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
86 | command = '' | 86 | command = '' | ||
87 | while(command != 'q'): | 87 | while(command != 'q'): | ||
88 | print(menu) | 88 | print(menu) | ||
89 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 92 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
n | 101 | print('OUTPUT SHOPPING CART') | n | 101 | print('\nOUTPUT SHOPPING CART') |
102 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 108 | itemName = input('Enter name of item to remove:\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
109 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
113 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 122 | print_menu(newCart) from math import sqrt | n | 122 | print_menu(newCart)from math import sqrt |
123 | class pt3d: | 123 | class pt3d: | ||
124 | def __init__(self, x, y, z): | 124 | def __init__(self, x, y, z): | ||
125 | self.x = x | 125 | self.x = x | ||
126 | self.y = y | 126 | self.y = y | ||
127 | self.z = z | 127 | self.z = z | ||
128 | def __add__(self, other): | 128 | def __add__(self, other): | ||
129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
130 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
132 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
t | 133 | return self.x == other.x & self.y == other.y & self.z == other.z | t | 133 | return self.x == other.x and self.y == other.y and self.z == other.z |
134 | def __str__(self): | 134 | def __str__(self): | ||
135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
136 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
137 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
138 | print(p1 + p2) | 138 | print(p1 + p2) | ||
139 | print(p1 - p2) | 139 | print(p1 - p2) | ||
140 | print(p1 == p2) | 140 | print(p1 == p2) | ||
141 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
142 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle(): | n | 2 | class Circle : |
3 | def __init__(self, radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*self.radius*self.radius | 6 | return math.pi*self.radius*self.radius | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__=='__main__': | n | 9 | if __name__ == '__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
n | 20 | string = '\n{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity | n | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, |
> | , self.item_price,(self.item_quantity * self.item_price)) | > | self.item_price, | ||
21 | (self.item_quantity * self.item_price)) | ||||
21 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
22 | return string, cost | 23 | return string, cost | ||
23 | def print_item_description(self): | 24 | def print_item_description(self): | ||
24 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
25 | print(string) | 26 | print(string) | ||
26 | return string | 27 | return string | ||
27 | class ShoppingCart: | 28 | class ShoppingCart: | ||
28 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
29 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
30 | self.current_date = current_date | 31 | self.current_date = current_date | ||
31 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
32 | def add_item(self): | 33 | def add_item(self): | ||
33 | print('ADD ITEM TO CART') | 34 | print('ADD ITEM TO CART') | ||
n | 34 | item_name = str(input('Enter the item name:\n')) | n | 35 | item_name = str(input('Enter the item name:')) |
36 | print() | ||||
35 | item_description = str(input('Enter the item description:\n')) | 37 | item_description = str(input('Enter the item description:')) | ||
38 | print() | ||||
36 | item_price = int(input('Enter the item price:\n')) | 39 | item_price = int(input('Enter the item price:')) | ||
40 | print() | ||||
37 | item_quantity = int(input('Enter the item quantity:\n')) | 41 | item_quantity = int(input('Enter the item quantity:')) | ||
42 | print() | ||||
38 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
39 | def remove_item(self): | 44 | def remove_item(self): | ||
40 | print('REMOVE ITEM FROM CART') | 45 | print('REMOVE ITEM FROM CART') | ||
n | 41 | string = str(input('Enter name of item to remove:\n')) | n | 46 | string = str(input('Enter name of item to remove:')) |
47 | print() | ||||
42 | i = 0 | 48 | i = 0 | ||
43 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
44 | if(item.item_name == string): | 50 | if(item.item_name == string): | ||
45 | del self.cart_items[i] | 51 | del self.cart_items[i] | ||
46 | flag=True | 52 | flag=True | ||
47 | break | 53 | break | ||
48 | else: | 54 | else: | ||
49 | flag=False | 55 | flag=False | ||
50 | i += 1 | 56 | i += 1 | ||
51 | if(flag==False): | 57 | if(flag==False): | ||
52 | print('Item not found in cart. Nothing removed.') | 58 | print('Item not found in cart. Nothing removed.') | ||
53 | def modify_item(self): | 59 | def modify_item(self): | ||
54 | print('CHANGE ITEM QUANTITY') | 60 | print('CHANGE ITEM QUANTITY') | ||
55 | name = str(input('Enter the item name:')) | 61 | name = str(input('Enter the item name:')) | ||
56 | print() | 62 | print() | ||
57 | for item in self.cart_items: | 63 | for item in self.cart_items: | ||
58 | if(item.item_name == name): | 64 | if(item.item_name == name): | ||
59 | quantity = int(input('Enter the new quantity:')) | 65 | quantity = int(input('Enter the new quantity:')) | ||
n | n | 66 | print() | ||
60 | item.item_quantity = quantity | 67 | item.item_quantity = quantity | ||
61 | flag=True | 68 | flag=True | ||
n | n | 69 | break | ||
62 | else: | 70 | else: | ||
63 | flag=False | 71 | flag=False | ||
64 | if(flag==False): | 72 | if(flag==False): | ||
65 | print('Item not found in cart. Nothing modified.') | 73 | print('Item not found in cart. Nothing modified.') | ||
66 | print() | 74 | print() | ||
67 | def get_num_items_in_cart(self): | 75 | def get_num_items_in_cart(self): | ||
68 | num_items=0 | 76 | num_items=0 | ||
69 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
70 | num_items= num_items+item.item_quantity | 78 | num_items= num_items+item.item_quantity | ||
71 | return num_items | 79 | return num_items | ||
72 | def get_cost_of_cart(self): | 80 | def get_cost_of_cart(self): | ||
73 | total_cost = 0 | 81 | total_cost = 0 | ||
74 | cost = 0 | 82 | cost = 0 | ||
75 | for item in self.cart_items: | 83 | for item in self.cart_items: | ||
76 | cost = (item.item_quantity * item.item_price) | 84 | cost = (item.item_quantity * item.item_price) | ||
77 | total_cost += cost | 85 | total_cost += cost | ||
78 | return total_cost | 86 | return total_cost | ||
79 | def print_total(self): | 87 | def print_total(self): | ||
80 | total_cost = self.get_cost_of_cart() | 88 | total_cost = self.get_cost_of_cart() | ||
81 | if (total_cost == 0): | 89 | if (total_cost == 0): | ||
82 | print('SHOPPING CART IS EMPTY') | 90 | print('SHOPPING CART IS EMPTY') | ||
83 | else: | 91 | else: | ||
84 | self.output_cart() | 92 | self.output_cart() | ||
85 | def print_descriptions(self): | 93 | def print_descriptions(self): | ||
86 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
88 | print('\nItem Descriptions') | 96 | print('\nItem Descriptions') | ||
89 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
90 | print('{}: {}'.format(item.item_name, item.item_description)) | 98 | print('{}: {}'.format(item.item_name, item.item_description)) | ||
91 | def output_cart(self): | 99 | def output_cart(self): | ||
92 | print('OUTPUT SHOPPING CART') | 100 | print('OUTPUT SHOPPING CART') | ||
93 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
94 | print('Number of Items:', self.get_num_items_in_cart()) | 102 | print('Number of Items:', self.get_num_items_in_cart()) | ||
n | n | 103 | print() | ||
95 | tc = 0 | 104 | tc = 0 | ||
96 | for item in self.cart_items: | 105 | for item in self.cart_items: | ||
97 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
98 | item.item_price, (item.item_quantity * item.item_price))) | 107 | item.item_price, (item.item_quantity * item.item_price))) | ||
99 | tc += (item.item_quantity * item.item_price) | 108 | tc += (item.item_quantity * item.item_price) | ||
100 | if len(self.cart_items) == 0: | 109 | if len(self.cart_items) == 0: | ||
n | 101 | print('\nSHOPPING CART IS EMPTY') | n | 110 | print('SHOPPING CART IS EMPTY') |
102 | print() | 111 | print() | ||
103 | print('Total: ${}'.format(tc)) | 112 | print('Total: ${}'.format(tc)) | ||
104 | def print_menu(customer_Cart): | 113 | def print_menu(customer_Cart): | ||
105 | menu = ('\nMENU\n' | 114 | menu = ('\nMENU\n' | ||
106 | 'a - Add item to cart\n' | 115 | 'a - Add item to cart\n' | ||
107 | 'r - Remove item from cart\n' | 116 | 'r - Remove item from cart\n' | ||
108 | 'c - Change item quantity\n' | 117 | 'c - Change item quantity\n' | ||
109 | 'i - Output items\' descriptions\n' | 118 | 'i - Output items\' descriptions\n' | ||
110 | 'o - Output shopping cart\n' | 119 | 'o - Output shopping cart\n' | ||
111 | 'q - Quit\n') | 120 | 'q - Quit\n') | ||
112 | command = '' | 121 | command = '' | ||
113 | while(command != 'q'): | 122 | while(command != 'q'): | ||
114 | print(menu) | 123 | print(menu) | ||
n | 115 | command = input('Choose an option:\n') | n | 124 | command = input('Choose an option:') |
125 | print() | ||||
116 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r'and command != 'c' and command != 'q'): | > | = 'r' | ||
127 | and command != 'c' and command != 'q'): | ||||
117 | command = input('Choose an option:\n') | 128 | command = input('Choose an option:') | ||
129 | print() | ||||
118 | if(command == 'a'): | 130 | if(command == 'a'): | ||
119 | customer_Cart.add_item() | 131 | customer_Cart.add_item() | ||
120 | if(command == 'o'): | 132 | if(command == 'o'): | ||
121 | customer_Cart.output_cart() | 133 | customer_Cart.output_cart() | ||
122 | if(command == 'i'): | 134 | if(command == 'i'): | ||
123 | customer_Cart.print_descriptions() | 135 | customer_Cart.print_descriptions() | ||
124 | if(command == 'r'): | 136 | if(command == 'r'): | ||
125 | customer_Cart.remove_item() | 137 | customer_Cart.remove_item() | ||
126 | if(command == 'c'): | 138 | if(command == 'c'): | ||
127 | customer_Cart.modify_item() | 139 | customer_Cart.modify_item() | ||
128 | def main(): | 140 | def main(): | ||
129 | customer_name = str(input('Enter customer\'s name:')) | 141 | customer_name = str(input('Enter customer\'s name:')) | ||
130 | print() | 142 | print() | ||
131 | current_date = str(input('Enter today\'s date:')) | 143 | current_date = str(input('Enter today\'s date:')) | ||
n | 132 | print() | n | 144 | print('\n') |
133 | print() | ||||
134 | print('Customer name:', customer_name) | 145 | print('Customer name:', customer_name, end='\n') | ||
135 | print('Today\'s date:', current_date) | 146 | print('Today\'s date:', current_date, end='\n') | ||
136 | x = ShoppingCart(customer_name, current_date) | 147 | newCart = ShoppingCart(customer_name, current_date) | ||
137 | print_menu(x) | 148 | print_menu(newCart) | ||
138 | if __name__ == '__main__': | 149 | if __name__ == '__main__': | ||
139 | main()import math | 150 | main()import math | ||
140 | class pt3d: | 151 | class pt3d: | ||
141 | def __init__(self,x=0,y=0,z=0): | 152 | def __init__(self,x=0,y=0,z=0): | ||
142 | self.x= x | 153 | self.x= x | ||
143 | self.y= y | 154 | self.y= y | ||
144 | self.z= z | 155 | self.z= z | ||
145 | def __add__(self, other): | 156 | def __add__(self, other): | ||
146 | x = self.x + other.x | 157 | x = self.x + other.x | ||
147 | y = self.y + other.y | 158 | y = self.y + other.y | ||
148 | z = self.z + other.z | 159 | z = self.z + other.z | ||
149 | return pt3d(x, y,z) | 160 | return pt3d(x, y,z) | ||
150 | def __sub__(self, other): | 161 | def __sub__(self, other): | ||
151 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 162 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
152 | def __eq__(self, other): | 163 | def __eq__(self, other): | ||
153 | return other.x==self.x and other.y==self.y and other.z==self.z | 164 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
154 | def __str__(self): | 165 | def __str__(self): | ||
155 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 166 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
156 | if __name__ == '__main__': | 167 | if __name__ == '__main__': | ||
157 | p1 = pt3d(1, 1, 1) | 168 | p1 = pt3d(1, 1, 1) | ||
158 | p2 = pt3d(2, 2, 2) | 169 | p2 = pt3d(2, 2, 2) | ||
159 | print(p1+p2) | 170 | print(p1+p2) | ||
160 | print(p1-p2) | 171 | print(p1-p2) | ||
161 | print(p1==p2) | 172 | print(p1==p2) | ||
162 | print(p1+p1==p2) | 173 | print(p1+p1==p2) | ||
t | 163 | print(p1==p2+pt3d(-1,-1,-1)) | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle : | n | 2 | class Circle(): |
3 | def __init__(self,radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*self.radius*self.radius | 6 | return math.pi*self.radius*self.radius | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__ == '__main__': | n | 9 | if __name__=='__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
n | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | n | 20 | string = '\n{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity |
> | self.item_price, | > | , self.item_price,(self.item_quantity * self.item_price)) | ||
21 | (self.item_quantity * self.item_price)) | ||||
22 | cost = self.item_quantity * self.item_price | 21 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 22 | return string, cost | ||
24 | def print_item_description(self): | 23 | def print_item_description(self): | ||
25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 24 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
26 | print(string) | 25 | print(string) | ||
27 | return string | 26 | return string | ||
28 | class ShoppingCart: | 27 | class ShoppingCart: | ||
29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 28 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
30 | self.customer_name = customer_name | 29 | self.customer_name = customer_name | ||
31 | self.current_date = current_date | 30 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 31 | self.cart_items = cart_items | ||
33 | def add_item(self): | 32 | def add_item(self): | ||
34 | print('ADD ITEM TO CART') | 33 | print('ADD ITEM TO CART') | ||
n | 35 | item_name = str(input('Enter the item name:')) | n | 34 | item_name = str(input('Enter the item name:\n')) |
36 | print() | ||||
37 | item_description = str(input('Enter the item description:')) | 35 | item_description = str(input('Enter the item description:\n')) | ||
38 | print() | ||||
39 | item_price = int(input('Enter the item price:')) | 36 | item_price = int(input('Enter the item price:\n')) | ||
40 | print() | ||||
41 | item_quantity = int(input('Enter the item quantity:')) | 37 | item_quantity = int(input('Enter the item quantity:\n')) | ||
42 | print() | ||||
43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 38 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
44 | def remove_item(self): | 39 | def remove_item(self): | ||
45 | print('REMOVE ITEM FROM CART') | 40 | print('REMOVE ITEM FROM CART') | ||
n | 46 | string = str(input('Enter name of item to remove:')) | n | 41 | string = str(input('Enter name of item to remove:\n')) |
47 | print() | ||||
48 | i = 0 | 42 | i = 0 | ||
49 | for item in self.cart_items: | 43 | for item in self.cart_items: | ||
50 | if(item.item_name == string): | 44 | if(item.item_name == string): | ||
51 | del self.cart_items[i] | 45 | del self.cart_items[i] | ||
52 | flag=True | 46 | flag=True | ||
53 | break | 47 | break | ||
54 | else: | 48 | else: | ||
55 | flag=False | 49 | flag=False | ||
56 | i += 1 | 50 | i += 1 | ||
57 | if(flag==False): | 51 | if(flag==False): | ||
58 | print('Item not found in cart. Nothing removed.') | 52 | print('Item not found in cart. Nothing removed.') | ||
59 | def modify_item(self): | 53 | def modify_item(self): | ||
60 | print('CHANGE ITEM QUANTITY') | 54 | print('CHANGE ITEM QUANTITY') | ||
61 | name = str(input('Enter the item name:')) | 55 | name = str(input('Enter the item name:')) | ||
62 | print() | 56 | print() | ||
63 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
64 | if(item.item_name == name): | 58 | if(item.item_name == name): | ||
65 | quantity = int(input('Enter the new quantity:')) | 59 | quantity = int(input('Enter the new quantity:')) | ||
n | 66 | print() | n | ||
67 | item.item_quantity = quantity | 60 | item.item_quantity = quantity | ||
68 | flag=True | 61 | flag=True | ||
n | 69 | break | n | ||
70 | else: | 62 | else: | ||
71 | flag=False | 63 | flag=False | ||
72 | if(flag==False): | 64 | if(flag==False): | ||
73 | print('Item not found in cart. Nothing modified.') | 65 | print('Item not found in cart. Nothing modified.') | ||
74 | print() | 66 | print() | ||
75 | def get_num_items_in_cart(self): | 67 | def get_num_items_in_cart(self): | ||
76 | num_items=0 | 68 | num_items=0 | ||
77 | for item in self.cart_items: | 69 | for item in self.cart_items: | ||
78 | num_items= num_items+item.item_quantity | 70 | num_items= num_items+item.item_quantity | ||
79 | return num_items | 71 | return num_items | ||
80 | def get_cost_of_cart(self): | 72 | def get_cost_of_cart(self): | ||
81 | total_cost = 0 | 73 | total_cost = 0 | ||
82 | cost = 0 | 74 | cost = 0 | ||
83 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
84 | cost = (item.item_quantity * item.item_price) | 76 | cost = (item.item_quantity * item.item_price) | ||
85 | total_cost += cost | 77 | total_cost += cost | ||
86 | return total_cost | 78 | return total_cost | ||
87 | def print_total(self): | 79 | def print_total(self): | ||
88 | total_cost = self.get_cost_of_cart() | 80 | total_cost = self.get_cost_of_cart() | ||
89 | if (total_cost == 0): | 81 | if (total_cost == 0): | ||
90 | print('SHOPPING CART IS EMPTY') | 82 | print('SHOPPING CART IS EMPTY') | ||
91 | else: | 83 | else: | ||
92 | self.output_cart() | 84 | self.output_cart() | ||
93 | def print_descriptions(self): | 85 | def print_descriptions(self): | ||
94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 86 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
96 | print('\nItem Descriptions') | 88 | print('\nItem Descriptions') | ||
97 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
98 | print('{}: {}'.format(item.item_name, item.item_description)) | 90 | print('{}: {}'.format(item.item_name, item.item_description)) | ||
99 | def output_cart(self): | 91 | def output_cart(self): | ||
100 | print('OUTPUT SHOPPING CART') | 92 | print('OUTPUT SHOPPING CART') | ||
101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 93 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
102 | print('Number of Items:', self.get_num_items_in_cart()) | 94 | print('Number of Items:', self.get_num_items_in_cart()) | ||
n | 103 | print() | n | ||
104 | tc = 0 | 95 | tc = 0 | ||
105 | for item in self.cart_items: | 96 | for item in self.cart_items: | ||
106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 97 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
107 | item.item_price, (item.item_quantity * item.item_price))) | 98 | item.item_price, (item.item_quantity * item.item_price))) | ||
108 | tc += (item.item_quantity * item.item_price) | 99 | tc += (item.item_quantity * item.item_price) | ||
109 | if len(self.cart_items) == 0: | 100 | if len(self.cart_items) == 0: | ||
n | 110 | print('SHOPPING CART IS EMPTY') | n | 101 | print('\nSHOPPING CART IS EMPTY') |
111 | print() | 102 | print() | ||
112 | print('Total: ${}'.format(tc)) | 103 | print('Total: ${}'.format(tc)) | ||
113 | def print_menu(customer_Cart): | 104 | def print_menu(customer_Cart): | ||
114 | menu = ('\nMENU\n' | 105 | menu = ('\nMENU\n' | ||
115 | 'a - Add item to cart\n' | 106 | 'a - Add item to cart\n' | ||
116 | 'r - Remove item from cart\n' | 107 | 'r - Remove item from cart\n' | ||
117 | 'c - Change item quantity\n' | 108 | 'c - Change item quantity\n' | ||
118 | 'i - Output items\' descriptions\n' | 109 | 'i - Output items\' descriptions\n' | ||
119 | 'o - Output shopping cart\n' | 110 | 'o - Output shopping cart\n' | ||
120 | 'q - Quit\n') | 111 | 'q - Quit\n') | ||
121 | command = '' | 112 | command = '' | ||
122 | while(command != 'q'): | 113 | while(command != 'q'): | ||
123 | print(menu) | 114 | print(menu) | ||
n | 124 | command = input('Choose an option:') | n | 115 | command = input('Choose an option:\n') |
125 | print() | ||||
126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 116 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' | > | = 'r'and command != 'c' and command != 'q'): | ||
127 | and command != 'c' and command != 'q'): | ||||
128 | command = input('Choose an option:') | 117 | command = input('Choose an option:\n') | ||
129 | print() | ||||
130 | if(command == 'a'): | 118 | if(command == 'a'): | ||
131 | customer_Cart.add_item() | 119 | customer_Cart.add_item() | ||
132 | if(command == 'o'): | 120 | if(command == 'o'): | ||
133 | customer_Cart.output_cart() | 121 | customer_Cart.output_cart() | ||
134 | if(command == 'i'): | 122 | if(command == 'i'): | ||
135 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
136 | if(command == 'r'): | 124 | if(command == 'r'): | ||
137 | customer_Cart.remove_item() | 125 | customer_Cart.remove_item() | ||
138 | if(command == 'c'): | 126 | if(command == 'c'): | ||
139 | customer_Cart.modify_item() | 127 | customer_Cart.modify_item() | ||
140 | def main(): | 128 | def main(): | ||
141 | customer_name = str(input('Enter customer\'s name:')) | 129 | customer_name = str(input('Enter customer\'s name:')) | ||
142 | print() | 130 | print() | ||
143 | current_date = str(input('Enter today\'s date:')) | 131 | current_date = str(input('Enter today\'s date:')) | ||
n | 144 | print('\n') | n | 132 | print() |
133 | print() | ||||
145 | print('Customer name:', customer_name, end='\n') | 134 | print('Customer name:', customer_name) | ||
146 | print('Today\'s date:', current_date, end='\n') | 135 | print('Today\'s date:', current_date) | ||
147 | newCart = ShoppingCart(customer_name, current_date) | 136 | x = ShoppingCart(customer_name, current_date) | ||
148 | print_menu(newCart) | 137 | print_menu(x) | ||
149 | if __name__ == '__main__': | 138 | if __name__ == '__main__': | ||
150 | main()import math | 139 | main()import math | ||
151 | class pt3d: | 140 | class pt3d: | ||
152 | def __init__(self,x=0,y=0,z=0): | 141 | def __init__(self,x=0,y=0,z=0): | ||
153 | self.x= x | 142 | self.x= x | ||
154 | self.y= y | 143 | self.y= y | ||
155 | self.z= z | 144 | self.z= z | ||
156 | def __add__(self, other): | 145 | def __add__(self, other): | ||
157 | x = self.x + other.x | 146 | x = self.x + other.x | ||
158 | y = self.y + other.y | 147 | y = self.y + other.y | ||
159 | z = self.z + other.z | 148 | z = self.z + other.z | ||
160 | return pt3d(x, y,z) | 149 | return pt3d(x, y,z) | ||
161 | def __sub__(self, other): | 150 | def __sub__(self, other): | ||
162 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 151 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
163 | def __eq__(self, other): | 152 | def __eq__(self, other): | ||
164 | return other.x==self.x and other.y==self.y and other.z==self.z | 153 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
165 | def __str__(self): | 154 | def __str__(self): | ||
166 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 155 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
167 | if __name__ == '__main__': | 156 | if __name__ == '__main__': | ||
168 | p1 = pt3d(1, 1, 1) | 157 | p1 = pt3d(1, 1, 1) | ||
169 | p2 = pt3d(2, 2, 2) | 158 | p2 = pt3d(2, 2, 2) | ||
170 | print(p1+p2) | 159 | print(p1+p2) | ||
171 | print(p1-p2) | 160 | print(p1-p2) | ||
172 | print(p1==p2) | 161 | print(p1==p2) | ||
173 | print(p1+p1==p2) | 162 | print(p1+p1==p2) | ||
t | t | 163 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self, radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return 2*math.pi*self.radius |
9 | small_circle = Circle(2) | 9 | if __name__ == '__main__': | ||
10 | print('Area of the circle:{:.3f}'.format(small_circle.area()))class ItemToPurcha | 10 | x = int(input()) | ||
> | se: | ||||
11 | NewCircle = Circle(x) | ||||
12 | print('{:.5f}'.format(NewCircle.area())) | ||||
13 | print('{:.5f}'.format(NewCircle.perimeter())) | ||||
14 | class ItemToPurchase: | ||||
11 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
12 | self.item_name=name | 16 | self.item_name=name | ||
13 | self.item_description=description | 17 | self.item_description=description | ||
14 | self.item_price=price | 18 | self.item_price=price | ||
15 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
16 | def print_item_description(self): | 20 | def print_item_description(self): | ||
17 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
18 | class ShoppingCart: | 22 | class ShoppingCart: | ||
19 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
20 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
21 | self.current_date = current_date | 25 | self.current_date = current_date | ||
22 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
23 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
n | 24 | self.cart_items.append(itemToPurchase) | n | 28 | self.cart_items.append(itemToPurchase) |
25 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
26 | tremove_item = False | 30 | tremove_item = False | ||
27 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
28 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
29 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
30 | tremove_item = True | 34 | tremove_item = True | ||
31 | break | 35 | break | ||
32 | if not tremove_item: | 36 | if not tremove_item: | ||
n | 33 | print('Item not found in the cart. Nothing removed') | n | 37 | print('Item not found in the cart. Nothing removed') |
34 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
35 | tmodify_item = False | 39 | tmodify_item = False | ||
36 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
37 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
38 | tmodify_item = True | 42 | tmodify_item = True | ||
39 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
40 | break | 44 | break | ||
41 | if not tmodify_item: | 45 | if not tmodify_item: | ||
n | 42 | print('Item not found in the cart. Nothing modified') | n | 46 | print('Item not found in the cart. Nothing modified') |
43 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
44 | num_items = 0 | 48 | num_items = 0 | ||
45 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
46 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
47 | return num_items | 51 | return num_items | ||
48 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
49 | total_cost = 0 | 53 | total_cost = 0 | ||
50 | cost = 0 | 54 | cost = 0 | ||
51 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
52 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
53 | total_cost += cost | 57 | total_cost += cost | ||
n | 54 | return total_cost | n | 58 | return total_cost |
55 | def print_total(self): | 59 | def print_total(self): | ||
56 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
57 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
58 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
59 | else: | 63 | else: | ||
60 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
61 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
62 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
63 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
64 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
n | 65 | print('\nTotal: $%d' %(total_cost)) | n | 69 | print('\nTotal: $%d' %(total_cost)) |
66 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
67 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
68 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
69 | else: | 73 | else: | ||
70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
71 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
72 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
n | 73 | item.print_item_description() | n | 77 | item.print_item_description() |
74 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
75 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
76 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
77 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
n | 78 | 'r - Remove item from the cart\n' | n | 82 | 'r - Remove item from cart\n' |
79 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
n | 80 | "i - Output item's descriptions\n" | n | 84 | "i - Output items' descriptions\n" |
81 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
82 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
83 | command = '' | 87 | command = '' | ||
84 | while(command != 'q'): | 88 | while(command != 'q'): | ||
85 | print(menu) | 89 | print(menu) | ||
n | 86 | command = input('Choose an option:') | n | 90 | command = input('Choose an option:\n') |
87 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
88 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
89 | if(command == 'a'): | 93 | if(command == 'a'): | ||
90 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
91 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
92 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
93 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
94 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
95 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
96 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
97 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
98 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
99 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
100 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
101 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
102 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
103 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
104 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
105 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
106 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
107 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
108 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
109 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
110 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
111 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
112 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
113 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
114 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
115 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
116 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
117 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
118 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
119 | print_menu(newCart)import math | 123 | print_menu(newCart)import math | ||
120 | class pt3d: | 124 | class pt3d: | ||
t | 121 | def __init__(self,x,y,z): | t | 125 | def __init__(self,x=0,y=0,z=0): |
122 | self.x= x | 126 | self.x= x | ||
123 | self.y= y | 127 | self.y= y | ||
124 | self.z= z | 128 | self.z= z | ||
125 | def __add__(self, other): | 129 | def __add__(self, other): | ||
126 | x = self.x + other.x | 130 | x = self.x + other.x | ||
127 | y = self.y + other.y | 131 | y = self.y + other.y | ||
128 | z = self.z + other.z | 132 | z = self.z + other.z | ||
129 | return pt3d(x, y,z) | 133 | return pt3d(x, y,z) | ||
130 | def __sub__(self, other): | 134 | def __sub__(self, other): | ||
131 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
132 | def __eq__(self, other): | 136 | def __eq__(self, other): | ||
133 | return other.x==self.x and other.y==self.y and other.z==self.z | 137 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
134 | def __str__(self): | 138 | def __str__(self): | ||
135 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
136 | if __name__ == '__main__': | 140 | if __name__ == '__main__': | ||
137 | p1 = pt3d(1, 1, 1) | 141 | p1 = pt3d(1, 1, 1) | ||
138 | p2 = pt3d(2, 2, 2) | 142 | p2 = pt3d(2, 2, 2) | ||
139 | print(p1+p2) | 143 | print(p1+p2) | ||
140 | print(p1-p2) | 144 | print(p1-p2) | ||
141 | print(p1==p2) | 145 | print(p1==p2) | ||
142 | print(p1+p1==p2) | 146 | print(p1+p1==p2) | ||
143 | print(p1==p2+pt3d(-1,-1,-1)) | 147 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self,radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*self.radius*self.radius | 6 | return math.pi*self.radius*self.radius | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
n | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | n | 20 | string = '\n{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity |
> | self.item_price, | > | , self.item_price,(self.item_quantity * self.item_price)) | ||
21 | (self.item_quantity * self.item_price)) | ||||
22 | cost = self.item_quantity * self.item_price | 21 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 22 | return string, cost | ||
24 | def print_item_description(self): | 23 | def print_item_description(self): | ||
25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 24 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
26 | print(string) | 25 | print(string) | ||
27 | return string | 26 | return string | ||
28 | class ShoppingCart: | 27 | class ShoppingCart: | ||
29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 28 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
30 | self.customer_name = customer_name | 29 | self.customer_name = customer_name | ||
31 | self.current_date = current_date | 30 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 31 | self.cart_items = cart_items | ||
33 | def add_item(self): | 32 | def add_item(self): | ||
34 | print('ADD ITEM TO CART') | 33 | print('ADD ITEM TO CART') | ||
n | 35 | item_name = str(input('Enter the item name:')) | n | 34 | item_name = str(input('Enter the item name:\n')) |
36 | item_description = str(input('Enter the item description:')) | 35 | item_description = str(input('Enter the item description:\n')) | ||
37 | item_price = int(input('Enter the item price:')) | 36 | item_price = int(input('Enter the item price:\n')) | ||
38 | item_quantity = int(input('Enter the item quantity:')) | 37 | item_quantity = int(input('Enter the item quantity:\n')) | ||
39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 38 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
40 | def remove_item(self): | 39 | def remove_item(self): | ||
41 | print('REMOVE ITEM FROM CART') | 40 | print('REMOVE ITEM FROM CART') | ||
n | 42 | string = str(input('Enter name of item to remove:')) | n | 41 | string = str(input('Enter name of item to remove:\n')) |
43 | i = 0 | 42 | i = 0 | ||
44 | for item in self.cart_items: | 43 | for item in self.cart_items: | ||
45 | if(item.item_name == string): | 44 | if(item.item_name == string): | ||
46 | del self.cart_items[i] | 45 | del self.cart_items[i] | ||
47 | flag=True | 46 | flag=True | ||
48 | break | 47 | break | ||
49 | else: | 48 | else: | ||
50 | flag=False | 49 | flag=False | ||
51 | i += 1 | 50 | i += 1 | ||
52 | if(flag==False): | 51 | if(flag==False): | ||
53 | print('Item not found in cart. Nothing removed.') | 52 | print('Item not found in cart. Nothing removed.') | ||
54 | def modify_item(self): | 53 | def modify_item(self): | ||
55 | print('CHANGE ITEM QUANTITY') | 54 | print('CHANGE ITEM QUANTITY') | ||
56 | name = str(input('Enter the item name:')) | 55 | name = str(input('Enter the item name:')) | ||
57 | print() | 56 | print() | ||
58 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
59 | if(item.item_name == name): | 58 | if(item.item_name == name): | ||
60 | quantity = int(input('Enter the new quantity:')) | 59 | quantity = int(input('Enter the new quantity:')) | ||
61 | item.item_quantity = quantity | 60 | item.item_quantity = quantity | ||
62 | flag=True | 61 | flag=True | ||
n | 63 | break | n | ||
64 | else: | 62 | else: | ||
65 | flag=False | 63 | flag=False | ||
66 | if(flag==False): | 64 | if(flag==False): | ||
67 | print('Item not found in cart. Nothing modified.') | 65 | print('Item not found in cart. Nothing modified.') | ||
68 | print() | 66 | print() | ||
69 | def get_num_items_in_cart(self): | 67 | def get_num_items_in_cart(self): | ||
70 | num_items=0 | 68 | num_items=0 | ||
71 | for item in self.cart_items: | 69 | for item in self.cart_items: | ||
72 | num_items= num_items+item.item_quantity | 70 | num_items= num_items+item.item_quantity | ||
73 | return num_items | 71 | return num_items | ||
74 | def get_cost_of_cart(self): | 72 | def get_cost_of_cart(self): | ||
75 | total_cost = 0 | 73 | total_cost = 0 | ||
76 | cost = 0 | 74 | cost = 0 | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
78 | cost = (item.item_quantity * item.item_price) | 76 | cost = (item.item_quantity * item.item_price) | ||
79 | total_cost += cost | 77 | total_cost += cost | ||
80 | return total_cost | 78 | return total_cost | ||
81 | def print_total(self): | 79 | def print_total(self): | ||
82 | total_cost = self.get_cost_of_cart() | 80 | total_cost = self.get_cost_of_cart() | ||
83 | if (total_cost == 0): | 81 | if (total_cost == 0): | ||
84 | print('SHOPPING CART IS EMPTY') | 82 | print('SHOPPING CART IS EMPTY') | ||
85 | else: | 83 | else: | ||
86 | self.output_cart() | 84 | self.output_cart() | ||
87 | def print_descriptions(self): | 85 | def print_descriptions(self): | ||
88 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 86 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
89 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
90 | print('\nItem Descriptions') | 88 | print('\nItem Descriptions') | ||
91 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
92 | print('{}: {}'.format(item.item_name, item.item_description)) | 90 | print('{}: {}'.format(item.item_name, item.item_description)) | ||
93 | def output_cart(self): | 91 | def output_cart(self): | ||
94 | print('OUTPUT SHOPPING CART') | 92 | print('OUTPUT SHOPPING CART') | ||
95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 93 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
96 | print('Number of Items:', self.get_num_items_in_cart()) | 94 | print('Number of Items:', self.get_num_items_in_cart()) | ||
97 | tc = 0 | 95 | tc = 0 | ||
98 | for item in self.cart_items: | 96 | for item in self.cart_items: | ||
99 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 97 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
100 | item.item_price, (item.item_quantity * item.item_price))) | 98 | item.item_price, (item.item_quantity * item.item_price))) | ||
101 | tc += (item.item_quantity * item.item_price) | 99 | tc += (item.item_quantity * item.item_price) | ||
102 | if len(self.cart_items) == 0: | 100 | if len(self.cart_items) == 0: | ||
n | 103 | print('SHOPPING CART IS EMPTY') | n | 101 | print('\nSHOPPING CART IS EMPTY') |
104 | print() | 102 | print() | ||
105 | print('Total: ${}'.format(tc)) | 103 | print('Total: ${}'.format(tc)) | ||
106 | def print_menu(customer_Cart): | 104 | def print_menu(customer_Cart): | ||
107 | menu = ('\nMENU\n' | 105 | menu = ('\nMENU\n' | ||
108 | 'a - Add item to cart\n' | 106 | 'a - Add item to cart\n' | ||
109 | 'r - Remove item from cart\n' | 107 | 'r - Remove item from cart\n' | ||
110 | 'c - Change item quantity\n' | 108 | 'c - Change item quantity\n' | ||
111 | 'i - Output items\' descriptions\n' | 109 | 'i - Output items\' descriptions\n' | ||
112 | 'o - Output shopping cart\n' | 110 | 'o - Output shopping cart\n' | ||
113 | 'q - Quit\n') | 111 | 'q - Quit\n') | ||
114 | command = '' | 112 | command = '' | ||
115 | while(command != 'q'): | 113 | while(command != 'q'): | ||
116 | print(menu) | 114 | print(menu) | ||
n | 117 | command = input('Choose an option:') | n | 115 | command = input('Choose an option:\n') |
118 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 116 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' | > | = 'r'and command != 'c' and command != 'q'): | ||
119 | and command != 'c' and command != 'q'): | ||||
120 | command = input('Choose an option:') | 117 | command = input('Choose an option:\n') | ||
121 | if(command == 'a'): | 118 | if(command == 'a'): | ||
122 | customer_Cart.add_item() | 119 | customer_Cart.add_item() | ||
123 | if(command == 'o'): | 120 | if(command == 'o'): | ||
124 | customer_Cart.output_cart() | 121 | customer_Cart.output_cart() | ||
125 | if(command == 'i'): | 122 | if(command == 'i'): | ||
126 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
127 | if(command == 'r'): | 124 | if(command == 'r'): | ||
128 | customer_Cart.remove_item() | 125 | customer_Cart.remove_item() | ||
129 | if(command == 'c'): | 126 | if(command == 'c'): | ||
130 | customer_Cart.modify_item() | 127 | customer_Cart.modify_item() | ||
n | 131 | if __name__ == '__main__': | n | 128 | def main(): |
132 | customer_name = str(input('Enter customer\'s name:')) | 129 | customer_name = str(input('Enter customer\'s name:')) | ||
n | n | 130 | print() | ||
133 | current_date = str(input('Enter today\'s date:')) | 131 | current_date = str(input('Enter today\'s date:')) | ||
n | n | 132 | print() | ||
133 | print() | ||||
134 | print('Customer name:', customer_name) | 134 | print('Customer name:', customer_name) | ||
135 | print('Today\'s date:', current_date) | 135 | print('Today\'s date:', current_date) | ||
136 | x = ShoppingCart(customer_name, current_date) | 136 | x = ShoppingCart(customer_name, current_date) | ||
137 | print_menu(x) | 137 | print_menu(x) | ||
n | 138 | import math | n | 138 | if __name__ == '__main__': |
139 | main()import math | ||||
139 | class pt3d: | 140 | class pt3d: | ||
n | 140 | def __init__(self,x,y,z): | n | 141 | def __init__(self,x=0,y=0,z=0): |
141 | self.x= x | 142 | self.x= x | ||
142 | self.y= y | 143 | self.y= y | ||
143 | self.z= z | 144 | self.z= z | ||
144 | def __add__(self, other): | 145 | def __add__(self, other): | ||
145 | x = self.x + other.x | 146 | x = self.x + other.x | ||
146 | y = self.y + other.y | 147 | y = self.y + other.y | ||
147 | z = self.z + other.z | 148 | z = self.z + other.z | ||
148 | return pt3d(x, y,z) | 149 | return pt3d(x, y,z) | ||
149 | def __sub__(self, other): | 150 | def __sub__(self, other): | ||
150 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 151 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
151 | def __eq__(self, other): | 152 | def __eq__(self, other): | ||
152 | return other.x==self.x and other.y==self.y and other.z==self.z | 153 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
153 | def __str__(self): | 154 | def __str__(self): | ||
154 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 155 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
t | 155 | if __name__ == "__main__": | t | 156 | if __name__ == '__main__': |
156 | p1 = pt3d(1,1,1) | 157 | p1 = pt3d(1, 1, 1) | ||
157 | p2 = pt3d(2,2,2) | 158 | p2 = pt3d(2, 2, 2) | ||
158 | print(p1 + p2) | 159 | print(p1+p2) | ||
159 | print(p1 - p2) | 160 | print(p1-p2) | ||
160 | print(p1 == p2) | 161 | print(p1==p2) | ||
161 | print(p1+p1 == p2) | 162 | print(p1+p1==p2) | ||
162 | print(p1==p2+pt3d(-1,-1,-1)) | 163 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import pi | n | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,r): | n | 3 | def __init__(self,x): |
4 | self.radius= r | 4 | self.x = x | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius**2*pi | n | 6 | area = math.pi*((self.x)**2) |
7 | return area | ||||
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 8 | return 2*pi*self.radius | n | 9 | perimeter = 2*math.pi*self.x |
10 | return perimeter | ||||
9 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
10 | x = int(input()) | 12 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 17 | self.item_name=name | ||
16 | self.item_description=description | 18 | self.item_description=description | ||
17 | self.item_price=price | 19 | self.item_price=price | ||
18 | self.item_quantity=quantity | 20 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 21 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 22 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 23 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 25 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 26 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 27 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 28 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 29 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 30 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 31 | tremove_item = False | ||
30 | for item in self.cart_items: | 32 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 33 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 34 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 35 | tremove_item = True | ||
34 | break | 36 | break | ||
35 | if not tremove_item: | 37 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 38 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 39 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 40 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 41 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 43 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 44 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 45 | break | ||
44 | if not tmodify_item: | 46 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 47 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 48 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 49 | num_items = 0 | ||
48 | for item in self.cart_items: | 50 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 51 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 52 | return num_items | ||
51 | def get_cost_of_cart(self): | 53 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 54 | total_cost = 0 | ||
53 | cost = 0 | 55 | cost = 0 | ||
54 | for item in self.cart_items: | 56 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 57 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 58 | total_cost += cost | ||
57 | return total_cost | 59 | return total_cost | ||
58 | def print_total(self): | 60 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 61 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 62 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 63 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 64 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 68 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 70 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 71 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 72 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 73 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 74 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 76 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
76 | item.print_item_description() | 78 | item.print_item_description() | ||
77 | def print_menu(newCart): | 79 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 80 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 81 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 82 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 83 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 84 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 85 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 86 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 87 | 'q - Quit\n') | ||
86 | command = '' | 88 | command = '' | ||
87 | while(command != 'q'): | 89 | while(command != 'q'): | ||
88 | print(menu) | 90 | print(menu) | ||
89 | command = input('Choose an option:') | 91 | command = input('Choose an option:') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 92 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
n | 91 | command = input('Choose an option\n:') | n | 93 | command = input('\nChoose an option:') |
92 | if(command == 'a'): | 94 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 95 | print("\nADD ITEM TO CART") | ||
n | 94 | item_name = input('Enter the item name:\n') | n | 96 | item_name = input('Enter item name:\n') |
95 | item_description = input('Enter the item description:\n') | 97 | item_description = input('Enter item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 98 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 99 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 101 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 102 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 103 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 104 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 105 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 107 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 108 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 109 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter name of item to remove:\n') | 110 | itemName = input('Enter name of item to remove:\n') | ||
109 | customer_Cart.remove_item(itemName) | 111 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 112 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 113 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter the item name:\n') | n | 114 | itemName = input('Enter name of item:\n') |
113 | qty = int(input('Enter the new quantity:\n')) | 115 | quantity = int(input('Enter the new quantity:\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 116 | itemToPurchase = ItemToPurchase(itemName,0,quantity) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 117 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 118 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 119 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 120 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 121 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 122 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 123 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) | 124 | print_menu(newCart) | ||
123 | print()import math | 125 | print()import math | ||
124 | class pt3d: | 126 | class pt3d: | ||
125 | def __init__(self,x=0,y=0,z=0): | 127 | def __init__(self,x=0,y=0,z=0): | ||
n | 126 | self.x = x | n | 128 | self.x=x |
127 | self.y = y | 129 | self.y=y | ||
128 | self.z = z | 130 | self.z=z | ||
129 | def __add__(self,point): | 131 | def __add__(self,point): | ||
t | 130 | x1 = self.x + point.x | t | 132 | x1 = self.x+point.x |
131 | y1 = self.y + point.y | 133 | y1 = self.y+point.y | ||
132 | z1 = self.z + point.z | 134 | z1 = self.z+point.z | ||
133 | return pt3d(x1,y1,z1) | 135 | return pt3d(x1, y1, z1) | ||
134 | def __sub__(self,point): | 136 | def __sub__(self,point): | ||
135 | x1 = (self.x-point.x)**2 | 137 | x1 = (self.x-point.x)**2 | ||
136 | y1 = (self.y-point.y)**2 | 138 | y1 = (self.y-point.y)**2 | ||
137 | z1 = (self.z-point.z)**2 | 139 | z1 = (self.z-point.z)**2 | ||
138 | return math.sqrt(x1+y1+z1) | 140 | return math.sqrt(x1+y1+z1) | ||
139 | def __str__(self): | 141 | def __str__(self): | ||
140 | return f"<{self.x},{self.y},{self.z}>" | 142 | return f"<{self.x},{self.y},{self.z}>" | ||
141 | def __eq__(self,point): | 143 | def __eq__(self,point): | ||
142 | if (self.x == point.x) and (self.y == point.y) and (self.x == point.x): | 144 | if (self.x == point.x) and (self.y == point.y) and (self.x == point.x): | ||
143 | return True | 145 | return True | ||
144 | else: | 146 | else: | ||
145 | return False | 147 | return False | ||
146 | a = pt3d(1,1,1) | 148 | a = pt3d(1,1,1) | ||
147 | b = pt3d(2,2,2) | 149 | b = pt3d(2,2,2) | ||
148 | print(a-b) | 150 | print(a-b) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | from math import pi |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,x): | n | 3 | def __init__(self,r): |
4 | self.x = x | 4 | self.radius= r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area = math.pi*((self.x)**2) | n | 6 | return self.radius**2*pi |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter = 2*math.pi*self.x | n | 8 | return 2*pi*self.radius |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
17 | self.item_name=name | 15 | self.item_name=name | ||
18 | self.item_description=description | 16 | self.item_description=description | ||
19 | self.item_price=price | 17 | self.item_price=price | ||
20 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
21 | def print_item_description(self): | 19 | def print_item_description(self): | ||
22 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
23 | class ShoppingCart: | 21 | class ShoppingCart: | ||
24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
25 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 24 | self.current_date = current_date | ||
27 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
28 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
29 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
30 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
31 | tremove_item = False | 29 | tremove_item = False | ||
32 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
33 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
34 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
35 | tremove_item = True | 33 | tremove_item = True | ||
36 | break | 34 | break | ||
37 | if not tremove_item: | 35 | if not tremove_item: | ||
38 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
39 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
40 | tmodify_item = False | 38 | tmodify_item = False | ||
41 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | tmodify_item = True | 41 | tmodify_item = True | ||
44 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
45 | break | 43 | break | ||
46 | if not tmodify_item: | 44 | if not tmodify_item: | ||
47 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
48 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
49 | num_items = 0 | 47 | num_items = 0 | ||
50 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
51 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
52 | return num_items | 50 | return num_items | ||
53 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
54 | total_cost = 0 | 52 | total_cost = 0 | ||
55 | cost = 0 | 53 | cost = 0 | ||
56 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
57 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
58 | total_cost += cost | 56 | total_cost += cost | ||
59 | return total_cost | 57 | return total_cost | ||
60 | def print_total(self): | 58 | def print_total(self): | ||
61 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
62 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
63 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
64 | else: | 62 | else: | ||
65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
67 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
68 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
70 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
74 | else: | 72 | else: | ||
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
76 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
78 | item.print_item_description() | 76 | item.print_item_description() | ||
79 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
80 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
81 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
82 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
83 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
84 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
85 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
86 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
88 | command = '' | 86 | command = '' | ||
89 | while(command != 'q'): | 87 | while(command != 'q'): | ||
90 | print(menu) | 88 | print(menu) | ||
91 | command = input('Choose an option:') | 89 | command = input('Choose an option:') | ||
92 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
n | 93 | command = input('\nChoose an option:') | n | 91 | command = input('Choose an option\n:') |
94 | if(command == 'a'): | 92 | if(command == 'a'): | ||
95 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
n | 96 | item_name = input('Enter item name:\n') | n | 94 | item_name = input('Enter the item name:\n') |
97 | item_description = input('Enter item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
98 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
99 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
101 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
102 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
103 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
104 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
105 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
107 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
108 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
109 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
110 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
111 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
112 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
113 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 114 | itemName = input('Enter name of item:\n') | n | 112 | itemName = input('Enter the item name:\n') |
115 | quantity = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity:\n')) | ||
116 | itemToPurchase = ItemToPurchase(itemName,0,quantity) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
117 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
118 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
119 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
120 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
121 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
122 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
123 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
124 | print_menu(newCart) | 122 | print_menu(newCart) | ||
125 | print()import math | 123 | print()import math | ||
126 | class pt3d: | 124 | class pt3d: | ||
127 | def __init__(self,x=0,y=0,z=0): | 125 | def __init__(self,x=0,y=0,z=0): | ||
n | 128 | self.x=x | n | 126 | self.x = x |
129 | self.y=y | 127 | self.y = y | ||
130 | self.z=z | 128 | self.z = z | ||
131 | def __add__(self,point): | 129 | def __add__(self,point): | ||
t | 132 | x1 = self.x+point.x | t | 130 | x1 = self.x + point.x |
133 | y1 = self.y+point.y | 131 | y1 = self.y + point.y | ||
134 | z1 = self.z+point.z | 132 | z1 = self.z + point.z | ||
135 | return pt3d(x1, y1, z1) | 133 | return pt3d(x1,y1,z1) | ||
136 | def __sub__(self,point): | 134 | def __sub__(self,point): | ||
137 | x1 = (self.x-point.x)**2 | 135 | x1 = (self.x-point.x)**2 | ||
138 | y1 = (self.y-point.y)**2 | 136 | y1 = (self.y-point.y)**2 | ||
139 | z1 = (self.z-point.z)**2 | 137 | z1 = (self.z-point.z)**2 | ||
140 | return math.sqrt(x1+y1+z1) | 138 | return math.sqrt(x1+y1+z1) | ||
141 | def __str__(self): | 139 | def __str__(self): | ||
142 | return f"<{self.x},{self.y},{self.z}>" | 140 | return f"<{self.x},{self.y},{self.z}>" | ||
143 | def __eq__(self,point): | 141 | def __eq__(self,point): | ||
144 | if (self.x == point.x) and (self.y == point.y) and (self.x == point.x): | 142 | if (self.x == point.x) and (self.y == point.y) and (self.x == point.x): | ||
145 | return True | 143 | return True | ||
146 | else: | 144 | else: | ||
147 | return False | 145 | return False | ||
148 | a = pt3d(1,1,1) | 146 | a = pt3d(1,1,1) | ||
149 | b = pt3d(2,2,2) | 147 | b = pt3d(2,2,2) | ||
150 | print(a-b) | 148 | print(a-b) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
n | 4 | self.radius=radius | n | 4 | self.radius = radius |
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*self.radius*self.radius | 6 | return math.pi*self.radius*self.radius | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return math.pi*2*self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description="none"): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
n | n | 18 | self.item_price=price | ||
17 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
n | 18 | self.item_price=price | n | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
n | 39 | for x in range(len(self.cart_items)): | n | 40 | for i in range(len(self.cart_items)): |
40 | if self.cart_items[x].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
n | 42 | self.cart_items[x].item_quantity = itemToPurchase.item_quantity | n | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity |
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
n | 56 | total_cost = total_cost+ cost | n | 57 | total_cost += cost |
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
n | 77 | def print_menu(finalcart): | n | 78 | def print_menu(newCart): |
78 | customer_Cart = finalcart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
n | 81 | 'r - Remove item from the cart\n' | n | 82 | 'r - Remove item from cart\n' |
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
n | 83 | "i - Output item's descriptions\n" | n | 84 | "i - Output items' descriptions\n" |
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:') | n | 90 | command = input('Choose an option:\n') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
n | n | 96 | item_description = input('Enter the item description:\n') | ||
95 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
n | 96 | item_description = input('Enter the item description:\n') | n | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
n | 100 | elif(command =='o'): | n | 101 | elif(command == 'o'): |
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
n | 103 | elif(command =='i'): | n | 104 | elif(command == 'i'): |
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
n | 106 | elif(command =='r'): | n | 107 | elif(command == 'r'): |
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
n | 110 | elif(command =='c'): | n | 111 | elif(command == 'c'): |
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
n | 121 | finalcart = ShoppingCart(customer_name, current_date) | n | 122 | newCart = ShoppingCart(customer_name, current_date) |
122 | print_menu(finalcart) import math | 123 | print_menu(newCart)import math | ||
123 | class pt3d: | 124 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 125 | def __init__(self,x=0,y=0,z=0): | ||
125 | self.x= x | 126 | self.x= x | ||
126 | self.y= y | 127 | self.y= y | ||
127 | self.z= z | 128 | self.z= z | ||
128 | def __add__(self, other): | 129 | def __add__(self, other): | ||
129 | x = self.x + other.x | 130 | x = self.x + other.x | ||
130 | y = self.y + other.y | 131 | y = self.y + other.y | ||
131 | z = self.z + other.z | 132 | z = self.z + other.z | ||
132 | return pt3d(x, y,z) | 133 | return pt3d(x, y,z) | ||
133 | def __sub__(self, other): | 134 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 136 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 137 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 138 | def __str__(self): | ||
n | 138 | return '<{0},{1},{2}>'.format(self.x, self.y, self.z) | n | 139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
139 | if __name__ == '__main__': | 140 | if __name__ == '__main__': | ||
t | 140 | p1 = pt3d(1, 2, 3) | t | 141 | p1 = pt3d(1, 1, 1) |
141 | p2 = pt3d(4, 5, 6) | 142 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 143 | print(p1+p2) | ||
143 | print(p1-p2) | 144 | print(p1-p2) | ||
144 | print(p1==p2) | 145 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 146 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 147 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,my_radius): | 3 | def __init__(self,radius): | ||
4 | self.radius=my_radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (math.pi*(self.radius**2)) | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return(2*math.pi*self.radius) | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
n | 28 | def remove_item(self, Nameitem): | n | 29 | def remove_item(self, itemName): |
29 | removeIt = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
n | 31 | if item.item_name == Nameitem: | n | 32 | if item.item_name == itemName: |
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
n | 33 | removeIt = True | n | 34 | tremove_item = True |
34 | break | 35 | break | ||
n | 35 | if not removeIt: | n | 36 | if not tremove_item: |
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
n | 38 | modifyIt = False | n | 39 | tmodify_item = False |
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 41 | modifyIt = True | n | 42 | tmodify_item = True |
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
n | 44 | if not modifyIt: | n | 45 | if not tmodify_item: |
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 82 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 84 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:') | n | 90 | command = input('Choose an option:\n') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart)import math | 123 | print_menu(newCart)import math | ||
123 | class pt3d: | 124 | class pt3d: | ||
n | 124 | def __init__(self, x=0, y=0, z=0): | n | 125 | def __init__(self,x=0,y=0,z=0): |
125 | self.x=x | 126 | self.x= x | ||
126 | self.y=y | 127 | self.y= y | ||
127 | self.z=z | 128 | self.z= z | ||
128 | def __add__(self, other): | 129 | def __add__(self, other): | ||
129 | x = self.x + other.x | 130 | x = self.x + other.x | ||
130 | y = self.y + other.y | 131 | y = self.y + other.y | ||
131 | z = self.z + other.z | 132 | z = self.z + other.z | ||
n | 132 | return pt3d(x, y, z) | n | 133 | return pt3d(x, y,z) |
133 | def __sub__(self, other): | 134 | def __sub__(self, other): | ||
n | 134 | return math.sqrt(math.pow(other.x-self.x, 2)+ math.pow(other.y-self.y, 2 | n | 135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, |
> | )+ math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __equal__(self, other): | 136 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 137 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 138 | def __str__(self): | ||
n | 138 | return '<{0},{1},{2}>'.format(self.x, self.y, self.z) | n | 139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
139 | if __name__ == '__main__': | 140 | if __name__ == '__main__': | ||
t | 140 | p1 = pt3d(1,1,1) | t | 141 | p1 = pt3d(1, 1, 1) |
141 | p2 = pt3d(2,2,2) | 142 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 143 | print(p1+p2) | ||
143 | print(p1-p2) | 144 | print(p1-p2) | ||
144 | print(p1==p2) | 145 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 146 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 147 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self,x): |
4 | self.radius = radius | 4 | self.x= x | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius**2 | n | 6 | area=(math.pi*(self.x**2)) |
7 | return area | ||||
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 9 | perimeter =2*math.pi*self.x |
10 | return perimeter | ||||
9 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
10 | x = int(input()) | 12 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
n | 12 | print('{:.5f}'.format(NewCircle.area())) | n | 14 | print(NewCircle.area()) |
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 17 | self.item_name=name | ||
16 | self.item_description=description | 18 | self.item_description=description | ||
17 | self.item_price=price | 19 | self.item_price=price | ||
18 | self.item_quantity=quantity | 20 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 21 | def print_item_description(self): | ||
n | 20 | print('%s: %s' % (self.item_name, self.item_description)) | n | 22 | print('%s: %s' % (self.item_name, self.item_description)) |
21 | class ShoppingCart: | 23 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 25 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 26 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 27 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 28 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 29 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 30 | def remove_item(self, itemName): | ||
n | 29 | tremove_item = False | n | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
n | 36 | print('Item not found in cart. Nothing removed.') | n | 37 | print('Item not found in the cart. Nothing removed') |
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
n | 45 | print('Item not found in cart. Nothing modified.') | n | 46 | print('Item not found in the cart. Nothing modified') |
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
n | 76 | item.print_item_description() | n | 77 | item.print_item_description() |
77 | def print_menu(newCart=' '): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
n | 81 | 'r - Remove item from cart\n' | n | 82 | 'r - Remove item from the cart\n' |
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
n | 83 | "i - Output items' descriptions\n" | n | 84 | "i - Output item's descriptions\n" |
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
n | 89 | command = input('Choose an option:\n') | n | 90 | command = input('Choose an option:') |
90 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 91 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c'): | > | = 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | ||
> | , item_description) | > | , item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
n | 101 | print('OUTPUT SHOPPING CART') | n | 102 | print('\nOUTPUT SHOPPING CART') |
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
n | 108 | itemName = input('Enter name of item to remove:\n') | n | 109 | itemName = input('Enter the name of the item to remove :\n') |
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter the item name:\n') | n | 113 | itemName = input('Enter the name of the item :\n') |
113 | qty = int(input('Enter the new quantity:\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 122 | print_menu(newCart) from math import sqrt | n | 123 | print_menu(newCart) |
124 | from math import sqrt | ||||
123 | class pt3d: | 125 | class pt3d: | ||
n | 124 | def __init__(self, x=0, y=0, z=0): | n | 126 | def __init__(self, x, y, z): |
125 | self.x = x | 127 | self.x = x | ||
126 | self.y = y | 128 | self.y = y | ||
127 | self.z = z | 129 | self.z = z | ||
128 | def __add__(self, other): | 130 | def __add__(self, other): | ||
129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 131 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
130 | def __sub__(self, other): | 132 | def __sub__(self, other): | ||
131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 133 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
132 | def __eq__(self, other): | 134 | def __eq__(self, other): | ||
n | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | n | 135 | return self.x == other.x & self.y == other.y & self.z == other.z |
134 | def __str__(self): | 136 | def __str__(self): | ||
t | 135 | return '<{},{},{}>'.format(self.x, self.y, self.z) | t | 137 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
136 | p1 = pt3d(1, 1, 1) | 138 | p1 = pt3d(1, 1, 1) | ||
137 | p2 = pt3d(2, 2, 2) | 139 | p2 = pt3d(2, 2, 2) | ||
138 | print(p1 + p2) | 140 | print(p1 + p2) | ||
139 | print(p1 - p2) | 141 | print(p1 - p2) | ||
140 | print(p1 == p2) | 142 | print(p1 == p2) | ||
141 | print(p1+p1 == p2) | 143 | print(p1+p1 == p2) | ||
142 | print(p1==p2+pt3d(-1, -1, -1)) | 144 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
n | 4 | self.radius=radius | n | 4 | self.radius = radius |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi *self.radius * self.radius | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2* math.pi*self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = float(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
n | 36 | print('Item not found in cart. Nothing removed.') | n | 37 | print('Item not found in the cart. Nothing removed') |
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
n | 45 | print('Item not found in cart. Nothing modified.') | n | 46 | print('Item not found in the cart. Nothing modified') |
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
n | 78 | customer_Cart= newCart | n | 79 | customer_Cart = newCart |
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 82 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 84 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
n | 86 | command = '' | n | 87 | command = '' |
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
89 | command = input('Choose an option:\n') | 90 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
n | 101 | print('OUTPUT SHOPPING CART') | n | 102 | print('\nOUTPUT SHOPPING CART') |
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
n | 108 | itemName = input('Enter name of item to remove:\n') | n | 109 | itemName = input('Enter the name of the item to remove :\n') |
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter the item name:\n') | n | 113 | itemName = input('Enter the name of the item :\n') |
113 | qty = int(input('Enter the new quantity:\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 122 | print_menu(newCart) import math | n | 123 | print_menu(newCart)import math |
123 | class pt3d: | 124 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 125 | def __init__(self,x=0,y=0,z=0): | ||
n | 125 | self.x = x | n | 126 | self.x= x |
126 | self.y = y | 127 | self.y= y | ||
127 | self.z = z | 128 | self.z= z | ||
128 | def __add__(self, other): | 129 | def __add__(self, other): | ||
129 | x = self.x + other.x | 130 | x = self.x + other.x | ||
130 | y = self.y + other.y | 131 | y = self.y + other.y | ||
131 | z = self.z + other.z | 132 | z = self.z + other.z | ||
n | 132 | return pt3d(x,y,z) | n | 133 | return pt3d(x, y,z) |
133 | def __sub__(self, other): | 134 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 135 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, other): | 136 | def __eq__(self, other): | ||
136 | return other.x==self.x and other.y==self.y and other.z==self.z | 137 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 138 | def __str__(self): | ||
t | 138 | return '<{0},{1},{2}>'.format(self.x, self.y, self.z) | t | 139 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
140 | if __name__ == '__main__': | ||||
139 | p1 = pt3d(1, 1, 1) | 141 | p1 = pt3d(1, 1, 1) | ||
140 | p2 = pt3d(2, 2, 2) | 142 | p2 = pt3d(2, 2, 2) | ||
141 | print(p1 + p2) | 143 | print(p1+p2) | ||
142 | print(p1 - p2) | 144 | print(p1-p2) | ||
143 | print(p1 == p2) | 145 | print(p1==p2) | ||
144 | print(p1+p1 == p2) | 146 | print(p1+p1==p2) | ||
145 | print(p1==p2+pt3d(-1, -1, -1)) | 147 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,x): | n | 3 | def __init__(self,radius): |
4 | self.x= x | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area=(math.pi*(self.x**2)) | n | 6 | return math.pi * self.radius**2 |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter =2*math.pi*self.x | n | 8 | return 2 * math.pi * self.radius |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
n | 14 | print(NewCircle.area()) | n | 12 | print('{:.5f}'.format(NewCircle.area())) |
15 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
17 | self.item_name=name | 15 | self.item_name=name | ||
18 | self.item_description=description | 16 | self.item_description=description | ||
19 | self.item_price=price | 17 | self.item_price=price | ||
20 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
21 | def print_item_description(self): | 19 | def print_item_description(self): | ||
n | 22 | print('%s: %s' % (self.item_name, self.item_description)) | n | 20 | print('%s: %s' % (self.item_name, self.item_description)) |
23 | class ShoppingCart: | 21 | class ShoppingCart: | ||
24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
25 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 24 | self.current_date = current_date | ||
27 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
28 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
29 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
30 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
n | n | 29 | tremove_item = False | ||
31 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 33 | tremove_item = True | ||
35 | break | 34 | break | ||
36 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 37 | print('Item not found in the cart. Nothing removed') | n | 36 | print('Item not found in cart. Nothing removed.') |
38 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 38 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 41 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 43 | break | ||
45 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 46 | print('Item not found in the cart. Nothing modified') | n | 45 | print('Item not found in cart. Nothing modified.') |
47 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 47 | num_items = 0 | ||
49 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 50 | return num_items | ||
52 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 52 | total_cost = 0 | ||
54 | cost = 0 | 53 | cost = 0 | ||
55 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 56 | total_cost += cost | ||
58 | return total_cost | 57 | return total_cost | ||
59 | def print_total(self): | 58 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 62 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
75 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
n | 77 | item.print_item_description() | n | 76 | item.print_item_description() |
78 | def print_menu(newCart): | 77 | def print_menu(newCart=' '): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
n | 82 | 'r - Remove item from the cart\n' | n | 81 | 'r - Remove item from cart\n' |
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
n | 84 | "i - Output item's descriptions\n" | n | 83 | "i - Output items' descriptions\n" |
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
n | 90 | command = input('Choose an option:') | n | 89 | command = input('Choose an option:\n') |
91 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 90 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c'): | > | = 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | ||
> | , item_description) | > | , item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
n | 102 | print('\nOUTPUT SHOPPING CART') | n | 101 | print('OUTPUT SHOPPING CART') |
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 109 | itemName = input('Enter the name of the item to remove :\n') | n | 108 | itemName = input('Enter name of item to remove:\n') |
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 113 | itemName = input('Enter the name of the item :\n') | n | 112 | itemName = input('Enter the item name:\n') |
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity:\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 123 | print_menu(newCart) | n | 122 | print_menu(newCart) from math import sqrt |
124 | from math import sqrt | ||||
125 | class pt3d: | 123 | class pt3d: | ||
n | 126 | def __init__(self, x, y, z): | n | 124 | def __init__(self, x=0, y=0, z=0): |
127 | self.x = x | 125 | self.x = x | ||
128 | self.y = y | 126 | self.y = y | ||
129 | self.z = z | 127 | self.z = z | ||
130 | def __add__(self, other): | 128 | def __add__(self, other): | ||
131 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
132 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
133 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
134 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
n | 135 | return self.x == other.x & self.y == other.y & self.z == other.z | n | 133 | return self.x == other.x and self.y == other.y and self.z == other.z |
136 | def __str__(self): | 134 | def __str__(self): | ||
t | 137 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | t | 135 | return '<{},{},{}>'.format(self.x, self.y, self.z) |
138 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
139 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
140 | print(p1 + p2) | 138 | print(p1 + p2) | ||
141 | print(p1 - p2) | 139 | print(p1 - p2) | ||
142 | print(p1 == p2) | 140 | print(p1 == p2) | ||
143 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
144 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius): | n | 3 | def __init__(self, rad): |
4 | self.radius = radius | 4 | self.radius = rad | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return (self.radius**2)*math.pi |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return self.radius*2*math.pi |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input('')) | 10 | x = int(input("")) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = circle(x) | ||
12 | print(NewCircle.area()) | 12 | print(NewCircle.area()) | ||
13 | print(NewCircle.perimeter())class ItemToPurchase: | 13 | print(NewCircle.perimeter())class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 72 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
76 | item.print_item_description() | 76 | item.print_item_description() | ||
77 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
82 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
83 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
86 | command = '' | 86 | command = '' | ||
87 | while(command != 'q'): | 87 | while(command != 'q'): | ||
88 | print(menu) | 88 | print(menu) | ||
89 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 92 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
101 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
109 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 112 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
113 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
122 | print_menu(newCart) import math | 122 | print_menu(newCart) import math | ||
123 | class pt3d: | 123 | class pt3d: | ||
124 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
n | 125 | self.x=x | n | 125 | self.x= x |
126 | self.y=y | 126 | self.y= y | ||
127 | self.z=z | 127 | self.z= z | ||
128 | def __add__(self, ab): | 128 | def __add__(self, other): | ||
129 | x =self.x +ab.x | 129 | x = self.x + other.x | ||
130 | y =self.y +ab.y | 130 | y = self.y + other.y | ||
131 | z =self.z +ab.z | 131 | z = self.z + other.z | ||
132 | return pt3d(x,y,z) | 132 | return pt3d(x, y,z) | ||
133 | def __sub__(self, ap): | 133 | def __sub__(self, other): | ||
134 | return math.sqrt(math.pow(ap.x-self.x, 2) + math.pow(ap.y-self.y, 2) + m | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | ath.pow(ap.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
135 | def __eq__(self, ap): | 135 | def __eq__(self, other): | ||
136 | return ap.x==self.x and ap.y==self.y and ap.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
137 | def __str__(self): | 137 | def __str__(self): | ||
138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
t | 139 | if __name__== '__main__': | t | 139 | if __name__ == '__main__': |
140 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1+p2) | 142 | print(p1+p2) | ||
143 | print(p1-p2) | 143 | print(p1-p2) | ||
144 | print(p1==p2) | 144 | print(p1==p2) | ||
145 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
146 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area = math.pi*self.radius**2 | n | 6 | return math.pi*(self.radius**2) |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter = 2*math.pi*self.radius | n | 8 | return 2*math.pi*self.radius |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
17 | self.item_name=name | 15 | self.item_name=name | ||
18 | self.item_description=description | 16 | self.item_description=description | ||
19 | self.item_price=price | 17 | self.item_price=price | ||
20 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
21 | def print_item_description(self): | 19 | def print_item_description(self): | ||
22 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
23 | class ShoppingCart: | 21 | class ShoppingCart: | ||
24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
25 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 24 | self.current_date = current_date | ||
27 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
28 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
29 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
30 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
31 | tremove_item = False | 29 | tremove_item = False | ||
32 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
33 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
34 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
35 | tremove_item = True | 33 | tremove_item = True | ||
36 | break | 34 | break | ||
37 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 38 | print('Item not found in the cart. Nothing removed') | n | 36 | print('Item not found in the cart. Nothing removed.') |
39 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
40 | tmodify_item = False | 38 | tmodify_item = False | ||
41 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | tmodify_item = True | 41 | tmodify_item = True | ||
44 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
45 | break | 43 | break | ||
46 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 47 | print('Item not found in the cart. Nothing modified') | n | 45 | print('Item not found in the cart. Nothing modified.') |
48 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
49 | num_items = 0 | 47 | num_items = 0 | ||
50 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
51 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
52 | return num_items | 50 | return num_items | ||
53 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
54 | total_cost = 0 | 52 | total_cost = 0 | ||
55 | cost = 0 | 53 | cost = 0 | ||
56 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
57 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
58 | total_cost += cost | 56 | total_cost += cost | ||
59 | return total_cost | 57 | return total_cost | ||
60 | def print_total(self): | 58 | def print_total(self): | ||
61 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
62 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
63 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
64 | else: | 62 | else: | ||
65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
67 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
68 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
70 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
74 | else: | 72 | else: | ||
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
76 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
78 | item.print_item_description() | 76 | item.print_item_description() | ||
79 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
80 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
81 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
82 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
n | 83 | 'r - Remove item from the cart\n' | n | 81 | 'r - Remove item from cart\n' |
84 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
n | 85 | "i - Output item's descriptions\n" | n | 83 | "i - Output items' descriptions\n" |
86 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
88 | command = '' | 86 | command = '' | ||
89 | while(command != 'q'): | 87 | while(command != 'q'): | ||
90 | print(menu) | 88 | print(menu) | ||
n | 91 | command = input('Choose an option:') | n | 89 | command = input('Choose an option:\n') |
92 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
93 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
94 | if(command == 'a'): | 92 | if(command == 'a'): | ||
95 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
96 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
97 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
98 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
99 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
101 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
102 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
103 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
104 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
105 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
107 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
108 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
109 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 110 | itemName = input('Enter the name of the item to remove :\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
111 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
112 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
113 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 114 | itemName = input('Enter the name of the item :\n') | n | 112 | itemName = input('Enter name of item :\n') |
115 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
116 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
117 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
118 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
119 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
120 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
121 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
122 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
123 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 124 | print_menu(newCart) | n | 122 | print_menu(newCart)from math import sqrt |
125 | from math import sqrt | ||||
126 | class pt3d: | 123 | class pt3d: | ||
127 | def init(self, x=0, y=0, z=0): | 124 | def __init__(self, x, y, z): | ||
128 | self.x = x | 125 | self.x = x | ||
129 | self.y = y | 126 | self.y = y | ||
t | 130 | self.z = z | t | 127 | self.z = z |
131 | def add(self, other): | 128 | def __add__(self, other): | ||
132 | points = pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
133 | return points | ||||
134 | def sub(self, other): | 130 | def __sub__(self, other): | ||
135 | points = sqrt((self.x-other.x)2+(self.y-other.y)2+(self.z-other.z)**2) | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | ||||
136 | return points | ||||
137 | def eq(self, other): | 132 | def __eq__(self, other): | ||
138 | return self.x == other.x and self.y == other.y and self.z == other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
139 | def str(self): | 134 | def __str__(self): | ||
140 | return '<{},{},{}>'.format(self.x, self.y, self.z) | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
141 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
142 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
143 | print(p1 + p2) | 138 | print(p1 + p2) | ||
144 | print(p1 - p2) | 139 | print(p1 - p2) | ||
145 | print(p1 == p2) | 140 | print(p1 == p2) | ||
146 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
147 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = int(radius) | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return round(math.pi*self.radius*self.radius,3) | 6 | return math.pi * self.radius**2 | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return round(2*math.pi*self.radius,3) | 8 | return 2 * math.pi * self.radius | ||
9 | circle=Circle(2) | 9 | if __name__=='__main__': | ||
10 | print("Area of circle=",circle.area()) | 10 | x = int(input()) | ||
11 | print("Perimeter of circle=",circle.perimeter())class ItemToPurchase: | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | ||||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||||
12 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription='none'): | > | cription='none'): | ||
13 | self.item_name = item_name | 15 | self.item_name = item_name | ||
14 | self.item_price = item_price | 16 | self.item_price = item_price | ||
15 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
16 | self.item_description = item_description | 18 | self.item_description = item_description | ||
17 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
18 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
19 | (self.item_quantity * self.item_pric | 21 | (self.item_quantity * self.item_pric | ||
> | e)) | > | e)) | ||
20 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
21 | return string, cost | 23 | return string, cost | ||
22 | def print_item_description(self): | 24 | def print_item_description(self): | ||
23 | string = '{}: {}'.format(self.item_name, self.item_description) | 25 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
24 | print(string, end='\n') | 26 | print(string, end='\n') | ||
25 | return string | 27 | return string | ||
26 | class ShoppingCart: | 28 | class ShoppingCart: | ||
27 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | 29 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | ||
> | t_items=[]): | > | t_items=[]): | ||
28 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
29 | self.current_date = current_date | 31 | self.current_date = current_date | ||
30 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
31 | def add_item(self, string): | 33 | def add_item(self, string): | ||
32 | print('\nADD ITEM TO CART', end='\n') | 34 | print('\nADD ITEM TO CART', end='\n') | ||
33 | item_name = str(input('Enter the item name: ')) | 35 | item_name = str(input('Enter the item name: ')) | ||
34 | item_description = str(input('\nEnter the item description: ')) | 36 | item_description = str(input('\nEnter the item description: ')) | ||
35 | item_price = int(input('\nEnter the item price: ')) | 37 | item_price = int(input('\nEnter the item price: ')) | ||
36 | item_quantity = int(input('\nEnter the item quantity: ')) | 38 | item_quantity = int(input('\nEnter the item quantity: ')) | ||
37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
38 | def remove_item(self): | 40 | def remove_item(self): | ||
39 | print('\nREMOVE ITEM FROM CART', end='\n') | 41 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
40 | string = str(input('Enter name of item to remove: ')) | 42 | string = str(input('Enter name of item to remove: ')) | ||
41 | i = 0 | 43 | i = 0 | ||
42 | for item in self.cart_items: | 44 | for item in self.cart_items: | ||
43 | if (item.item_name == string): | 45 | if (item.item_name == string): | ||
44 | del self.cart_items[i] | 46 | del self.cart_items[i] | ||
45 | i += 1 | 47 | i += 1 | ||
46 | flag = True | 48 | flag = True | ||
47 | break | 49 | break | ||
48 | else: | 50 | else: | ||
49 | flag = False | 51 | flag = False | ||
50 | if (flag == False): | 52 | if (flag == False): | ||
51 | print('Item not found in cart. Nothing removed') | 53 | print('Item not found in cart. Nothing removed') | ||
52 | def modify_item(self): | 54 | def modify_item(self): | ||
53 | print('\nCHANGE ITEM QUANTITY', end='\n') | 55 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
54 | name = str(input('Enter the item name: ')) | 56 | name = str(input('Enter the item name: ')) | ||
55 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
56 | if (item.item_name == name): | 58 | if (item.item_name == name): | ||
57 | quantity = int(input('Enter the new quantity: ')) | 59 | quantity = int(input('Enter the new quantity: ')) | ||
58 | item.item_quantity = quantity | 60 | item.item_quantity = quantity | ||
59 | flag = True | 61 | flag = True | ||
60 | break | 62 | break | ||
61 | else: | 63 | else: | ||
62 | flag = False | 64 | flag = False | ||
63 | if (flag == False): | 65 | if (flag == False): | ||
64 | print('Item not found in cart. Nothing modified') | 66 | print('Item not found in cart. Nothing modified') | ||
65 | def get_num_items_in_cart(self): | 67 | def get_num_items_in_cart(self): | ||
66 | num_items = 0 | 68 | num_items = 0 | ||
67 | for item in self.cart_items: | 69 | for item in self.cart_items: | ||
68 | num_items = num_items + item.item_quantity | 70 | num_items = num_items + item.item_quantity | ||
69 | return num_items | 71 | return num_items | ||
70 | def get_cost_of_cart(self): | 72 | def get_cost_of_cart(self): | ||
71 | total_cost = 0 | 73 | total_cost = 0 | ||
72 | cost = 0 | 74 | cost = 0 | ||
73 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
74 | cost = (item.item_quantity * item.item_price) | 76 | cost = (item.item_quantity * item.item_price) | ||
75 | total_cost += cost | 77 | total_cost += cost | ||
76 | return total_cost | 78 | return total_cost | ||
77 | def print_total(self): | 79 | def print_total(self): | ||
78 | total_cost = self.get_cost_of_cart() | 80 | total_cost = self.get_cost_of_cart() | ||
79 | if (total_cost == 0): | 81 | if (total_cost == 0): | ||
80 | print('SHOPPING CART IS EMPTY') | 82 | print('SHOPPING CART IS EMPTY') | ||
81 | else: | 83 | else: | ||
82 | self.output_cart() | 84 | self.output_cart() | ||
83 | def print_descriptions(self): | 85 | def print_descriptions(self): | ||
84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 86 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
86 | print('\nItem Descriptions', end='\n') | 88 | print('\nItem Descriptions', end='\n') | ||
87 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 90 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
89 | def output_cart(self): | 91 | def output_cart(self): | ||
90 | new = ShoppingCart() | 92 | new = ShoppingCart() | ||
91 | print('\nOUTPUT SHOPPING CART', end='\n') | 93 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 94 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 95 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
94 | tc = 0 | 96 | tc = 0 | ||
95 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 98 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
97 | item.item_price, (item.item_quantit | 99 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
98 | tc += (item.item_quantity * item.item_price) | 100 | tc += (item.item_quantity * item.item_price) | ||
99 | print('\nTotal: ${}'.format(tc), end='\n') | 101 | print('\nTotal: ${}'.format(tc), end='\n') | ||
100 | def print_menu(ShoppingCart): | 102 | def print_menu(ShoppingCart): | ||
101 | customer_Cart = newCart | 103 | customer_Cart = newCart | ||
102 | string = '' | 104 | string = '' | ||
103 | menu = ('\nMENU\n' | 105 | menu = ('\nMENU\n' | ||
104 | 'a - Add item to cart\n' | 106 | 'a - Add item to cart\n' | ||
105 | 'r - Remove item from cart\n' | 107 | 'r - Remove item from cart\n' | ||
106 | 'c - Change item quantity\n' | 108 | 'c - Change item quantity\n' | ||
107 | 'i - Output items\' descriptions\n' | 109 | 'i - Output items\' descriptions\n' | ||
108 | 'o - Output shopping cart\n' | 110 | 'o - Output shopping cart\n' | ||
109 | 'q - Quit\n') | 111 | 'q - Quit\n') | ||
110 | command = '' | 112 | command = '' | ||
111 | while (command != 'q'): | 113 | while (command != 'q'): | ||
112 | string = '' | 114 | string = '' | ||
113 | print(menu, end='\n') | 115 | print(menu, end='\n') | ||
114 | command = input('Choose an option: ') | 116 | command = input('Choose an option: ') | ||
115 | while (command != 'a' and command != 'o' and command != 'i' and command | 117 | while (command != 'a' and command != 'o' and command != 'i' and command | ||
> | != 'r' | > | != 'r' | ||
116 | and command != 'c' and command != 'q'): | 118 | and command != 'c' and command != 'q'): | ||
117 | command = input('Choose an option: ') | 119 | command = input('Choose an option: ') | ||
118 | if (command == 'a'): | 120 | if (command == 'a'): | ||
119 | customer_Cart.add_item(string) | 121 | customer_Cart.add_item(string) | ||
120 | if (command == 'o'): | 122 | if (command == 'o'): | ||
121 | customer_Cart.output_cart() | 123 | customer_Cart.output_cart() | ||
122 | if (command == 'i'): | 124 | if (command == 'i'): | ||
123 | customer_Cart.print_descriptions() | 125 | customer_Cart.print_descriptions() | ||
124 | if (command == 'r'): | 126 | if (command == 'r'): | ||
125 | customer_Cart.remove_item() | 127 | customer_Cart.remove_item() | ||
126 | if (command == 'c'): | 128 | if (command == 'c'): | ||
127 | customer_Cart.modify_item() | 129 | customer_Cart.modify_item() | ||
128 | customer_name = str(input('Enter customer\'s name: ')) | 130 | customer_name = str(input('Enter customer\'s name: ')) | ||
129 | current_date = str(input('\nEnter today\'s date: ')) | 131 | current_date = str(input('\nEnter today\'s date: ')) | ||
130 | print('Customer name:', customer_name, end='\n') | 132 | print('Customer name:', customer_name, end='\n') | ||
131 | print('Today\'s date:', current_date, end='\n') | 133 | print('Today\'s date:', current_date, end='\n') | ||
132 | newCart = ShoppingCart(customer_name, current_date) | 134 | newCart = ShoppingCart(customer_name, current_date) | ||
133 | print_menu(newCart)from math import sqrt | 135 | print_menu(newCart)from math import sqrt | ||
134 | class pt3d: | 136 | class pt3d: | ||
n | 135 | def __init__(self, x, y, z): | n | 137 | def __init__(self, x=0, y=0, z=0): |
136 | self.x = x | 138 | self.x = x | ||
137 | self.y = y | 139 | self.y = y | ||
138 | self.z = z | 140 | self.z = z | ||
139 | def __add__(self, other): | 141 | def __add__(self, other): | ||
140 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 142 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
141 | def __sub__(self, other): | 143 | def __sub__(self, other): | ||
142 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 144 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
143 | def __eq__(self, other): | 145 | def __eq__(self, other): | ||
144 | return self.x == other.x & self.y == other.y & self.z == other.z | 146 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
145 | def __str__(self): | 147 | def __str__(self): | ||
146 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 148 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
t | t | 149 | if __name__ == '__main__': | ||
147 | p1 = pt3d(1, 1, 1) | 150 | p1 = pt3d(1, 1, 1) | ||
148 | p2 = pt3d(2, 2, 2) | 151 | p2 = pt3d(2, 2, 2) | ||
149 | print(p1 + p2) | 152 | print(p1+p2) | ||
150 | print(p1 - p2) | 153 | print(p1-p2) | ||
151 | print(p1 == p2) | 154 | print(p1==p2) | ||
152 | print(p1+p1 == p2) | 155 | print(p1+p1==p2) | ||
153 | print(p1==p2+pt3d(-1, -1, -1)) | 156 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle(): | n | 2 | class Circle: |
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = int(radius) | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi * self.radius**2 | 6 | return round(math.pi*self.radius*self.radius,3) | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2 * math.pi * self.radius | 8 | return round(2*math.pi*self.radius,3) | ||
9 | if __name__=='__main__': | 9 | circle=Circle(2) | ||
10 | x = int(input()) | 10 | print("Area of circle=",circle.area()) | ||
11 | NewCircle = Circle(x) | 11 | print("Perimeter of circle=",circle.perimeter())class ItemToPurchase: | ||
12 | print('{:.5f}'.format(NewCircle.area())) | ||||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 12 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription='none'): | > | cription='none'): | ||
15 | self.item_name = item_name | 13 | self.item_name = item_name | ||
16 | self.item_price = item_price | 14 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 15 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 16 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 17 | def print_item_cost(self): | ||
20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 18 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
21 | (self.item_quantity * self.item_pric | 19 | (self.item_quantity * self.item_pric | ||
> | e)) | > | e)) | ||
22 | cost = self.item_quantity * self.item_price | 20 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 21 | return string, cost | ||
24 | def print_item_description(self): | 22 | def print_item_description(self): | ||
25 | string = '{}: {}'.format(self.item_name, self.item_description) | 23 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
26 | print(string, end='\n') | 24 | print(string, end='\n') | ||
27 | return string | 25 | return string | ||
28 | class ShoppingCart: | 26 | class ShoppingCart: | ||
29 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | 27 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | ||
> | t_items=[]): | > | t_items=[]): | ||
30 | self.customer_name = customer_name | 28 | self.customer_name = customer_name | ||
31 | self.current_date = current_date | 29 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 30 | self.cart_items = cart_items | ||
33 | def add_item(self, string): | 31 | def add_item(self, string): | ||
34 | print('\nADD ITEM TO CART', end='\n') | 32 | print('\nADD ITEM TO CART', end='\n') | ||
35 | item_name = str(input('Enter the item name: ')) | 33 | item_name = str(input('Enter the item name: ')) | ||
36 | item_description = str(input('\nEnter the item description: ')) | 34 | item_description = str(input('\nEnter the item description: ')) | ||
37 | item_price = int(input('\nEnter the item price: ')) | 35 | item_price = int(input('\nEnter the item price: ')) | ||
38 | item_quantity = int(input('\nEnter the item quantity: ')) | 36 | item_quantity = int(input('\nEnter the item quantity: ')) | ||
39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
40 | def remove_item(self): | 38 | def remove_item(self): | ||
41 | print('\nREMOVE ITEM FROM CART', end='\n') | 39 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
42 | string = str(input('Enter name of item to remove: ')) | 40 | string = str(input('Enter name of item to remove: ')) | ||
43 | i = 0 | 41 | i = 0 | ||
44 | for item in self.cart_items: | 42 | for item in self.cart_items: | ||
45 | if (item.item_name == string): | 43 | if (item.item_name == string): | ||
46 | del self.cart_items[i] | 44 | del self.cart_items[i] | ||
47 | i += 1 | 45 | i += 1 | ||
48 | flag = True | 46 | flag = True | ||
49 | break | 47 | break | ||
50 | else: | 48 | else: | ||
51 | flag = False | 49 | flag = False | ||
52 | if (flag == False): | 50 | if (flag == False): | ||
53 | print('Item not found in cart. Nothing removed') | 51 | print('Item not found in cart. Nothing removed') | ||
54 | def modify_item(self): | 52 | def modify_item(self): | ||
55 | print('\nCHANGE ITEM QUANTITY', end='\n') | 53 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
56 | name = str(input('Enter the item name: ')) | 54 | name = str(input('Enter the item name: ')) | ||
57 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
58 | if (item.item_name == name): | 56 | if (item.item_name == name): | ||
59 | quantity = int(input('Enter the new quantity: ')) | 57 | quantity = int(input('Enter the new quantity: ')) | ||
60 | item.item_quantity = quantity | 58 | item.item_quantity = quantity | ||
61 | flag = True | 59 | flag = True | ||
62 | break | 60 | break | ||
63 | else: | 61 | else: | ||
64 | flag = False | 62 | flag = False | ||
65 | if (flag == False): | 63 | if (flag == False): | ||
66 | print('Item not found in cart. Nothing modified') | 64 | print('Item not found in cart. Nothing modified') | ||
67 | def get_num_items_in_cart(self): | 65 | def get_num_items_in_cart(self): | ||
68 | num_items = 0 | 66 | num_items = 0 | ||
69 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
70 | num_items = num_items + item.item_quantity | 68 | num_items = num_items + item.item_quantity | ||
71 | return num_items | 69 | return num_items | ||
72 | def get_cost_of_cart(self): | 70 | def get_cost_of_cart(self): | ||
73 | total_cost = 0 | 71 | total_cost = 0 | ||
74 | cost = 0 | 72 | cost = 0 | ||
75 | for item in self.cart_items: | 73 | for item in self.cart_items: | ||
76 | cost = (item.item_quantity * item.item_price) | 74 | cost = (item.item_quantity * item.item_price) | ||
77 | total_cost += cost | 75 | total_cost += cost | ||
78 | return total_cost | 76 | return total_cost | ||
79 | def print_total(self): | 77 | def print_total(self): | ||
80 | total_cost = self.get_cost_of_cart() | 78 | total_cost = self.get_cost_of_cart() | ||
81 | if (total_cost == 0): | 79 | if (total_cost == 0): | ||
82 | print('SHOPPING CART IS EMPTY') | 80 | print('SHOPPING CART IS EMPTY') | ||
83 | else: | 81 | else: | ||
84 | self.output_cart() | 82 | self.output_cart() | ||
85 | def print_descriptions(self): | 83 | def print_descriptions(self): | ||
86 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
88 | print('\nItem Descriptions', end='\n') | 86 | print('\nItem Descriptions', end='\n') | ||
89 | for item in self.cart_items: | 87 | for item in self.cart_items: | ||
90 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
91 | def output_cart(self): | 89 | def output_cart(self): | ||
92 | new = ShoppingCart() | 90 | new = ShoppingCart() | ||
93 | print('\nOUTPUT SHOPPING CART', end='\n') | 91 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
94 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
95 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
96 | tc = 0 | 94 | tc = 0 | ||
97 | for item in self.cart_items: | 95 | for item in self.cart_items: | ||
98 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
99 | item.item_price, (item.item_quantit | 97 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
100 | tc += (item.item_quantity * item.item_price) | 98 | tc += (item.item_quantity * item.item_price) | ||
101 | print('\nTotal: ${}'.format(tc), end='\n') | 99 | print('\nTotal: ${}'.format(tc), end='\n') | ||
102 | def print_menu(ShoppingCart): | 100 | def print_menu(ShoppingCart): | ||
103 | customer_Cart = newCart | 101 | customer_Cart = newCart | ||
104 | string = '' | 102 | string = '' | ||
105 | menu = ('\nMENU\n' | 103 | menu = ('\nMENU\n' | ||
106 | 'a - Add item to cart\n' | 104 | 'a - Add item to cart\n' | ||
107 | 'r - Remove item from cart\n' | 105 | 'r - Remove item from cart\n' | ||
108 | 'c - Change item quantity\n' | 106 | 'c - Change item quantity\n' | ||
109 | 'i - Output items\' descriptions\n' | 107 | 'i - Output items\' descriptions\n' | ||
110 | 'o - Output shopping cart\n' | 108 | 'o - Output shopping cart\n' | ||
111 | 'q - Quit\n') | 109 | 'q - Quit\n') | ||
112 | command = '' | 110 | command = '' | ||
113 | while (command != 'q'): | 111 | while (command != 'q'): | ||
114 | string = '' | 112 | string = '' | ||
115 | print(menu, end='\n') | 113 | print(menu, end='\n') | ||
116 | command = input('Choose an option: ') | 114 | command = input('Choose an option: ') | ||
117 | while (command != 'a' and command != 'o' and command != 'i' and command | 115 | while (command != 'a' and command != 'o' and command != 'i' and command | ||
> | != 'r' | > | != 'r' | ||
118 | and command != 'c' and command != 'q'): | 116 | and command != 'c' and command != 'q'): | ||
119 | command = input('Choose an option: ') | 117 | command = input('Choose an option: ') | ||
120 | if (command == 'a'): | 118 | if (command == 'a'): | ||
121 | customer_Cart.add_item(string) | 119 | customer_Cart.add_item(string) | ||
122 | if (command == 'o'): | 120 | if (command == 'o'): | ||
123 | customer_Cart.output_cart() | 121 | customer_Cart.output_cart() | ||
124 | if (command == 'i'): | 122 | if (command == 'i'): | ||
125 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
126 | if (command == 'r'): | 124 | if (command == 'r'): | ||
127 | customer_Cart.remove_item() | 125 | customer_Cart.remove_item() | ||
128 | if (command == 'c'): | 126 | if (command == 'c'): | ||
129 | customer_Cart.modify_item() | 127 | customer_Cart.modify_item() | ||
130 | customer_name = str(input('Enter customer\'s name: ')) | 128 | customer_name = str(input('Enter customer\'s name: ')) | ||
131 | current_date = str(input('\nEnter today\'s date: ')) | 129 | current_date = str(input('\nEnter today\'s date: ')) | ||
132 | print('Customer name:', customer_name, end='\n') | 130 | print('Customer name:', customer_name, end='\n') | ||
133 | print('Today\'s date:', current_date, end='\n') | 131 | print('Today\'s date:', current_date, end='\n') | ||
134 | newCart = ShoppingCart(customer_name, current_date) | 132 | newCart = ShoppingCart(customer_name, current_date) | ||
135 | print_menu(newCart)from math import sqrt | 133 | print_menu(newCart)from math import sqrt | ||
136 | class pt3d: | 134 | class pt3d: | ||
n | 137 | def __init__(self, x=0, y=0, z=0): | n | 135 | def __init__(self, x, y, z): |
138 | self.x = x | 136 | self.x = x | ||
139 | self.y = y | 137 | self.y = y | ||
140 | self.z = z | 138 | self.z = z | ||
141 | def __add__(self, other): | 139 | def __add__(self, other): | ||
142 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 140 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
143 | def __sub__(self, other): | 141 | def __sub__(self, other): | ||
144 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 142 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
145 | def __eq__(self, other): | 143 | def __eq__(self, other): | ||
146 | return self.x == other.x & self.y == other.y & self.z == other.z | 144 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
147 | def __str__(self): | 145 | def __str__(self): | ||
148 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | 146 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||
t | 149 | if __name__ == '__main__': | t | ||
150 | p1 = pt3d(1, 1, 1) | 147 | p1 = pt3d(1, 1, 1) | ||
151 | p2 = pt3d(2, 2, 2) | 148 | p2 = pt3d(2, 2, 2) | ||
152 | print(p1+p2) | 149 | print(p1 + p2) | ||
153 | print(p1-p2) | 150 | print(p1 - p2) | ||
154 | print(p1==p2) | 151 | print(p1 == p2) | ||
155 | print(p1+p1==p2) | 152 | print(p1+p1 == p2) | ||
156 | print(p1==p2+pt3d(-1,-1,-1)) | 153 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*self.radius**2 | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return math.pi*2*self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
n | 20 | print('{}: {}'.format(self.item_name,self.item_description)) | n | 20 | print('%s: %s' % (self.item_name, self.item_description)) |
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 36 | print('Item not found in the cart. Nothing removed') | n | 36 | print('Item not found in the cart. Nothing removed.') |
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
n | 39 | for n in range(len(self.cart_items)): | n | 39 | for i in range(len(self.cart_items)): |
40 | if self.cart_items[n].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
n | 42 | self.cart_items[n].item_quantity = itemToPurchase.item_quantity | n | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity |
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 45 | print('Item not found in the cart. Nothing modified') | n | 45 | print('Item not found in the cart. Nothing modified.') |
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
n | 49 | num_items += item.item_quantity | n | 49 | num_items = num_items + item.item_quantity |
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
n | 57 | return total_cost | n | 57 | return total_cost |
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 62 | else: | ||
n | 63 | print("{}'s Shopping Cart - {}".format(self.customer_name, self.curre | n | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | nt_date)) | > | ent_date)) | ||
64 | print("Number of Items: {} ".format(self.get_num_items_in_cart())) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
n | 67 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quanti | n | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, |
> | ty, item.item_price, total)) | > | item.item_price, total)) | ||
68 | print("Total: ${}".format(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 72 | else: | ||
n | 73 | print("{}'s Shopping Cart - {}".format(self.customer_name, self.curre | n | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | nt_date)) | > | ent_date)) | ||
74 | print() | ||||
75 | print('Item Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
n | 82 | 'r - Remove item from the cart\n' | n | 81 | 'r - Remove item from cart\n' |
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
n | 84 | "i - Output item's descriptions\n" | n | 83 | "i - Output items' descriptions\n" |
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
n | 90 | command = input('Choose an option:') | n | 89 | command = input('Choose an option:\n') |
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
102 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
103 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 109 | itemName = input('Enter the name of the item to remove :\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
110 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 113 | itemName = input('Enter the name of the item :\n') | n | 112 | itemName = input('Enter name of item :\n') |
114 | qty = int(input('Enter the new quantity :\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
n | 120 | print() | n | ||
121 | print("Customer name: {}".format(customer_name)) | 119 | print("\nCustomer name: %s" %customer_name) | ||
122 | print("Today's date: {}".format(current_date)) | 120 | print("Today's date: %s" %current_date) | ||
123 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 124 | print_menu(newCart)import math | n | 122 | print_menu(newCart)from math import sqrt |
125 | class pt3d: | 123 | class pt3d: | ||
n | 126 | def __init__(self, x=0, y=0, z=0): | n | 124 | def __init__(self, x, y, z): |
127 | self.x = x | 125 | self.x = x | ||
128 | self.y = y | 126 | self.y = y | ||
129 | self.z = z | 127 | self.z = z | ||
130 | def __add__(self, other): | 128 | def __add__(self, other): | ||
131 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
132 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
n | 133 | return math.sqrt((other.x - self.x)**2 + (other.z - self.z)**2 + (other. | n | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot |
> | y - self.y)**2) | > | her.z)**2) | ||
134 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
135 | return self.x == other.x and self.y == other.y and self.z == other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
136 | def __str__(self): | 134 | def __str__(self): | ||
t | 137 | return '<{},{},{}>'.format(self.x, self.y, self.z) | t | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
138 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
139 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
140 | print(p1 + p2) | 138 | print(p1 + p2) | ||
141 | print(p1 - p2) | 139 | print(p1 - p2) | ||
142 | print(p1 == p2) | 140 | print(p1 == p2) | ||
143 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
144 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*self.radius*self.radius | 6 | return math.pi*self.radius*self.radius | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
8 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
n | 9 | if __name__=='__main__': | n | 9 | if __name__ == '__main__': |
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
21 | (self.item_quantity * self.item_price)) | 21 | (self.item_quantity * self.item_price)) | ||
22 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 23 | return string, cost | ||
24 | def print_item_description(self): | 24 | def print_item_description(self): | ||
25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
26 | print(string) | 26 | print(string) | ||
27 | return string | 27 | return string | ||
28 | class ShoppingCart: | 28 | class ShoppingCart: | ||
29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
30 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
31 | self.current_date = current_date | 31 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
33 | def add_item(self): | 33 | def add_item(self): | ||
34 | print('ADD ITEM TO CART') | 34 | print('ADD ITEM TO CART') | ||
35 | item_name = str(input('Enter the item name:')) | 35 | item_name = str(input('Enter the item name:')) | ||
36 | print() | 36 | print() | ||
37 | item_description = str(input('Enter the item description:')) | 37 | item_description = str(input('Enter the item description:')) | ||
38 | print() | 38 | print() | ||
39 | item_price = int(input('Enter the item price:')) | 39 | item_price = int(input('Enter the item price:')) | ||
40 | print() | 40 | print() | ||
41 | item_quantity = int(input('Enter the item quantity:')) | 41 | item_quantity = int(input('Enter the item quantity:')) | ||
42 | print() | 42 | print() | ||
43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
44 | def remove_item(self): | 44 | def remove_item(self): | ||
45 | print('REMOVE ITEM FROM CART') | 45 | print('REMOVE ITEM FROM CART') | ||
46 | string = str(input('Enter name of item to remove:')) | 46 | string = str(input('Enter name of item to remove:')) | ||
47 | print() | 47 | print() | ||
48 | i = 0 | 48 | i = 0 | ||
49 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
50 | if(item.item_name == string): | 50 | if(item.item_name == string): | ||
51 | del self.cart_items[i] | 51 | del self.cart_items[i] | ||
52 | flag=True | 52 | flag=True | ||
53 | break | 53 | break | ||
54 | else: | 54 | else: | ||
55 | flag=False | 55 | flag=False | ||
56 | i += 1 | 56 | i += 1 | ||
57 | if(flag==False): | 57 | if(flag==False): | ||
58 | print('Item not found in cart. Nothing removed.') | 58 | print('Item not found in cart. Nothing removed.') | ||
59 | def modify_item(self): | 59 | def modify_item(self): | ||
60 | print('CHANGE ITEM QUANTITY') | 60 | print('CHANGE ITEM QUANTITY') | ||
61 | name = str(input('Enter the item name:')) | 61 | name = str(input('Enter the item name:')) | ||
62 | print() | 62 | print() | ||
63 | for item in self.cart_items: | 63 | for item in self.cart_items: | ||
64 | if(item.item_name == name): | 64 | if(item.item_name == name): | ||
65 | quantity = int(input('Enter the new quantity:')) | 65 | quantity = int(input('Enter the new quantity:')) | ||
66 | print() | 66 | print() | ||
67 | item.item_quantity = quantity | 67 | item.item_quantity = quantity | ||
68 | flag=True | 68 | flag=True | ||
69 | break | 69 | break | ||
70 | else: | 70 | else: | ||
71 | flag=False | 71 | flag=False | ||
72 | if(flag==False): | 72 | if(flag==False): | ||
73 | print('Item not found in cart. Nothing modified.') | 73 | print('Item not found in cart. Nothing modified.') | ||
74 | print() | 74 | print() | ||
75 | def get_num_items_in_cart(self): | 75 | def get_num_items_in_cart(self): | ||
76 | num_items=0 | 76 | num_items=0 | ||
77 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
78 | num_items= num_items+item.item_quantity | 78 | num_items= num_items+item.item_quantity | ||
79 | return num_items | 79 | return num_items | ||
80 | def get_cost_of_cart(self): | 80 | def get_cost_of_cart(self): | ||
81 | total_cost = 0 | 81 | total_cost = 0 | ||
82 | cost = 0 | 82 | cost = 0 | ||
83 | for item in self.cart_items: | 83 | for item in self.cart_items: | ||
84 | cost = (item.item_quantity * item.item_price) | 84 | cost = (item.item_quantity * item.item_price) | ||
85 | total_cost += cost | 85 | total_cost += cost | ||
86 | return total_cost | 86 | return total_cost | ||
87 | def print_total(self): | 87 | def print_total(self): | ||
88 | total_cost = self.get_cost_of_cart() | 88 | total_cost = self.get_cost_of_cart() | ||
89 | if (total_cost == 0): | 89 | if (total_cost == 0): | ||
90 | print('SHOPPING CART IS EMPTY') | 90 | print('SHOPPING CART IS EMPTY') | ||
91 | else: | 91 | else: | ||
92 | self.output_cart() | 92 | self.output_cart() | ||
93 | def print_descriptions(self): | 93 | def print_descriptions(self): | ||
94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
96 | print('\nItem Descriptions') | 96 | print('\nItem Descriptions') | ||
97 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
98 | print('{}: {}'.format(item.item_name, item.item_description)) | 98 | print('{}: {}'.format(item.item_name, item.item_description)) | ||
99 | def output_cart(self): | 99 | def output_cart(self): | ||
100 | print('OUTPUT SHOPPING CART') | 100 | print('OUTPUT SHOPPING CART') | ||
101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
102 | print('Number of Items:', self.get_num_items_in_cart()) | 102 | print('Number of Items:', self.get_num_items_in_cart()) | ||
103 | print() | 103 | print() | ||
104 | tc = 0 | 104 | tc = 0 | ||
105 | for item in self.cart_items: | 105 | for item in self.cart_items: | ||
106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
107 | item.item_price, (item.item_quantity * item.item_price))) | 107 | item.item_price, (item.item_quantity * item.item_price))) | ||
108 | tc += (item.item_quantity * item.item_price) | 108 | tc += (item.item_quantity * item.item_price) | ||
109 | if len(self.cart_items) == 0: | 109 | if len(self.cart_items) == 0: | ||
110 | print('SHOPPING CART IS EMPTY') | 110 | print('SHOPPING CART IS EMPTY') | ||
111 | print() | 111 | print() | ||
112 | print('Total: ${}'.format(tc)) | 112 | print('Total: ${}'.format(tc)) | ||
113 | def print_menu(customer_Cart): | 113 | def print_menu(customer_Cart): | ||
114 | menu = ('\nMENU\n' | 114 | menu = ('\nMENU\n' | ||
115 | 'a - Add item to cart\n' | 115 | 'a - Add item to cart\n' | ||
116 | 'r - Remove item from cart\n' | 116 | 'r - Remove item from cart\n' | ||
117 | 'c - Change item quantity\n' | 117 | 'c - Change item quantity\n' | ||
118 | 'i - Output items\' descriptions\n' | 118 | 'i - Output items\' descriptions\n' | ||
119 | 'o - Output shopping cart\n' | 119 | 'o - Output shopping cart\n' | ||
120 | 'q - Quit\n') | 120 | 'q - Quit\n') | ||
121 | command = '' | 121 | command = '' | ||
122 | while(command != 'q'): | 122 | while(command != 'q'): | ||
123 | print(menu) | 123 | print(menu) | ||
124 | command = input('Choose an option:') | 124 | command = input('Choose an option:') | ||
125 | print() | 125 | print() | ||
126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' | > | = 'r' | ||
127 | and command != 'c' and command != 'q'): | 127 | and command != 'c' and command != 'q'): | ||
128 | command = input('Choose an option:') | 128 | command = input('Choose an option:') | ||
129 | print() | 129 | print() | ||
130 | if(command == 'a'): | 130 | if(command == 'a'): | ||
131 | customer_Cart.add_item() | 131 | customer_Cart.add_item() | ||
132 | if(command == 'o'): | 132 | if(command == 'o'): | ||
133 | customer_Cart.output_cart() | 133 | customer_Cart.output_cart() | ||
134 | if(command == 'i'): | 134 | if(command == 'i'): | ||
135 | customer_Cart.print_descriptions() | 135 | customer_Cart.print_descriptions() | ||
136 | if(command == 'r'): | 136 | if(command == 'r'): | ||
137 | customer_Cart.remove_item() | 137 | customer_Cart.remove_item() | ||
138 | if(command == 'c'): | 138 | if(command == 'c'): | ||
139 | customer_Cart.modify_item() | 139 | customer_Cart.modify_item() | ||
140 | def main(): | 140 | def main(): | ||
141 | customer_name = str(input('Enter customer\'s name:')) | 141 | customer_name = str(input('Enter customer\'s name:')) | ||
142 | print() | 142 | print() | ||
143 | current_date = str(input('Enter today\'s date:')) | 143 | current_date = str(input('Enter today\'s date:')) | ||
144 | print('\n') | 144 | print('\n') | ||
145 | print('Customer name:', customer_name, end='\n') | 145 | print('Customer name:', customer_name, end='\n') | ||
146 | print('Today\'s date:', current_date, end='\n') | 146 | print('Today\'s date:', current_date, end='\n') | ||
147 | newCart = ShoppingCart(customer_name, current_date) | 147 | newCart = ShoppingCart(customer_name, current_date) | ||
148 | print_menu(newCart) | 148 | print_menu(newCart) | ||
149 | if __name__ == '__main__': | 149 | if __name__ == '__main__': | ||
n | 150 | main()from math import sqrt | n | 150 | main()import math |
151 | class pt3d: | 151 | class pt3d: | ||
n | 152 | def __init__(self, x=0, y=0, z=0): | n | 152 | def __init__(self,x=0,y=0,z=0): |
153 | self.x = x | 153 | self.x= x | ||
154 | self.y = y | 154 | self.y= y | ||
155 | self.z = z | 155 | self.z= z | ||
156 | def __add__(self, other): | 156 | def __add__(self, other): | ||
n | 157 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | n | 157 | x = self.x + other.x |
158 | y = self.y + other.y | ||||
159 | z = self.z + other.z | ||||
160 | return pt3d(x, y,z) | ||||
158 | def __sub__(self, other): | 161 | def __sub__(self, other): | ||
n | 159 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | n | 162 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, |
> | her.z)**2) | > | 2)+math.pow(other.z-self.z, 2)) | ||
160 | def __eq__(self, other): | 163 | def __eq__(self, other): | ||
n | 161 | return self.x == other.x & self.y == other.y & self.z == other.z | n | 164 | return other.x==self.x and other.y==self.y and other.z==self.z |
162 | def __str__(self): | 165 | def __str__(self): | ||
t | 163 | return (f'<{self.x}, {self.y}, {self.z}>') | t | 166 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
167 | if __name__ == '__main__': | ||||
164 | p1 = pt3d(1, 1, 1) | 168 | p1 = pt3d(1, 1, 1) | ||
165 | p2 = pt3d(2, 2, 2) | 169 | p2 = pt3d(2, 2, 2) | ||
166 | print(p1 + p2) | 170 | print(p1+p2) | ||
167 | print(p1 - p2) | 171 | print(p1-p2) | ||
168 | print(p1 == p2) | 172 | print(p1==p2) | ||
169 | print(p1+p1 == p2) | 173 | print(p1+p1==p2) | ||
170 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self,r): |
4 | self.radius = radius | 4 | self.rad=r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return self.rad*self.rad*3.14159265359 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return self.rad*2*3.14159265359 |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
n | 19 | def print_item_cost(self): | n | ||
20 | total = self.item_price * self.item_quantity | ||||
21 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.ite | ||||
> | m_price, total)) | ||||
22 | def print_item_description(self): | 19 | def print_item_description(self): | ||
23 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
24 | class ShoppingCart: | 21 | class ShoppingCart: | ||
25 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
26 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 24 | self.current_date = current_date | ||
n | 28 | self.cart_items = cart_items | n | 25 | self.cart_items = cart_items |
29 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
n | 30 | self.cart_items.append(itemToPurchase) | n | 27 | self.cart_items.append(itemToPurchase) |
31 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
32 | tremove_item = False | 29 | tremove_item = False | ||
33 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
36 | tremove_item = True | 33 | tremove_item = True | ||
37 | break | 34 | break | ||
n | 38 | if not tremove_item: | n | 35 | if not tremove_item: |
39 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
41 | tmodify_item = False | 38 | tmodify_item = False | ||
42 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
44 | tmodify_item = True | 41 | tmodify_item = True | ||
45 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
46 | break | 43 | break | ||
n | 47 | if not tmodify_item: | n | 44 | if not tmodify_item: |
48 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
49 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
50 | num_items = 0 | 47 | num_items = 0 | ||
51 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
52 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
n | 53 | return num_items | n | 50 | return num_items |
54 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
55 | total_cost = 0 | 52 | total_cost = 0 | ||
56 | cost = 0 | 53 | cost = 0 | ||
57 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
58 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
59 | total_cost += cost | 56 | total_cost += cost | ||
60 | return total_cost | 57 | return total_cost | ||
61 | def print_total(self): | 58 | def print_total(self): | ||
n | 62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_ | n | ||
> | date)) | ||||
63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||||
64 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
n | 65 | if (total_cost == 0): | n | 60 | if (total_cost == 0): |
66 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
67 | else: | 62 | else: | ||
n | n | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | ||||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||||
68 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
n | 69 | item.print_item_cost() | n | 66 | total = item.item_price * item.item_quantity |
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||||
> | item.item_price, total)) | ||||
70 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
n | 74 | else: | n | 72 | else: |
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
76 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
n | 78 | item.print_item_description() | n | 76 | item.print_item_description() |
79 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
80 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
81 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
n | 82 | 'a - Add item to cart\n' | n | 80 | 'a - Add item to cart\n' |
83 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
84 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
85 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
86 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
88 | command = '' | 86 | command = '' | ||
89 | while(command != 'q'): | 87 | while(command != 'q'): | ||
90 | print(menu) | 88 | print(menu) | ||
91 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
92 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
93 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
94 | if(command == 'a'): | 92 | if(command == 'a'): | ||
95 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
96 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
97 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
98 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
99 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
101 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
102 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
103 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
n | 104 | customer_Cart.print_total() | n | 102 | customer_Cart.print_total() |
105 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
107 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
108 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
109 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
110 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
111 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
112 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
113 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 114 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
115 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
116 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
n | 117 | customer_Cart.modify_item(itemToPurchase) | n | 115 | customer_Cart.modify_item(itemToPurchase) |
118 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
t | 119 | customer_name = input("Enter customer's name:\n") | t | 117 | customer_name = input("Enter customer's name:\n") |
120 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
121 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
122 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
123 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
124 | print_menu(newCart)import math | 122 | print_menu(newCart) import math | ||
125 | class pt3d: | 123 | class pt3d: | ||
126 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self,x=0,y=0,z=0): | ||
127 | self.x= x | 125 | self.x= x | ||
128 | self.y= y | 126 | self.y= y | ||
129 | self.z= z | 127 | self.z= z | ||
130 | def __add__(self, other): | 128 | def __add__(self, other): | ||
131 | x = self.x + other.x | 129 | x = self.x + other.x | ||
132 | y = self.y + other.y | 130 | y = self.y + other.y | ||
133 | z = self.z + other.z | 131 | z = self.z + other.z | ||
134 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
135 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
136 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
137 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
138 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
139 | def __str__(self): | 137 | def __str__(self): | ||
140 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
141 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
142 | p1 = pt3d(1, 1, 1) | 140 | p1 = pt3d(1, 1, 1) | ||
143 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
144 | print(p1+p2) | 142 | print(p1+p2) | ||
145 | print(p1-p2) | 143 | print(p1-p2) | ||
146 | print(p1==p2) | 144 | print(p1==p2) | ||
147 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
148 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from cmath import pi | n | ||
2 | import math | 1 | import math | ||
3 | class Circle: | 2 | class Circle: | ||
n | 4 | def __init__ (self, r=0): | n | 3 | def __init__(self,r): |
5 | self.r = r | 4 | self.rad=r | ||
6 | def area(self): | 5 | def area(self): | ||
n | 7 | area = (math.pi * (self.r**2)) | n | 6 | return self.rad*self.rad*3.14159265359 |
8 | return area | ||||
9 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 10 | circum = (2*math.pi* self.r) | n | 8 | return self.rad*2*3.14159265359 |
11 | return circum | ||||
12 | if __name__=="__main__": | 9 | if __name__=='__main__': | ||
13 | r= int (input()) | 10 | x = int(input()) | ||
14 | NewCircle = Circle(r) | 11 | NewCircle = Circle(x) | ||
15 | print(' {:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
16 | print(' {:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
17 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
18 | self.item_name=name | 15 | self.item_name=name | ||
19 | self.item_description=description | 16 | self.item_description=description | ||
20 | self.item_price=price | 17 | self.item_price=price | ||
21 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
22 | def print_item_description(self): | 19 | def print_item_description(self): | ||
23 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
24 | class ShoppingCart: | 21 | class ShoppingCart: | ||
25 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
26 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 24 | self.current_date = current_date | ||
28 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
29 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
30 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
31 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
32 | tremove_item = False | 29 | tremove_item = False | ||
33 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
36 | tremove_item = True | 33 | tremove_item = True | ||
37 | break | 34 | break | ||
38 | if not tremove_item: | 35 | if not tremove_item: | ||
39 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
41 | tmodify_item = False | 38 | tmodify_item = False | ||
42 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
44 | tmodify_item = True | 41 | tmodify_item = True | ||
45 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
46 | break | 43 | break | ||
47 | if not tmodify_item: | 44 | if not tmodify_item: | ||
48 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
49 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
50 | num_items = 0 | 47 | num_items = 0 | ||
51 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
52 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
53 | return num_items | 50 | return num_items | ||
54 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
55 | total_cost = 0 | 52 | total_cost = 0 | ||
56 | cost = 0 | 53 | cost = 0 | ||
57 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
58 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
59 | total_cost += cost | 56 | total_cost += cost | ||
60 | return total_cost | 57 | return total_cost | ||
61 | def print_total(self): | 58 | def print_total(self): | ||
62 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
63 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
64 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
65 | else: | 62 | else: | ||
66 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
67 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
68 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
69 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
70 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
71 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
72 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
73 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
74 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
75 | else: | 72 | else: | ||
76 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
77 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
78 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
79 | item.print_item_description() | 76 | item.print_item_description() | ||
80 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
81 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
82 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
83 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
84 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
85 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
86 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
87 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
88 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
89 | command = '' | 86 | command = '' | ||
90 | while(command != 'q'): | 87 | while(command != 'q'): | ||
91 | print(menu) | 88 | print(menu) | ||
92 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
93 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
94 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
95 | if(command == 'a'): | 92 | if(command == 'a'): | ||
96 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
97 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
98 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
99 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
100 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
102 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
103 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
n | 104 | print('\nOUTPUT SHOPPING CART') | n | 101 | print('OUTPUT SHOPPING CART') |
105 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
106 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
107 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
108 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
109 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
110 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
111 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
112 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
113 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
114 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 115 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
116 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
117 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
118 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
119 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
120 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
121 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
122 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
123 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
124 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
125 | print_menu(newCart) import math | 122 | print_menu(newCart) import math | ||
126 | class pt3d: | 123 | class pt3d: | ||
n | 127 | def __init__(self,x,y,z): | n | 124 | def __init__(self,x=0,y=0,z=0): |
128 | self.x= x | 125 | self.x= x | ||
129 | self.y= y | 126 | self.y= y | ||
130 | self.z= z | 127 | self.z= z | ||
131 | def __add__(self, other): | 128 | def __add__(self, other): | ||
132 | x = self.x + other.x | 129 | x = self.x + other.x | ||
133 | y = self.y + other.y | 130 | y = self.y + other.y | ||
134 | z = self.z + other.z | 131 | z = self.z + other.z | ||
135 | return pt3d(x, y,z) | 132 | return pt3d(x, y,z) | ||
136 | def __sub__(self, other): | 133 | def __sub__(self, other): | ||
137 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, 2)+math. | 134 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
138 | def __eq__(self, other): | 135 | def __eq__(self, other): | ||
139 | return other.x==self.x and other.y==self.y and other.z==self.z | 136 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
140 | def __str__(self): | 137 | def __str__(self): | ||
141 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 138 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
142 | if __name__ == '__main__': | 139 | if __name__ == '__main__': | ||
t | 143 | p1 = pt3d(1, 1, 1) | t | 140 | p1 = pt3d(1, 1, 1) |
144 | p2 = pt3d(2, 2, 2) | 141 | p2 = pt3d(2, 2, 2) | ||
145 | print(p1+p2) | 142 | print(p1+p2) | ||
146 | print(p1-p2) | 143 | print(p1-p2) | ||
147 | print(p1==p2) | 144 | print(p1==p2) | ||
148 | print(p1+p1==p2) | 145 | print(p1+p1==p2) | ||
149 | print(p1==p2+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,x): | 3 | def __init__(self,x): | ||
n | 4 | self.x= x | n | 4 | self.x = x |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area=(math.pi*(self.x**2)) | n | 6 | area = math.pi*((self.x)**2) |
7 | return area | 7 | return area | ||
8 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 9 | perimeter =2*math.pi*self.x | n | 9 | perimeter = 2*math.pi*self.x |
10 | return perimeter | 10 | return perimeter | ||
11 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
12 | x = int(input()) | 12 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
n | 14 | print(NewCircle.area()) | n | 14 | print('{:.5f}'.format(NewCircle.area())) |
15 | print(NewCircle.perimeter())class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
17 | self.item_name=name | 17 | self.item_name=name | ||
18 | self.item_description=description | 18 | self.item_description=description | ||
19 | self.item_price=price | 19 | self.item_price=price | ||
20 | self.item_quantity=quantity | 20 | self.item_quantity=quantity | ||
21 | def print_item_description(self): | 21 | def print_item_description(self): | ||
22 | print('%s: %s' % (self.item_name, self.item_description)) | 22 | print('%s: %s' % (self.item_name, self.item_description)) | ||
23 | class ShoppingCart: | 23 | class ShoppingCart: | ||
24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
25 | self.customer_name = customer_name | 25 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 26 | self.current_date = current_date | ||
27 | self.cart_items = cart_items | 27 | self.cart_items = cart_items | ||
28 | def add_item(self, itemToPurchase): | 28 | def add_item(self, itemToPurchase): | ||
29 | self.cart_items.append(itemToPurchase) | 29 | self.cart_items.append(itemToPurchase) | ||
30 | def remove_item(self, itemName): | 30 | def remove_item(self, itemName): | ||
31 | tremove_item = False | 31 | tremove_item = False | ||
32 | for item in self.cart_items: | 32 | for item in self.cart_items: | ||
33 | if item.item_name == itemName: | 33 | if item.item_name == itemName: | ||
34 | self.cart_items.remove(item) | 34 | self.cart_items.remove(item) | ||
35 | tremove_item = True | 35 | tremove_item = True | ||
36 | break | 36 | break | ||
37 | if not tremove_item: | 37 | if not tremove_item: | ||
n | 38 | print('Item not found in the cart. Nothing removed') | n | 38 | print('Item not found in cart. Nothing removed.') |
39 | def modify_item(self, itemToPurchase): | 39 | def modify_item(self, itemToPurchase): | ||
40 | tmodify_item = False | 40 | tmodify_item = False | ||
41 | for i in range(len(self.cart_items)): | 41 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | tmodify_item = True | 43 | tmodify_item = True | ||
44 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 44 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
45 | break | 45 | break | ||
46 | if not tmodify_item: | 46 | if not tmodify_item: | ||
n | 47 | print('Item not found in the cart. Nothing modified') | n | 47 | print('Item not found in cart. Nothing modified.') |
48 | def get_num_items_in_cart(self): | 48 | def get_num_items_in_cart(self): | ||
49 | num_items = 0 | 49 | num_items = 0 | ||
50 | for item in self.cart_items: | 50 | for item in self.cart_items: | ||
51 | num_items = num_items + item.item_quantity | 51 | num_items = num_items + item.item_quantity | ||
52 | return num_items | 52 | return num_items | ||
53 | def get_cost_of_cart(self): | 53 | def get_cost_of_cart(self): | ||
54 | total_cost = 0 | 54 | total_cost = 0 | ||
55 | cost = 0 | 55 | cost = 0 | ||
56 | for item in self.cart_items: | 56 | for item in self.cart_items: | ||
57 | cost = (item.item_quantity * item.item_price) | 57 | cost = (item.item_quantity * item.item_price) | ||
58 | total_cost += cost | 58 | total_cost += cost | ||
59 | return total_cost | 59 | return total_cost | ||
60 | def print_total(self): | 60 | def print_total(self): | ||
61 | total_cost = self.get_cost_of_cart() | 61 | total_cost = self.get_cost_of_cart() | ||
62 | if (total_cost == 0): | 62 | if (total_cost == 0): | ||
63 | print('SHOPPING CART IS EMPTY') | 63 | print('SHOPPING CART IS EMPTY') | ||
64 | else: | 64 | else: | ||
65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
67 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
68 | total = item.item_price * item.item_quantity | 68 | total = item.item_price * item.item_quantity | ||
69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
70 | print('\nTotal: $%d' %(total_cost)) | 70 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 71 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 72 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 73 | print('SHOPPING CART IS EMPTY') | ||
74 | else: | 74 | else: | ||
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
76 | print('\nItem Descriptions') | 76 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
78 | item.print_item_description() | 78 | item.print_item_description() | ||
79 | def print_menu(newCart): | 79 | def print_menu(newCart): | ||
80 | customer_Cart = newCart | 80 | customer_Cart = newCart | ||
81 | menu = ('\nMENU\n' | 81 | menu = ('\nMENU\n' | ||
82 | 'a - Add item to cart\n' | 82 | 'a - Add item to cart\n' | ||
83 | 'r - Remove item from cart\n' | 83 | 'r - Remove item from cart\n' | ||
84 | 'c - Change item quantity\n' | 84 | 'c - Change item quantity\n' | ||
85 | "i - Output items' descriptions\n" | 85 | "i - Output items' descriptions\n" | ||
86 | 'o - Output shopping cart\n' | 86 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 87 | 'q - Quit\n') | ||
88 | command = '' | 88 | command = '' | ||
89 | while(command != 'q'): | 89 | while(command != 'q'): | ||
90 | print(menu) | 90 | print(menu) | ||
91 | command = input('Choose an option:') | 91 | command = input('Choose an option:') | ||
92 | while(command != 'a' and command != 'o' and command != 'i' and command != | 92 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
n | 93 | command = input('Choose an option:\n') | n | 93 | command = input('\nChoose an option:') |
94 | if(command == 'a'): | 94 | if(command == 'a'): | ||
95 | print("\nADD ITEM TO CART") | 95 | print("\nADD ITEM TO CART") | ||
n | 96 | item_name = input('Enter the item name:\n') | n | 96 | item_name = input('Enter item name:\n') |
97 | item_description = input('Enter the item description:\n') | 97 | item_description = input('Enter item description:\n') | ||
98 | item_price = int(input('Enter the item price:\n')) | 98 | item_price = int(input('Enter the item price:\n')) | ||
99 | item_quantity = int(input('Enter the item quantity:\n')) | 99 | item_quantity = int(input('Enter the item quantity:\n')) | ||
100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
101 | customer_Cart.add_item(itemtoPurchase) | 101 | customer_Cart.add_item(itemtoPurchase) | ||
102 | elif(command == 'o'): | 102 | elif(command == 'o'): | ||
103 | print('\nOUTPUT SHOPPING CART') | 103 | print('\nOUTPUT SHOPPING CART') | ||
104 | customer_Cart.print_total() | 104 | customer_Cart.print_total() | ||
105 | elif(command == 'i'): | 105 | elif(command == 'i'): | ||
106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
107 | customer_Cart.print_descriptions() | 107 | customer_Cart.print_descriptions() | ||
108 | elif(command == 'r'): | 108 | elif(command == 'r'): | ||
109 | print('REMOVE ITEM FROM CART') | 109 | print('REMOVE ITEM FROM CART') | ||
n | 110 | itemName = input('Enter the name of the item to remove :\n') | n | 110 | itemName = input('Enter name of item to remove:\n') |
111 | customer_Cart.remove_item(itemName) | 111 | customer_Cart.remove_item(itemName) | ||
112 | elif(command == 'c'): | 112 | elif(command == 'c'): | ||
113 | print('\nCHANGE ITEM QUANTITY') | 113 | print('\nCHANGE ITEM QUANTITY') | ||
n | 114 | itemName = input('Enter the name of the item :\n') | n | 114 | itemName = input('Enter name of item:\n') |
115 | qty = int(input('Enter the new quantity :\n')) | 115 | quantity = int(input('Enter the new quantity:\n')) | ||
116 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 116 | itemToPurchase = ItemToPurchase(itemName,0,quantity) | ||
117 | customer_Cart.modify_item(itemToPurchase) | 117 | customer_Cart.modify_item(itemToPurchase) | ||
118 | if __name__ == "__main__": | 118 | if __name__ == "__main__": | ||
119 | customer_name = input("Enter customer's name:\n") | 119 | customer_name = input("Enter customer's name:\n") | ||
120 | current_date = input("Enter today's date:\n") | 120 | current_date = input("Enter today's date:\n") | ||
121 | print("\nCustomer name: %s" %customer_name) | 121 | print("\nCustomer name: %s" %customer_name) | ||
122 | print("Today's date: %s" %current_date) | 122 | print("Today's date: %s" %current_date) | ||
123 | newCart = ShoppingCart(customer_name, current_date) | 123 | newCart = ShoppingCart(customer_name, current_date) | ||
124 | print_menu(newCart) | 124 | print_menu(newCart) | ||
n | 125 | print() | n | 125 | print()import math |
126 | import math | ||||
127 | class pt3d: | 126 | class pt3d: | ||
t | 128 | def __init__(self,x=0,y=0,z=0): | t | 127 | def __init__(self,x=0,y=0,z=0): |
129 | self.x=x | 128 | self.x=x | ||
130 | self.y=y | 129 | self.y=y | ||
131 | self.z=z | 130 | self.z=z | ||
132 | def __add__(self,A): | 131 | def __add__(self,point): | ||
133 | x1 = self.x+A.x | 132 | x1 = self.x+point.x | ||
134 | y1 = self.y+A.y | 133 | y1 = self.y+point.y | ||
135 | z1 = self.z+A.z | 134 | z1 = self.z+point.z | ||
136 | return pt3d(x1, y1, z1) | 135 | return pt3d(x1, y1, z1) | ||
137 | def __sub__(self,A): | 136 | def __sub__(self,point): | ||
138 | x1 = (self.x-A.x)**2 | 137 | x1 = (self.x-point.x)**2 | ||
139 | y1 = (self.y-A.y)**2 | 138 | y1 = (self.y-point.y)**2 | ||
140 | z1 = (self.z-A.z)**2 | 139 | z1 = (self.z-point.z)**2 | ||
141 | return math.sqrt(x1+y1+z1) | 140 | return math.sqrt(x1+y1+z1) | ||
142 | def __str__(self): | 141 | def __str__(self): | ||
143 | return f"<{self.x},{self.y},{self.z}>" | 142 | return f"<{self.x},{self.y},{self.z}>" | ||
144 | def __eq__(self,A): | 143 | def __eq__(self,point): | ||
145 | if (self.x == A.x) and (self.y == A.y) and (self.x == A.x): | 144 | if (self.x == point.x) and (self.y == point.y) and (self.x == point.x): | ||
146 | return True | 145 | return True | ||
147 | else: | 146 | else: | ||
148 | return False | 147 | return False | ||
149 | a = pt3d(1,1,1) | 148 | a = pt3d(1,1,1) | ||
150 | b = pt3d(2,2,2) | 149 | b = pt3d(2,2,2) | ||
151 | print(a-b) | 150 | print(a-b) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | pi=3.14159265359 | n | ||
3 | class Circle(): | 2 | class Circle(): | ||
n | 4 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
5 | self.radius=radius | 4 | self.radius = radius | ||
6 | def area(self): | 5 | def area(self): | ||
n | 7 | return pi*self.radius*self.radius | n | 6 | return math.pi * self.radius**2 |
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | return pi*2*self.radius | n | 8 | return 2*math.pi*self.radius |
10 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
11 | x = int(input()) | 10 | x = int(input()) | ||
12 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
13 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 14 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
16 | self.item_name=name | 16 | self.item_name=name | ||
17 | self.item_description=description | 17 | self.item_description=description | ||
18 | self.item_price=price | 18 | self.item_price=price | ||
19 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
20 | def print_item_description(self): | 20 | def print_item_description(self): | ||
21 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
22 | class ShoppingCart: | 22 | class ShoppingCart: | ||
23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
24 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
25 | self.current_date = current_date | 25 | self.current_date = current_date | ||
26 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
27 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
28 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
29 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
30 | tremove_item = False | 30 | tremove_item = False | ||
31 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
32 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
33 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
34 | tremove_item = True | 34 | tremove_item = True | ||
35 | break | 35 | break | ||
36 | if not tremove_item: | 36 | if not tremove_item: | ||
37 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
38 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
39 | tmodify_item = False | 39 | tmodify_item = False | ||
40 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
42 | tmodify_item = True | 42 | tmodify_item = True | ||
43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
44 | break | 44 | break | ||
45 | if not tmodify_item: | 45 | if not tmodify_item: | ||
46 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
47 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
48 | num_items = 0 | 48 | num_items = 0 | ||
49 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
50 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
51 | return num_items | 51 | return num_items | ||
52 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
53 | total_cost = 0 | 53 | total_cost = 0 | ||
54 | cost = 0 | 54 | cost = 0 | ||
55 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
56 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
57 | total_cost += cost | 57 | total_cost += cost | ||
58 | return total_cost | 58 | return total_cost | ||
59 | def print_total(self): | 59 | def print_total(self): | ||
60 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
61 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
62 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
63 | else: | 63 | else: | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 73 | else: | ||
74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
75 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
77 | item.print_item_description() | 77 | item.print_item_description() | ||
78 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
79 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
80 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from the cart\n' | 82 | 'r - Remove item from the cart\n' | ||
83 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
84 | "i - Output item's descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
87 | command = '' | 87 | command = '' | ||
88 | while(command != 'q'): | 88 | while(command != 'q'): | ||
89 | print(menu) | 89 | print(menu) | ||
90 | command = input('Choose an option:') | 90 | command = input('Choose an option:') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 93 | if(command == 'a'): | ||
94 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
95 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
96 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
97 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
98 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
100 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
101 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
102 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
103 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
104 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
106 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
107 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
108 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
109 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
110 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
111 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
112 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
113 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
114 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
116 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
117 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
118 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
119 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
120 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
121 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
122 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 123 | print_menu(newCart) | n | 123 | print_menu(newCart)from math import sqrt |
124 | from math import sqrt | ||||
125 | class pt3d: | 124 | class pt3d: | ||
n | 126 | def __init__(self, a, b, c): | n | 125 | def __init__(self, x, y, z): |
127 | self.a = a | 126 | self.x = x | ||
128 | self.b = b | 127 | self.y = y | ||
129 | self.c = c | 128 | self.z = z | ||
130 | def __add__(self, other): | 129 | def __add__(self, other): | ||
n | 131 | return pt3d(self.a + other.a, self.b + other.b, self.c + other.c) | n | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) |
132 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
n | 133 | return sqrt((self.a - other.a)**2 + (self.b - other.b)**2 + (self.c - ot | n | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot |
> | her.c)**2) | > | her.z)**2) | ||
134 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
n | 135 | return self.a == other.a & self.b == other.b & self.c == other.c | n | 134 | return self.x == other.x & self.y == other.y & self.z == other.z |
136 | def __str__(self): | 135 | def __str__(self): | ||
t | 137 | return '<{}, {}, {}>'.format(self.a, self.b, self.c) | t | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
138 | thing1 = pt3d(1, 1, 1) | 137 | p1 = pt3d(1, 1, 1) | ||
139 | thingy2 = pt3d(2, 2, 2) | 138 | p2 = pt3d(2, 2, 2) | ||
140 | print(thing1 + thingy2) | 139 | print(p1 + p2) | ||
141 | print(thing1 - thingy2) | 140 | print(p1 - p2) | ||
142 | print(thing1 == thingy2) | 141 | print(p1 == p2) | ||
143 | print(thing1*2 == thingy2) | 142 | print(p1+p1 == p2) | ||
144 | print(thing1==thingy2+pt3d(-1, -1, -1)) | 143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
4 | self.radius=radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
6 | return math.pi*(self.radius**2) | 6 | return math.pi*(self.radius**2) | ||
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*math.pi | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
n | 19 | def print_item_cost(self): | n | ||
20 | total = self.item_price * self.item_quantity | ||||
21 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.ite | ||||
> | m_price, total)) | ||||
22 | def print_item_description(self): | 19 | def print_item_description(self): | ||
23 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
24 | class ShoppingCart: | 21 | class ShoppingCart: | ||
25 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
26 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 24 | self.current_date = current_date | ||
n | 28 | self.cart_items = cart_items | n | 25 | self.cart_items = cart_items |
29 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
n | 30 | self.cart_items.append(itemToPurchase) | n | 27 | self.cart_items.append(itemToPurchase) |
31 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
32 | tremove_item = False | 29 | tremove_item = False | ||
33 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
36 | tremove_item = True | 33 | tremove_item = True | ||
37 | break | 34 | break | ||
n | 38 | if not tremove_item: | n | 35 | if not tremove_item: |
39 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in the cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
41 | tmodify_item = False | 38 | tmodify_item = False | ||
42 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
44 | tmodify_item = True | 41 | tmodify_item = True | ||
45 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
46 | break | 43 | break | ||
n | 47 | if not tmodify_item: | n | 44 | if not tmodify_item: |
48 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in the cart. Nothing modified.') | ||
49 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
50 | num_items = 0 | 47 | num_items = 0 | ||
51 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
52 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
n | 53 | return num_items | n | 50 | return num_items |
54 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
55 | total_cost = 0 | 52 | total_cost = 0 | ||
56 | cost = 0 | 53 | cost = 0 | ||
57 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
58 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
59 | total_cost += cost | 56 | total_cost += cost | ||
60 | return total_cost | 57 | return total_cost | ||
61 | def print_total(self): | 58 | def print_total(self): | ||
n | 62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_ | n | ||
> | date)) | ||||
63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||||
64 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
n | 65 | if (total_cost == 0): | n | 60 | if (total_cost == 0): |
66 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
67 | else: | 62 | else: | ||
n | n | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | ||||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||||
68 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
n | 69 | item.print_item_cost() | n | 66 | total = item.item_price * item.item_quantity |
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||||
> | item.item_price, total)) | ||||
70 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
n | 74 | else: | n | 72 | else: |
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
76 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
n | 78 | item.print_item_description() | n | 76 | item.print_item_description() |
79 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
n | 80 | customer_Cart = newCart | n | 78 | customer_Cart = newCart |
81 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
82 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
83 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
84 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
85 | 'i - Output items\' descriptions\n' | 83 | "i - Output items' descriptions\n" | ||
86 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
88 | command = '' | 86 | command = '' | ||
89 | while(command != 'q'): | 87 | while(command != 'q'): | ||
90 | print(menu) | 88 | print(menu) | ||
91 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
92 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
93 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
94 | if(command == 'a'): | 92 | if(command == 'a'): | ||
95 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
96 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
97 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
n | 98 | item_price = float(input('Enter the item price:\n')) | n | 96 | item_price = int(input('Enter the item price:\n')) |
99 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
101 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
102 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
n | 103 | print('OUTPUT SHOPPING CART') | n | 101 | print('\nOUTPUT SHOPPING CART') |
104 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
105 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
107 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
108 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
109 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 110 | itemName = input('Enter name of item to remove:\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
111 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
112 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
113 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 114 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
115 | quantity = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
116 | itemToPurchase = ItemToPurchase(itemName,0,quantity) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
117 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
118 | def execute_menu(command, my_cart): | ||||
119 | customer_Cart = my_cart | ||||
120 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
121 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
122 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
123 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
124 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
125 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
126 | print_menu(newCart)from math import sqrt | 122 | print_menu(newCart)from math import sqrt | ||
127 | class pt3d: | 123 | class pt3d: | ||
n | 128 | def __init__(self, x=0, y=0, z=0): | n | 124 | def __init__(self, x, y, z): |
129 | self.x = x | 125 | self.x = x | ||
130 | self.y = y | 126 | self.y = y | ||
131 | self.z = z | 127 | self.z = z | ||
132 | def __add__(self, other): | 128 | def __add__(self, other): | ||
133 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
134 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
135 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
136 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
137 | return self.x == other.x and self.y == other.y and self.z == other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
138 | def __str__(self): | 134 | def __str__(self): | ||
t | 139 | return '<{0},{1},{2}>'.format(self.x, self.y, self.z) | t | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
140 | p1 = pt3d(1,1,1) | 136 | p1 = pt3d(1, 1, 1) | ||
141 | p2 = pt3d(2,2,2) | 137 | p2 = pt3d(2, 2, 2) | ||
142 | print(p1 + p2) | 138 | print(p1 + p2) | ||
143 | print(p1 - p2) | 139 | print(p1 - p2) | ||
144 | print(p1 == p2) | 140 | print(p1 == p2) | ||
145 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
146 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
n | 4 | self.radius = radius | n | 4 | self.radius = radius |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius**2 | n | 6 | return (math.pi)*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return self.radius * math.pi *2 | n | 8 | return (2*self.radius)*math.pi |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | ||
> | em_description = "none"): | > | em_description = "none"): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 20 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price,(self.item_quantity*self.item_price))) | > | item_price,(self.item_quantity*self.item_price))) | ||
21 | def print_item_description(self): | 21 | def print_item_description(self): | ||
22 | print('{}: {}'.format(self.item_name, self.item_description)) | 22 | print('{}: {}'.format(self.item_name, self.item_description)) | ||
23 | class ShoppingCart: | 23 | class ShoppingCart: | ||
24 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | 24 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | ||
25 | self.customer_name = customer_name | 25 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 26 | self.current_date = current_date | ||
27 | self.cart_items = [] | 27 | self.cart_items = [] | ||
28 | def add_item(self, ItemToPurchase): | 28 | def add_item(self, ItemToPurchase): | ||
29 | self.cart_items.append(ItemToPurchase) | 29 | self.cart_items.append(ItemToPurchase) | ||
30 | def remove_item(self, itemname): | 30 | def remove_item(self, itemname): | ||
31 | Removeitem = False | 31 | Removeitem = False | ||
32 | for i in self.cart_items: | 32 | for i in self.cart_items: | ||
33 | if i.item_name == itemname: | 33 | if i.item_name == itemname: | ||
34 | self.cart_items.remove(i) | 34 | self.cart_items.remove(i) | ||
35 | Removeitem = True | 35 | Removeitem = True | ||
36 | break | 36 | break | ||
37 | if not Removeitem: | 37 | if not Removeitem: | ||
38 | print('Item not found in cart. Nothing removed.') | 38 | print('Item not found in cart. Nothing removed.') | ||
39 | def modify_item(self, itemToPurchase): | 39 | def modify_item(self, itemToPurchase): | ||
40 | Modifyitem = False | 40 | Modifyitem = False | ||
41 | for i in range(len(self.cart_items)): | 41 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | Modifyitem = True | 43 | Modifyitem = True | ||
44 | if (itemToPurchase.item_price == 0 and itemToPurchase.item_quant | 44 | if (itemToPurchase.item_price == 0 and itemToPurchase.item_quant | ||
> | ity == 0 and itemToPurchase.item_description == 'none'): | > | ity == 0 and itemToPurchase.item_description == 'none'): | ||
45 | break | 45 | break | ||
46 | else: | 46 | else: | ||
47 | if (itemToPurchase.item_price != 0): | 47 | if (itemToPurchase.item_price != 0): | ||
48 | self.cart_items[i].item_price = itemToPurchase.item_pric | 48 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||
> | e | > | e | ||
49 | if (itemToPurchase.item_quantity != 0): | 49 | if (itemToPurchase.item_quantity != 0): | ||
50 | self.cart_items[i].item_quantity = itemToPurchase.item_q | 50 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | > | uantity | ||
51 | if (itemToPurchase.item_description != 'none'): | 51 | if (itemToPurchase.item_description != 'none'): | ||
52 | self.cart_items[i].item_description = itemToPurchase.ite | 52 | self.cart_items[i].item_description = itemToPurchase.ite | ||
> | m_description | > | m_description | ||
53 | break | 53 | break | ||
54 | if not Modifyitem: | 54 | if not Modifyitem: | ||
55 | print('Item not found in cart. Nothing modified.') | 55 | print('Item not found in cart. Nothing modified.') | ||
56 | def get_num_items_in_cart(self): | 56 | def get_num_items_in_cart(self): | ||
57 | num_items = 0 | 57 | num_items = 0 | ||
58 | for i in self.cart_items: | 58 | for i in self.cart_items: | ||
59 | num_items = num_items + i.item_quantity | 59 | num_items = num_items + i.item_quantity | ||
60 | return num_items | 60 | return num_items | ||
61 | def get_cost_of_cart(self): | 61 | def get_cost_of_cart(self): | ||
62 | total_cost = 0 | 62 | total_cost = 0 | ||
63 | cost = 0 | 63 | cost = 0 | ||
64 | for i in self.cart_items: | 64 | for i in self.cart_items: | ||
65 | cost = (i.item_quantity * i.item_price) | 65 | cost = (i.item_quantity * i.item_price) | ||
66 | total_cost += cost | 66 | total_cost += cost | ||
67 | return total_cost | 67 | return total_cost | ||
68 | def print_total(self): | 68 | def print_total(self): | ||
69 | total_cost = self.get_cost_of_cart() | 69 | total_cost = self.get_cost_of_cart() | ||
70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
71 | print('Number of Items: {}\n'.format(self.get_num_items_in_cart())) | 71 | print('Number of Items: {}\n'.format(self.get_num_items_in_cart())) | ||
72 | for i in self.cart_items: | 72 | for i in self.cart_items: | ||
73 | i.print_item_cost() | 73 | i.print_item_cost() | ||
74 | if (total_cost == 0): | 74 | if (total_cost == 0): | ||
75 | print('SHOPPING CART IS EMPTY') | 75 | print('SHOPPING CART IS EMPTY') | ||
76 | print('\nTotal: ${}'.format((total_cost))) | 76 | print('\nTotal: ${}'.format((total_cost))) | ||
77 | def print_descriptions(self): | 77 | def print_descriptions(self): | ||
78 | if len(self.cart_items) == 0: | 78 | if len(self.cart_items) == 0: | ||
79 | print('SHOPPING CART IS EMPTY') | 79 | print('SHOPPING CART IS EMPTY') | ||
80 | else: | 80 | else: | ||
81 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 81 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
82 | print('\nItem Descriptions') | 82 | print('\nItem Descriptions') | ||
83 | for i in self.cart_items: | 83 | for i in self.cart_items: | ||
84 | i.print_item_description() | 84 | i.print_item_description() | ||
85 | def print_menu(): | 85 | def print_menu(): | ||
86 | print('MENU\n' | 86 | print('MENU\n' | ||
87 | 'a - Add item to cart\n' | 87 | 'a - Add item to cart\n' | ||
88 | 'r - Remove item from cart\n' | 88 | 'r - Remove item from cart\n' | ||
89 | 'c - Change item quantity\n' | 89 | 'c - Change item quantity\n' | ||
90 | "i - Output items' descriptions\n" | 90 | "i - Output items' descriptions\n" | ||
91 | 'o - Output shopping cart\n' | 91 | 'o - Output shopping cart\n' | ||
92 | 'q - Quit\n') | 92 | 'q - Quit\n') | ||
93 | def execute_menu(choice, my_cart): | 93 | def execute_menu(choice, my_cart): | ||
94 | customer_Cart = my_cart | 94 | customer_Cart = my_cart | ||
95 | if choice == 'a': | 95 | if choice == 'a': | ||
96 | print("\nADD ITEM TO CART") | 96 | print("\nADD ITEM TO CART") | ||
97 | item_name = input('Enter the item name:\n') | 97 | item_name = input('Enter the item name:\n') | ||
98 | item_description = input('Enter the item description:\n') | 98 | item_description = input('Enter the item description:\n') | ||
99 | item_price = int(input('Enter the item price:\n')) | 99 | item_price = int(input('Enter the item price:\n')) | ||
100 | item_quantity = int(input('Enter the item quantity:\n')) | 100 | item_quantity = int(input('Enter the item quantity:\n')) | ||
101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
102 | customer_Cart.add_item(itemtoPurchase) | 102 | customer_Cart.add_item(itemtoPurchase) | ||
103 | elif choice == 'o': | 103 | elif choice == 'o': | ||
104 | print('OUTPUT SHOPPING CART') | 104 | print('OUTPUT SHOPPING CART') | ||
105 | customer_Cart.print_total() | 105 | customer_Cart.print_total() | ||
106 | elif choice == 'i': | 106 | elif choice == 'i': | ||
107 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 107 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
108 | customer_Cart.print_descriptions() | 108 | customer_Cart.print_descriptions() | ||
109 | elif choice == 'r': | 109 | elif choice == 'r': | ||
110 | print('REMOVE ITEM FROM CART') | 110 | print('REMOVE ITEM FROM CART') | ||
111 | itemName = input('Enter name of item to remove:\n') | 111 | itemName = input('Enter name of item to remove:\n') | ||
112 | customer_Cart.remove_item(itemName) | 112 | customer_Cart.remove_item(itemName) | ||
113 | elif choice == 'c': | 113 | elif choice == 'c': | ||
114 | print('\nCHANGE ITEM QUANTITY') | 114 | print('\nCHANGE ITEM QUANTITY') | ||
115 | itemName = input('Enter the item name:\n') | 115 | itemName = input('Enter the item name:\n') | ||
116 | qty = int(input('Enter the new quantity:\n')) | 116 | qty = int(input('Enter the new quantity:\n')) | ||
117 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 117 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
118 | customer_Cart.modify_item(itemToPurchase) | 118 | customer_Cart.modify_item(itemToPurchase) | ||
119 | if __name__ == "__main__": | 119 | if __name__ == "__main__": | ||
120 | customer_name = input("Enter customer's name:\n") | 120 | customer_name = input("Enter customer's name:\n") | ||
121 | current_date = input("Enter today's date:\n") | 121 | current_date = input("Enter today's date:\n") | ||
122 | print("\nCustomer name: {}".format(customer_name)) | 122 | print("\nCustomer name: {}".format(customer_name)) | ||
123 | print("Today's date: {}".format(current_date)) | 123 | print("Today's date: {}".format(current_date)) | ||
124 | newCart = ShoppingCart(customer_name, current_date) | 124 | newCart = ShoppingCart(customer_name, current_date) | ||
125 | choice = '' | 125 | choice = '' | ||
126 | while choice != 'q': | 126 | while choice != 'q': | ||
127 | print() | 127 | print() | ||
128 | print_menu() | 128 | print_menu() | ||
129 | choice = input('Choose an option:\n') | 129 | choice = input('Choose an option:\n') | ||
130 | while choice != 'a' and choice != 'o' and choice != 'i' and choice != 'q | 130 | while choice != 'a' and choice != 'o' and choice != 'i' and choice != 'q | ||
> | ' and choice != 'r' and choice != 'c': | > | ' and choice != 'r' and choice != 'c': | ||
131 | choice = input('Choose an option:\n') | 131 | choice = input('Choose an option:\n') | ||
132 | execute_menu(choice, newCart)import math | 132 | execute_menu(choice, newCart)import math | ||
133 | class pt3d: | 133 | class pt3d: | ||
n | 134 | def __init__(self, x = 0, y = 0, z = 0): | n | 134 | def __init__(self,x=0,y=0,z=0): |
135 | self.x = x | 135 | self.x= x | ||
136 | self.y = y | 136 | self.y= y | ||
137 | self.z = z | 137 | self.z= z | ||
138 | def __add__(self,variable): | 138 | def __add__(self, new): | ||
139 | return pt3d((self.x + variable.x),(self.y + variable.y),(self.z + variab | 139 | x = self.x + new.x | ||
> | le.z)) | ||||
140 | y = self.y + new.y | ||||
141 | z = self.z + new.z | ||||
142 | return pt3d(x, y,z) | ||||
143 | def __sub__(self, new): | ||||
144 | return math.sqrt(math.pow(new.x-self.x, 2) + math.pow(new.y-self.y, 2)+m | ||||
> | ath.pow(new.z-self.z, 2)) | ||||
145 | def __eq__(self, new): | ||||
146 | return new.x==self.x and new.y==self.y and new.z==self.z | ||||
140 | def __str__(self): | 147 | def __str__(self): | ||
n | 141 | return "<{},{},{}>".format(self.x,self.y,self.z) | n | 148 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
142 | def __sub__(self, variable): | ||||
143 | return math.sqrt((self.x - variable.x)**2 + (self.y - variable.y)**2 + ( | ||||
> | self.z - variable.z)**2) | ||||
144 | def __eq__(self,variable): | ||||
145 | return ((self.x == variable.x) and (self.y == variable.y) and (self.z == | ||||
> | variable.z)) | ||||
146 | if __name__ == '__main__': | 149 | if __name__ == '__main__': | ||
t | 147 | p1 = pt3d(1,1,1) | t | 150 | p1 = pt3d(1, 1, 1) |
148 | p2 = pt3d(2,2,2) | 151 | p2 = pt3d(2, 2, 2) | ||
149 | print(p1 + p2) | 152 | print(p1+p2) | ||
150 | print(p1 - p2) | 153 | print(p1-p2) | ||
151 | print(p1 == p2) | 154 | print(p1==p2) | ||
152 | print(p1 + p1 == p2) | 155 | print(p1+p1==p2) | ||
153 | print(p1 == p2 + pt3d(-1,-1,-1)) | 156 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
n | 4 | self.radius = radius | n | 4 | self.radius = radius |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (math.pi)*(self.radius**2) | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return (2*self.radius)*math.pi | n | 8 | return self.radius * math.pi *2 |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it | ||
> | em_description = "none"): | > | em_description = "none"): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 20 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price,(self.item_quantity*self.item_price))) | > | item_price,(self.item_quantity*self.item_price))) | ||
21 | def print_item_description(self): | 21 | def print_item_description(self): | ||
22 | print('{}: {}'.format(self.item_name, self.item_description)) | 22 | print('{}: {}'.format(self.item_name, self.item_description)) | ||
23 | class ShoppingCart: | 23 | class ShoppingCart: | ||
24 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | 24 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | ||
25 | self.customer_name = customer_name | 25 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 26 | self.current_date = current_date | ||
27 | self.cart_items = [] | 27 | self.cart_items = [] | ||
28 | def add_item(self, ItemToPurchase): | 28 | def add_item(self, ItemToPurchase): | ||
29 | self.cart_items.append(ItemToPurchase) | 29 | self.cart_items.append(ItemToPurchase) | ||
30 | def remove_item(self, itemname): | 30 | def remove_item(self, itemname): | ||
31 | Removeitem = False | 31 | Removeitem = False | ||
32 | for i in self.cart_items: | 32 | for i in self.cart_items: | ||
33 | if i.item_name == itemname: | 33 | if i.item_name == itemname: | ||
34 | self.cart_items.remove(i) | 34 | self.cart_items.remove(i) | ||
35 | Removeitem = True | 35 | Removeitem = True | ||
36 | break | 36 | break | ||
37 | if not Removeitem: | 37 | if not Removeitem: | ||
38 | print('Item not found in cart. Nothing removed.') | 38 | print('Item not found in cart. Nothing removed.') | ||
39 | def modify_item(self, itemToPurchase): | 39 | def modify_item(self, itemToPurchase): | ||
40 | Modifyitem = False | 40 | Modifyitem = False | ||
41 | for i in range(len(self.cart_items)): | 41 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | Modifyitem = True | 43 | Modifyitem = True | ||
44 | if (itemToPurchase.item_price == 0 and itemToPurchase.item_quant | 44 | if (itemToPurchase.item_price == 0 and itemToPurchase.item_quant | ||
> | ity == 0 and itemToPurchase.item_description == 'none'): | > | ity == 0 and itemToPurchase.item_description == 'none'): | ||
45 | break | 45 | break | ||
46 | else: | 46 | else: | ||
47 | if (itemToPurchase.item_price != 0): | 47 | if (itemToPurchase.item_price != 0): | ||
48 | self.cart_items[i].item_price = itemToPurchase.item_pric | 48 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||
> | e | > | e | ||
49 | if (itemToPurchase.item_quantity != 0): | 49 | if (itemToPurchase.item_quantity != 0): | ||
50 | self.cart_items[i].item_quantity = itemToPurchase.item_q | 50 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | > | uantity | ||
51 | if (itemToPurchase.item_description != 'none'): | 51 | if (itemToPurchase.item_description != 'none'): | ||
52 | self.cart_items[i].item_description = itemToPurchase.ite | 52 | self.cart_items[i].item_description = itemToPurchase.ite | ||
> | m_description | > | m_description | ||
53 | break | 53 | break | ||
54 | if not Modifyitem: | 54 | if not Modifyitem: | ||
55 | print('Item not found in cart. Nothing modified.') | 55 | print('Item not found in cart. Nothing modified.') | ||
56 | def get_num_items_in_cart(self): | 56 | def get_num_items_in_cart(self): | ||
57 | num_items = 0 | 57 | num_items = 0 | ||
58 | for i in self.cart_items: | 58 | for i in self.cart_items: | ||
59 | num_items = num_items + i.item_quantity | 59 | num_items = num_items + i.item_quantity | ||
60 | return num_items | 60 | return num_items | ||
61 | def get_cost_of_cart(self): | 61 | def get_cost_of_cart(self): | ||
62 | total_cost = 0 | 62 | total_cost = 0 | ||
63 | cost = 0 | 63 | cost = 0 | ||
64 | for i in self.cart_items: | 64 | for i in self.cart_items: | ||
65 | cost = (i.item_quantity * i.item_price) | 65 | cost = (i.item_quantity * i.item_price) | ||
66 | total_cost += cost | 66 | total_cost += cost | ||
67 | return total_cost | 67 | return total_cost | ||
68 | def print_total(self): | 68 | def print_total(self): | ||
69 | total_cost = self.get_cost_of_cart() | 69 | total_cost = self.get_cost_of_cart() | ||
70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 70 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
71 | print('Number of Items: {}\n'.format(self.get_num_items_in_cart())) | 71 | print('Number of Items: {}\n'.format(self.get_num_items_in_cart())) | ||
72 | for i in self.cart_items: | 72 | for i in self.cart_items: | ||
73 | i.print_item_cost() | 73 | i.print_item_cost() | ||
74 | if (total_cost == 0): | 74 | if (total_cost == 0): | ||
75 | print('SHOPPING CART IS EMPTY') | 75 | print('SHOPPING CART IS EMPTY') | ||
76 | print('\nTotal: ${}'.format((total_cost))) | 76 | print('\nTotal: ${}'.format((total_cost))) | ||
77 | def print_descriptions(self): | 77 | def print_descriptions(self): | ||
78 | if len(self.cart_items) == 0: | 78 | if len(self.cart_items) == 0: | ||
79 | print('SHOPPING CART IS EMPTY') | 79 | print('SHOPPING CART IS EMPTY') | ||
80 | else: | 80 | else: | ||
81 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 81 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
82 | print('\nItem Descriptions') | 82 | print('\nItem Descriptions') | ||
83 | for i in self.cart_items: | 83 | for i in self.cart_items: | ||
84 | i.print_item_description() | 84 | i.print_item_description() | ||
85 | def print_menu(): | 85 | def print_menu(): | ||
86 | print('MENU\n' | 86 | print('MENU\n' | ||
87 | 'a - Add item to cart\n' | 87 | 'a - Add item to cart\n' | ||
88 | 'r - Remove item from cart\n' | 88 | 'r - Remove item from cart\n' | ||
89 | 'c - Change item quantity\n' | 89 | 'c - Change item quantity\n' | ||
90 | "i - Output items' descriptions\n" | 90 | "i - Output items' descriptions\n" | ||
91 | 'o - Output shopping cart\n' | 91 | 'o - Output shopping cart\n' | ||
92 | 'q - Quit\n') | 92 | 'q - Quit\n') | ||
93 | def execute_menu(choice, my_cart): | 93 | def execute_menu(choice, my_cart): | ||
94 | customer_Cart = my_cart | 94 | customer_Cart = my_cart | ||
95 | if choice == 'a': | 95 | if choice == 'a': | ||
96 | print("\nADD ITEM TO CART") | 96 | print("\nADD ITEM TO CART") | ||
97 | item_name = input('Enter the item name:\n') | 97 | item_name = input('Enter the item name:\n') | ||
98 | item_description = input('Enter the item description:\n') | 98 | item_description = input('Enter the item description:\n') | ||
99 | item_price = int(input('Enter the item price:\n')) | 99 | item_price = int(input('Enter the item price:\n')) | ||
100 | item_quantity = int(input('Enter the item quantity:\n')) | 100 | item_quantity = int(input('Enter the item quantity:\n')) | ||
101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 101 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
102 | customer_Cart.add_item(itemtoPurchase) | 102 | customer_Cart.add_item(itemtoPurchase) | ||
103 | elif choice == 'o': | 103 | elif choice == 'o': | ||
104 | print('OUTPUT SHOPPING CART') | 104 | print('OUTPUT SHOPPING CART') | ||
105 | customer_Cart.print_total() | 105 | customer_Cart.print_total() | ||
106 | elif choice == 'i': | 106 | elif choice == 'i': | ||
107 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 107 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
108 | customer_Cart.print_descriptions() | 108 | customer_Cart.print_descriptions() | ||
109 | elif choice == 'r': | 109 | elif choice == 'r': | ||
110 | print('REMOVE ITEM FROM CART') | 110 | print('REMOVE ITEM FROM CART') | ||
111 | itemName = input('Enter name of item to remove:\n') | 111 | itemName = input('Enter name of item to remove:\n') | ||
112 | customer_Cart.remove_item(itemName) | 112 | customer_Cart.remove_item(itemName) | ||
113 | elif choice == 'c': | 113 | elif choice == 'c': | ||
114 | print('\nCHANGE ITEM QUANTITY') | 114 | print('\nCHANGE ITEM QUANTITY') | ||
115 | itemName = input('Enter the item name:\n') | 115 | itemName = input('Enter the item name:\n') | ||
116 | qty = int(input('Enter the new quantity:\n')) | 116 | qty = int(input('Enter the new quantity:\n')) | ||
117 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 117 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
118 | customer_Cart.modify_item(itemToPurchase) | 118 | customer_Cart.modify_item(itemToPurchase) | ||
119 | if __name__ == "__main__": | 119 | if __name__ == "__main__": | ||
120 | customer_name = input("Enter customer's name:\n") | 120 | customer_name = input("Enter customer's name:\n") | ||
121 | current_date = input("Enter today's date:\n") | 121 | current_date = input("Enter today's date:\n") | ||
122 | print("\nCustomer name: {}".format(customer_name)) | 122 | print("\nCustomer name: {}".format(customer_name)) | ||
123 | print("Today's date: {}".format(current_date)) | 123 | print("Today's date: {}".format(current_date)) | ||
124 | newCart = ShoppingCart(customer_name, current_date) | 124 | newCart = ShoppingCart(customer_name, current_date) | ||
125 | choice = '' | 125 | choice = '' | ||
126 | while choice != 'q': | 126 | while choice != 'q': | ||
127 | print() | 127 | print() | ||
128 | print_menu() | 128 | print_menu() | ||
129 | choice = input('Choose an option:\n') | 129 | choice = input('Choose an option:\n') | ||
130 | while choice != 'a' and choice != 'o' and choice != 'i' and choice != 'q | 130 | while choice != 'a' and choice != 'o' and choice != 'i' and choice != 'q | ||
> | ' and choice != 'r' and choice != 'c': | > | ' and choice != 'r' and choice != 'c': | ||
131 | choice = input('Choose an option:\n') | 131 | choice = input('Choose an option:\n') | ||
132 | execute_menu(choice, newCart)import math | 132 | execute_menu(choice, newCart)import math | ||
133 | class pt3d: | 133 | class pt3d: | ||
n | 134 | def __init__(self,x=0,y=0,z=0): | n | 134 | def __init__(self, x = 0, y = 0, z = 0): |
135 | self.x= x | 135 | self.x = x | ||
136 | self.y= y | 136 | self.y = y | ||
137 | self.z= z | 137 | self.z = z | ||
138 | def __add__(self, new): | 138 | def __add__(self,variable): | ||
139 | x = self.x + new.x | 139 | return pt3d((self.x + variable.x),(self.y + variable.y),(self.z + variab | ||
> | le.z)) | ||||
140 | y = self.y + new.y | ||||
141 | z = self.z + new.z | ||||
142 | return pt3d(x, y,z) | ||||
143 | def __sub__(self, new): | ||||
144 | return math.sqrt(math.pow(new.x-self.x, 2) + math.pow(new.y-self.y, 2)+m | ||||
> | ath.pow(new.z-self.z, 2)) | ||||
145 | def __eq__(self, new): | ||||
146 | return new.x==self.x and new.y==self.y and new.z==self.z | ||||
147 | def __str__(self): | 140 | def __str__(self): | ||
n | 148 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | n | 141 | return "<{},{},{}>".format(self.x,self.y,self.z) |
142 | def __sub__(self, variable): | ||||
143 | return math.sqrt((self.x - variable.x)**2 + (self.y - variable.y)**2 + ( | ||||
> | self.z - variable.z)**2) | ||||
144 | def __eq__(self,variable): | ||||
145 | return ((self.x == variable.x) and (self.y == variable.y) and (self.z == | ||||
> | variable.z)) | ||||
149 | if __name__ == '__main__': | 146 | if __name__ == '__main__': | ||
t | 150 | p1 = pt3d(1, 1, 1) | t | 147 | p1 = pt3d(1,1,1) |
151 | p2 = pt3d(2, 2, 2) | 148 | p2 = pt3d(2,2,2) | ||
152 | print(p1+p2) | 149 | print(p1 + p2) | ||
153 | print(p1-p2) | 150 | print(p1 - p2) | ||
154 | print(p1==p2) | 151 | print(p1 == p2) | ||
155 | print(p1+p1==p2) | 152 | print(p1 + p1 == p2) | ||
156 | print(p1==p2+pt3d(-1,-1,-1)) | 153 | print(p1 == p2 + pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (math.pi * self.radius * self.radius) | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return (2 * math.pi * self.radius) | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter())) |
14 | class ItemToPurchase: | ||||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_description=description | 17 | self.item_description=description | ||
17 | self.item_price=price | 18 | self.item_price=price | ||
18 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 20 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 21 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 22 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 25 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 26 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 28 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 30 | tremove_item = False | ||
30 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 34 | tremove_item = True | ||
34 | break | 35 | break | ||
35 | if not tremove_item: | 36 | if not tremove_item: | ||
36 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
37 | def modify_item(self, itemToPurchase): | 38 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 39 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 42 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 44 | break | ||
44 | if not tmodify_item: | 45 | if not tmodify_item: | ||
45 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
46 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 48 | num_items = 0 | ||
48 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 51 | return num_items | ||
51 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 53 | total_cost = 0 | ||
53 | cost = 0 | 54 | cost = 0 | ||
54 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 57 | total_cost += cost | ||
57 | return total_cost | 58 | return total_cost | ||
58 | def print_total(self): | 59 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
62 | else: | 63 | else: | ||
63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
65 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
66 | total = item.item_price * item.item_quantity | 67 | total = item.item_price * item.item_quantity | ||
67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
68 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
69 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
70 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
71 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 73 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
75 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
76 | item.print_item_description() | 77 | item.print_item_description() | ||
77 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
78 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
79 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
80 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
81 | 'r - Remove item from the cart\n' | 82 | 'r - Remove item from the cart\n' | ||
82 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
83 | "i - Output item's descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
84 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
85 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
86 | command = '' | 87 | command = '' | ||
87 | while(command != 'q'): | 88 | while(command != 'q'): | ||
88 | print(menu) | 89 | print(menu) | ||
89 | command = input('Choose an option:') | 90 | command = input('Choose an option:') | ||
90 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
91 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
92 | if(command == 'a'): | 93 | if(command == 'a'): | ||
93 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
94 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
95 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
96 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
97 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
99 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
100 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
101 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
102 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
103 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
105 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
106 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
107 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
108 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
109 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
110 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
111 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
112 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
113 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
115 | customer_Cart.modify_item(itemToPurchase) | 116 | customer_Cart.modify_item(itemToPurchase) | ||
116 | if __name__ == "__main__": | 117 | if __name__ == "__main__": | ||
117 | customer_name = input("Enter customer's name:\n") | 118 | customer_name = input("Enter customer's name:\n") | ||
118 | current_date = input("Enter today's date:\n") | 119 | current_date = input("Enter today's date:\n") | ||
119 | print("\nCustomer name: %s" %customer_name) | 120 | print("\nCustomer name: %s" %customer_name) | ||
120 | print("Today's date: %s" %current_date) | 121 | print("Today's date: %s" %current_date) | ||
121 | newCart = ShoppingCart(customer_name, current_date) | 122 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 122 | print_menu(newCart) | n | 123 | print_menu(newCart)from math import sqrt |
123 | import math | ||||
124 | class pt3d: | 124 | class pt3d: | ||
n | 125 | def __init__(self,x=0,y=0,z=0): | n | 125 | def __init__(self, x, y, z): |
126 | self.x = x | 126 | self.x = x | ||
127 | self.y = y | 127 | self.y = y | ||
128 | self.z = z | 128 | self.z = z | ||
n | 129 | def __add__(self,pt): | n | 129 | def __add__(self, other): |
130 | if isinstance(pt,pt3d): | ||||
131 | return pt3d(self.x + pt.x , self.y + pt.y , self.z + pt.z) | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
132 | def __sub__(self,pt): | 131 | def __sub__(self, other): | ||
133 | if isinstance(pt,pt3d): | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | ||||
134 | A = pow((self.x - pt.x),2) | ||||
135 | B = pow((self.y - pt.y),2) | ||||
136 | C = pow((self.z - pt.z),2) | ||||
137 | return math.sqrt(A + B + C) | ||||
138 | def __eq__(self,pt): | 133 | def __eq__(self, other): | ||
139 | if self.x == pt.x and self.y == pt.y and self.z == pt.z: | 134 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
140 | return True | ||||
141 | else: | ||||
142 | return False | ||||
143 | def __str__(self): | 135 | def __str__(self): | ||
t | 144 | return "<{},{},{}>".format(self.x,self.y,self.z) | t | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
137 | p1 = pt3d(1, 1, 1) | ||||
138 | p2 = pt3d(2, 2, 2) | ||||
139 | print(p1 + p2) | ||||
140 | print(p1 - p2) | ||||
141 | print(p1 == p2) | ||||
142 | print(p1+p1 == p2) | ||||
143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
4 | self.radius=radius | ||||
5 | def get_radius(self): | ||||
6 | return self.radius | ||||
7 | def set_radius(self,radius): | ||||
8 | self.radius = radius | 4 | self.radius = radius | ||
9 | def area(self): | 5 | def area(self): | ||
n | 10 | return math.pi*self.radius*self.radius | n | 6 | return math.pi*(self.radius**2) |
11 | def perimeter(self): | 7 | def perimeter(self): | ||
12 | return 2*math.pi*self.radius | 8 | return 2*math.pi*self.radius | ||
13 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
n | n | 10 | x = int(input()) | ||
14 | circle = Circle(5) | 11 | NewCircle = Circle(x) | ||
15 | print("Testing Circle methods (radius =5)\n") | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
16 | print("get_area():",circle.get_area()) | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
17 | print("get_perimeter():",circle.get_perimeter())class ItemToPurchase: | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
18 | def __init__(self, name='none', | ||||
19 | price=0, quantity=0, | ||||
20 | description='none'): | ||||
21 | self.item_name=name | 15 | self.item_name=name | ||
22 | self.item_description=description | 16 | self.item_description=description | ||
23 | self.item_price=price | 17 | self.item_price=price | ||
24 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
25 | def print_item_description(self): | 19 | def print_item_description(self): | ||
26 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
27 | class ShoppingCart: | 21 | class ShoppingCart: | ||
n | 28 | def __init__(self, customer_name = 'none', | n | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', |
> | cart_items = []): | ||||
29 | current_date = 'January 1, 2016', | ||||
30 | cart_items = []): | ||||
31 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
32 | self.current_date = current_date | 24 | self.current_date = current_date | ||
33 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
n | 34 | def add_item(self, | n | 26 | def add_item(self, itemToPurchase): |
35 | itemToPurchase): | ||||
36 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
n | 37 | def remove_item(self, | n | 28 | def remove_item(self, itemName): |
38 | itemName): | ||||
39 | tremove_item = False | 29 | tremove_item = False | ||
40 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
41 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
42 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
43 | tremove_item = True | 33 | tremove_item = True | ||
44 | break | 34 | break | ||
45 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 46 | print('Item not found in cart. Nothing removed.') | n | 36 | print('Item not found in the cart. Nothing removed.') |
47 | def modify_item(self, | 37 | def modify_item(self, itemToPurchase): | ||
48 | itemToPurchase): | ||||
49 | tmodify_item = False | 38 | tmodify_item = False | ||
50 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
51 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
52 | tmodify_item = True | 41 | tmodify_item = True | ||
53 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
54 | break | 43 | break | ||
55 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 56 | print('Item not found in cart. Nothing modified.') | n | 45 | print('Item not found in the cart. Nothing modified.') |
57 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
58 | num_items = 0 | 47 | num_items = 0 | ||
59 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
60 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
61 | return num_items | 50 | return num_items | ||
62 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
63 | total_cost = 0 | 52 | total_cost = 0 | ||
64 | cost = 0 | 53 | cost = 0 | ||
65 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
66 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
67 | total_cost += cost | 56 | total_cost += cost | ||
68 | return total_cost | 57 | return total_cost | ||
69 | def print_total(self): | 58 | def print_total(self): | ||
70 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
71 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
72 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 62 | else: | ||
n | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, | n | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | ent_date)) | ||||
75 | self.current_date)) | ||||
76 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
77 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
78 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
n | 79 | print('%s %d @ $%d = $%d' % (item.item_name, | n | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, |
> | item.item_price, total)) | ||||
80 | item.item_quantity, | ||||
81 | item.item_price, total)) | ||||
82 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
83 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
84 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
85 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
86 | else: | 72 | else: | ||
n | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, | n | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | ent_date)) | ||||
88 | self.current_date)) | ||||
89 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
90 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
91 | item.print_item_description() | 76 | item.print_item_description() | ||
92 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
93 | customer_Cart = newCart | 78 | customer_Cart = newCart | ||
94 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
95 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
96 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
97 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
98 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
99 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
100 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
101 | command = '' | 86 | command = '' | ||
102 | while(command != 'q'): | 87 | while(command != 'q'): | ||
103 | print(menu) | 88 | print(menu) | ||
104 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
105 | while(command != 'a' and command != 'o' and command != 'i' and command != | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
106 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
107 | if(command == 'a'): | 92 | if(command == 'a'): | ||
108 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
109 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
110 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
111 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
112 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
n | 113 | itemtoPurchase = ItemToPurchase(item_name, | n | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, |
> | item_description) | ||||
114 | item_price, | ||||
115 | item_quantity, | ||||
116 | item_description) | ||||
117 | customer_Cart.add_item(itemtoPurchase) | 99 | customer_Cart.add_item(itemtoPurchase) | ||
118 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
119 | print('\nOUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
120 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
121 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
122 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
123 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
124 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
125 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 126 | itemName = input('Enter name of item to remove:\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
127 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
128 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
129 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
n | 130 | itemName = input('Enter the item name:\n') | n | 112 | itemName = input('Enter name of item :\n') |
131 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
132 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
133 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
134 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
135 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
136 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
137 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
138 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
n | 139 | newCart = ShoppingCart(customer_name, | n | 121 | newCart = ShoppingCart(customer_name, current_date) |
140 | current_date) | ||||
141 | print_menu(newCart) from math import sqrt | 122 | print_menu(newCart)from math import sqrt | ||
142 | class pt3d: | 123 | class pt3d: | ||
143 | def __init__(self,x=0,y=0,z=0): | 124 | def __init__(self, x, y, z): | ||
144 | self.x = x | 125 | self.x = x | ||
145 | self.y = y | 126 | self.y = y | ||
146 | self.z = z | 127 | self.z = z | ||
n | 147 | def __add__(self, other): | n | 128 | def __add__(self, other): |
148 | return pt3d(self.x + other.x, self.y + other.y,self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
149 | def __sub__(self,other): | 130 | def __sub__(self, other): | ||
150 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
n | 151 | def __eq__(self,other): | n | 132 | def __eq__(self, other): |
152 | return self.x == other.x and self.y == other.y and self.z == other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
153 | def __str__(self): | 134 | def __str__(self): | ||
n | 154 | return '<{},{},{}>'.format(self.x, self.y, self.z) | n | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
155 | p1 = pt3d(1,1,1) | 136 | p1 = pt3d(1, 1, 1) | ||
156 | p2 = pt3d(2,2,2) | 137 | p2 = pt3d(2, 2, 2) | ||
157 | print(p1 + p2) | 138 | print(p1 + p2) | ||
n | 158 | print(p1-p2) | n | 139 | print(p1 - p2) |
159 | print(p1 == p2) | 140 | print(p1 == p2) | ||
160 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
t | 161 | print(p1==p2+pt3d(-1,-1,-1)) | t | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,x): | n | 3 | def __init__(self,radius): |
4 | self.radius = x | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area = math.pi * (self.radius)**2 | n | 6 | return (self.radius)**2 * math.pi |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter = 2 * math.pi * self.radius | n | 8 | return 2*math.pi * (self.radius) |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | n | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des |
> | cription='none'): | ||||
17 | self.item_name = name | 15 | self.item_name = item_name | ||
18 | self.item_price = price | 16 | self.item_price = item_price | ||
19 | self.item_quantity = quantity | 17 | self.item_quantity = item_quantity | ||
20 | self.item_description = description | 18 | self.item_description = item_description | ||
21 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
n | 22 | total = self.item_quantity*self.item_price | n | 20 | totalCost = self.item_quantity*self.item_price |
23 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | 21 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | ||
> | f.item_price, Cost)) | > | f.item_price, totalCost)) | ||
24 | def print_item_description(self): | 22 | def print_item_description(self): | ||
n | 25 | print('{}: {}'.format(self.item_name,self.item_description)) | n | 23 | print(f'{self.item_name}: {item_description}') |
26 | class ShoppingCart: | 24 | class ShoppingCart: | ||
n | 27 | def __init__(self, customer='none', current='January 1, 2016', items=[]): | n | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016', car |
> | t_items=[]): | ||||
28 | self.customer_name = customer | 26 | self.customer_name = customer_name | ||
29 | self.current_date = current | 27 | self.current_date = current_date | ||
30 | self.cart_items = items | 28 | self.cart_items = cart_items | ||
31 | def add_item(self, string): | 29 | def add_item(self, string): | ||
32 | print('\nADD ITEM TO CART', end='\n') | 30 | print('\nADD ITEM TO CART', end='\n') | ||
33 | item_name = str(input('Enter the item name:')) | 31 | item_name = str(input('Enter the item name:')) | ||
34 | item_description = str(input('\nEnter the item description:')) | 32 | item_description = str(input('\nEnter the item description:')) | ||
35 | item_price = int(input('\nEnter the item price:')) | 33 | item_price = int(input('\nEnter the item price:')) | ||
36 | item_quantity = int(input('\nEnter the item quantity:')) | 34 | item_quantity = int(input('\nEnter the item quantity:')) | ||
37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 35 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
38 | def remove_item(self): | 36 | def remove_item(self): | ||
39 | print('\nREMOVE ITEM FROM CART', end='\n') | 37 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
40 | string = str(input('Enter name of item to remove:')) | 38 | string = str(input('Enter name of item to remove:')) | ||
n | 41 | i = 0 | n | 39 | n = 0 |
42 | for item in self.cart_items: | 40 | for item in self.cart_items: | ||
43 | if (item.item_name == string): | 41 | if (item.item_name == string): | ||
n | 44 | del self.cart_items[i] | n | 42 | del self.cart_items[n] |
45 | i += 1 | 43 | n += 1 | ||
46 | jawn = True | 44 | f = True | ||
47 | break | 45 | break | ||
48 | else: | 46 | else: | ||
n | 49 | jawn = False | n | 47 | f = False |
50 | if (jawn == False): | 48 | if (f == False): | ||
51 | print('Item not found in cart. Nothing removed') | 49 | print('Item not found in cart. Nothing removed') | ||
52 | def modify_item(self): | 50 | def modify_item(self): | ||
53 | print('\nCHANGE ITEM QUANTITY', end='\n') | 51 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
54 | name = str(input('Enter the item name:')) | 52 | name = str(input('Enter the item name:')) | ||
55 | for item in self.cart_items: | 53 | for item in self.cart_items: | ||
56 | if (item.item_name == name): | 54 | if (item.item_name == name): | ||
57 | quantity = int(input('Enter the new quantity:')) | 55 | quantity = int(input('Enter the new quantity:')) | ||
58 | item.item_quantity = quantity | 56 | item.item_quantity = quantity | ||
n | 59 | jawn = True | n | 57 | f = True |
60 | break | 58 | break | ||
61 | else: | 59 | else: | ||
n | 62 | jawn = False | n | 60 | f = False |
63 | if (jawn == False): | 61 | if (f == False): | ||
64 | print('Item not found in cart. Nothing modified') | 62 | print('Item not found in cart. Nothing modified') | ||
65 | def get_num_items_in_cart(self): | 63 | def get_num_items_in_cart(self): | ||
66 | num_items = 0 | 64 | num_items = 0 | ||
67 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
68 | num_items = num_items + item.item_quantity | 66 | num_items = num_items + item.item_quantity | ||
69 | return num_items | 67 | return num_items | ||
70 | def get_cost_of_cart(self): | 68 | def get_cost_of_cart(self): | ||
n | 71 | total_cost = 0 | n | 69 | Total_Cost = 0 |
72 | cost = 0 | 70 | cost = 0 | ||
73 | for item in self.cart_items: | 71 | for item in self.cart_items: | ||
74 | cost = (item.item_quantity * item.item_price) | 72 | cost = (item.item_quantity * item.item_price) | ||
n | 75 | total_cost += cost | n | 73 | Total_Cost += cost |
76 | return total_cost | 74 | return Total_Cost | ||
77 | def print_total(self): | 75 | def print_total(self): | ||
n | 78 | total_cost = self.get_cost_of_cart() | n | 76 | Total_Cost = self.get_cost_of_cart() |
79 | if (total_cost == 0): | 77 | if (Total_Cost == 0): | ||
80 | print('SHOPPING CART IS EMPTY') | 78 | print('SHOPPING CART IS EMPTY') | ||
81 | else: | 79 | else: | ||
82 | self.output_cart() | 80 | self.output_cart() | ||
83 | def print_descriptions(self): | 81 | def print_descriptions(self): | ||
84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 82 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
86 | print('\nItem Descriptions', end='\n') | 84 | print('\nItem Descriptions', end='\n') | ||
87 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 86 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
89 | def output_cart(self): | 87 | def output_cart(self): | ||
90 | new = ShoppingCart() | 88 | new = ShoppingCart() | ||
91 | print('\nOUTPUT SHOPPING CART', end='\n') | 89 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 90 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 91 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
n | 94 | tc = 0 | n | 92 | T = 0 |
95 | for item in self.cart_items: | 93 | for item in self.cart_items: | ||
96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 94 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
97 | item.item_price, (item.item_quantit | 95 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
n | 98 | tc += (item.item_quantity * item.item_price) | n | 96 | T += (item.item_quantity * item.item_price) |
99 | print('\nTotal: ${}'.format(tc), end='\n') | 97 | print('\nTotal: ${}'.format(T), end='\n') | ||
100 | def print_menu(ShoppingCart): | 98 | def print_menu(ShoppingCart): | ||
101 | customer_Cart = newCart | 99 | customer_Cart = newCart | ||
102 | string = '' | 100 | string = '' | ||
103 | menu = ('\nMENU\n' | 101 | menu = ('\nMENU\n' | ||
104 | 'a - Add item to cart\n' | 102 | 'a - Add item to cart\n' | ||
105 | 'r - Remove item from cart\n' | 103 | 'r - Remove item from cart\n' | ||
106 | 'c - Change item quantity\n' | 104 | 'c - Change item quantity\n' | ||
107 | 'i - Output items\' descriptions\n' | 105 | 'i - Output items\' descriptions\n' | ||
108 | 'o - Output shopping cart\n' | 106 | 'o - Output shopping cart\n' | ||
109 | 'q - Quit\n') | 107 | 'q - Quit\n') | ||
110 | command = '' | 108 | command = '' | ||
111 | while (command != 'q'): | 109 | while (command != 'q'): | ||
112 | string = '' | 110 | string = '' | ||
113 | print(menu, end='\n') | 111 | print(menu, end='\n') | ||
114 | command = input('Choose an option:') | 112 | command = input('Choose an option:') | ||
115 | while (command != 'a' and command != 'o' and command != 'i' and comm | 113 | while (command != 'a' and command != 'o' and command != 'i' and comm | ||
> | and != 'r' | > | and != 'r' | ||
116 | and command != 'c' and command != 'q'): | 114 | and command != 'c' and command != 'q'): | ||
117 | command = input('Choose an option:') | 115 | command = input('Choose an option:') | ||
118 | if (command == 'a'): | 116 | if (command == 'a'): | ||
119 | customer_Cart.add_item(string) | 117 | customer_Cart.add_item(string) | ||
120 | if (command == 'o'): | 118 | if (command == 'o'): | ||
121 | customer_Cart.output_cart() | 119 | customer_Cart.output_cart() | ||
122 | if (command == 'i'): | 120 | if (command == 'i'): | ||
123 | customer_Cart.print_descriptions() | 121 | customer_Cart.print_descriptions() | ||
124 | if (command == 'r'): | 122 | if (command == 'r'): | ||
125 | customer_Cart.remove_item() | 123 | customer_Cart.remove_item() | ||
126 | if (command == 'c'): | 124 | if (command == 'c'): | ||
127 | customer_Cart.modify_item() | 125 | customer_Cart.modify_item() | ||
128 | customer_name = str(input('Enter customer\'s name:')) | 126 | customer_name = str(input('Enter customer\'s name:')) | ||
129 | current_date = str(input('\nEnter today\'s date:')) | 127 | current_date = str(input('\nEnter today\'s date:')) | ||
130 | print() | 128 | print() | ||
131 | print('\nCustomer name:', customer_name,) | 129 | print('\nCustomer name:', customer_name,) | ||
132 | print('Today\'s date:', current_date,) | 130 | print('Today\'s date:', current_date,) | ||
133 | newCart = ShoppingCart(customer_name, current_date) | 131 | newCart = ShoppingCart(customer_name, current_date) | ||
134 | print_menu(newCart)import math | 132 | print_menu(newCart)import math | ||
135 | class pt3d: | 133 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 134 | def __init__(self,x=0,y=0,z=0): | ||
137 | self.x = x | 135 | self.x = x | ||
138 | self.y = y | 136 | self.y = y | ||
139 | self.z = z | 137 | self.z = z | ||
140 | def __eq__(self, other): | 138 | def __eq__(self, other): | ||
141 | if self.x == other.x and self.y == other.y and self.z == other.z: | 139 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||
142 | return True | 140 | return True | ||
143 | else: | 141 | else: | ||
144 | return False | 142 | return False | ||
145 | def __add__(self,other): | 143 | def __add__(self,other): | ||
146 | addition_x = self.x + other.x | 144 | addition_x = self.x + other.x | ||
147 | addition_y = self.y + other.y | 145 | addition_y = self.y + other.y | ||
148 | addition_z = self.z + other.z | 146 | addition_z = self.z + other.z | ||
149 | return pt3d(addition_x,addition_y,addition_z) | 147 | return pt3d(addition_x,addition_y,addition_z) | ||
n | 150 | def __eq__(self, other): | n | 148 | def __eq__(self, other): |
151 | if self.x == other.x and self.y == other.y and self.z == other.z: | 149 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||
152 | return True | 150 | return True | ||
153 | else: | 151 | else: | ||
154 | return False | 152 | return False | ||
155 | def __add__(self,other): | 153 | def __add__(self,other): | ||
n | 156 | addition_x = self.x + other.x | n | 154 | add_x = self.x + other.x |
157 | addition_y = self.y + other.y | 155 | add_y = self.y + other.y | ||
158 | addition_z = self.z + other.z | 156 | add_z = self.z + other.z | ||
159 | return pt3d(addition_x,addition_y,addition_z) | 157 | return pt3d(add_x,add_y,add_z) | ||
160 | def __sub__(self, other): | 158 | def __sub__(self, other): | ||
t | 161 | subtraction_x = self.x - other.x | t | 159 | sub_x = self.x - other.x |
162 | subtraction_y = self.y - other.y | 160 | sub_y = self.y - other.y | ||
163 | subtraction_z = self.z - other.z | 161 | sub_z = self.z - other.z | ||
164 | distance = math.sqrt(subtraction_x**2 + subtraction_y**2 + subtraction_z | 162 | dist = math.sqrt(sub_x**2 + sub_y**2 + sub_z**2) | ||
> | **2) | ||||
165 | return distance | 163 | return dist | ||
166 | def __str__(self): | 164 | def __str__(self): | ||
167 | return (f'<{self.x},{self.y},{self.z}>') | 165 | return (f'<{self.x},{self.y},{self.z}>') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self,x): |
4 | self.radius = radius | 4 | self.radius = x | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.radius)**2 * math.pi | n | 6 | area = math.pi * (self.radius)**2 |
7 | return area | ||||
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 8 | return 2*math.pi * (self.radius) | n | 9 | perimeter = 2 * math.pi * self.radius |
10 | return perimeter | ||||
9 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
10 | x = int(input()) | 12 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | n | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): |
> | cription='none'): | ||||
15 | self.item_name = item_name | 17 | self.item_name = name | ||
16 | self.item_price = item_price | 18 | self.item_price = price | ||
17 | self.item_quantity = item_quantity | 19 | self.item_quantity = quantity | ||
18 | self.item_description = item_description | 20 | self.item_description = description | ||
19 | def print_item_cost(self): | 21 | def print_item_cost(self): | ||
n | 20 | totalCost = self.item_quantity*self.item_price | n | 22 | total = self.item_quantity*self.item_price |
21 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | 23 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | ||
> | f.item_price, totalCost)) | > | f.item_price, Cost)) | ||
22 | def print_item_description(self): | 24 | def print_item_description(self): | ||
n | 23 | print(f'{self.item_name}: {item_description}') | n | 25 | print('{}: {}'.format(self.item_name,self.item_description)) |
24 | class ShoppingCart: | 26 | class ShoppingCart: | ||
n | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | n | 27 | def __init__(self, customer='none', current='January 1, 2016', items=[]): |
> | t_items=[]): | ||||
26 | self.customer_name = customer_name | 28 | self.customer_name = customer | ||
27 | self.current_date = current_date | 29 | self.current_date = current | ||
28 | self.cart_items = cart_items | 30 | self.cart_items = items | ||
29 | def add_item(self, string): | 31 | def add_item(self, string): | ||
30 | print('\nADD ITEM TO CART', end='\n') | 32 | print('\nADD ITEM TO CART', end='\n') | ||
31 | item_name = str(input('Enter the item name:')) | 33 | item_name = str(input('Enter the item name:')) | ||
32 | item_description = str(input('\nEnter the item description:')) | 34 | item_description = str(input('\nEnter the item description:')) | ||
33 | item_price = int(input('\nEnter the item price:')) | 35 | item_price = int(input('\nEnter the item price:')) | ||
34 | item_quantity = int(input('\nEnter the item quantity:')) | 36 | item_quantity = int(input('\nEnter the item quantity:')) | ||
35 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
36 | def remove_item(self): | 38 | def remove_item(self): | ||
37 | print('\nREMOVE ITEM FROM CART', end='\n') | 39 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
38 | string = str(input('Enter name of item to remove:')) | 40 | string = str(input('Enter name of item to remove:')) | ||
n | 39 | n = 0 | n | 41 | i = 0 |
40 | for item in self.cart_items: | 42 | for item in self.cart_items: | ||
41 | if (item.item_name == string): | 43 | if (item.item_name == string): | ||
n | 42 | del self.cart_items[n] | n | 44 | del self.cart_items[i] |
43 | n += 1 | 45 | i += 1 | ||
44 | f = True | 46 | jawn = True | ||
45 | break | 47 | break | ||
46 | else: | 48 | else: | ||
n | 47 | f = False | n | 49 | jawn = False |
48 | if (f == False): | 50 | if (jawn == False): | ||
49 | print('Item not found in cart. Nothing removed') | 51 | print('Item not found in cart. Nothing removed') | ||
50 | def modify_item(self): | 52 | def modify_item(self): | ||
51 | print('\nCHANGE ITEM QUANTITY', end='\n') | 53 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
52 | name = str(input('Enter the item name:')) | 54 | name = str(input('Enter the item name:')) | ||
53 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
54 | if (item.item_name == name): | 56 | if (item.item_name == name): | ||
55 | quantity = int(input('Enter the new quantity:')) | 57 | quantity = int(input('Enter the new quantity:')) | ||
56 | item.item_quantity = quantity | 58 | item.item_quantity = quantity | ||
n | 57 | f = True | n | 59 | jawn = True |
58 | break | 60 | break | ||
59 | else: | 61 | else: | ||
n | 60 | f = False | n | 62 | jawn = False |
61 | if (f == False): | 63 | if (jawn == False): | ||
62 | print('Item not found in cart. Nothing modified') | 64 | print('Item not found in cart. Nothing modified') | ||
63 | def get_num_items_in_cart(self): | 65 | def get_num_items_in_cart(self): | ||
64 | num_items = 0 | 66 | num_items = 0 | ||
65 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
66 | num_items = num_items + item.item_quantity | 68 | num_items = num_items + item.item_quantity | ||
67 | return num_items | 69 | return num_items | ||
68 | def get_cost_of_cart(self): | 70 | def get_cost_of_cart(self): | ||
n | 69 | Total_Cost = 0 | n | 71 | total_cost = 0 |
70 | cost = 0 | 72 | cost = 0 | ||
71 | for item in self.cart_items: | 73 | for item in self.cart_items: | ||
72 | cost = (item.item_quantity * item.item_price) | 74 | cost = (item.item_quantity * item.item_price) | ||
n | 73 | Total_Cost += cost | n | 75 | total_cost += cost |
74 | return Total_Cost | 76 | return total_cost | ||
75 | def print_total(self): | 77 | def print_total(self): | ||
n | 76 | Total_Cost = self.get_cost_of_cart() | n | 78 | total_cost = self.get_cost_of_cart() |
77 | if (Total_Cost == 0): | 79 | if (total_cost == 0): | ||
78 | print('SHOPPING CART IS EMPTY') | 80 | print('SHOPPING CART IS EMPTY') | ||
79 | else: | 81 | else: | ||
80 | self.output_cart() | 82 | self.output_cart() | ||
81 | def print_descriptions(self): | 83 | def print_descriptions(self): | ||
82 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
84 | print('\nItem Descriptions', end='\n') | 86 | print('\nItem Descriptions', end='\n') | ||
85 | for item in self.cart_items: | 87 | for item in self.cart_items: | ||
86 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
87 | def output_cart(self): | 89 | def output_cart(self): | ||
88 | new = ShoppingCart() | 90 | new = ShoppingCart() | ||
89 | print('\nOUTPUT SHOPPING CART', end='\n') | 91 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
90 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
91 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
n | 92 | T = 0 | n | 94 | tc = 0 |
93 | for item in self.cart_items: | 95 | for item in self.cart_items: | ||
94 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
95 | item.item_price, (item.item_quantit | 97 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
n | 96 | T += (item.item_quantity * item.item_price) | n | 98 | tc += (item.item_quantity * item.item_price) |
97 | print('\nTotal: ${}'.format(T), end='\n') | 99 | print('\nTotal: ${}'.format(tc), end='\n') | ||
98 | def print_menu(ShoppingCart): | 100 | def print_menu(ShoppingCart): | ||
99 | customer_Cart = newCart | 101 | customer_Cart = newCart | ||
100 | string = '' | 102 | string = '' | ||
101 | menu = ('\nMENU\n' | 103 | menu = ('\nMENU\n' | ||
102 | 'a - Add item to cart\n' | 104 | 'a - Add item to cart\n' | ||
103 | 'r - Remove item from cart\n' | 105 | 'r - Remove item from cart\n' | ||
104 | 'c - Change item quantity\n' | 106 | 'c - Change item quantity\n' | ||
105 | 'i - Output items\' descriptions\n' | 107 | 'i - Output items\' descriptions\n' | ||
106 | 'o - Output shopping cart\n' | 108 | 'o - Output shopping cart\n' | ||
107 | 'q - Quit\n') | 109 | 'q - Quit\n') | ||
108 | command = '' | 110 | command = '' | ||
109 | while (command != 'q'): | 111 | while (command != 'q'): | ||
110 | string = '' | 112 | string = '' | ||
111 | print(menu, end='\n') | 113 | print(menu, end='\n') | ||
112 | command = input('Choose an option:') | 114 | command = input('Choose an option:') | ||
113 | while (command != 'a' and command != 'o' and command != 'i' and comm | 115 | while (command != 'a' and command != 'o' and command != 'i' and comm | ||
> | and != 'r' | > | and != 'r' | ||
114 | and command != 'c' and command != 'q'): | 116 | and command != 'c' and command != 'q'): | ||
115 | command = input('Choose an option:') | 117 | command = input('Choose an option:') | ||
116 | if (command == 'a'): | 118 | if (command == 'a'): | ||
117 | customer_Cart.add_item(string) | 119 | customer_Cart.add_item(string) | ||
118 | if (command == 'o'): | 120 | if (command == 'o'): | ||
119 | customer_Cart.output_cart() | 121 | customer_Cart.output_cart() | ||
120 | if (command == 'i'): | 122 | if (command == 'i'): | ||
121 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
122 | if (command == 'r'): | 124 | if (command == 'r'): | ||
123 | customer_Cart.remove_item() | 125 | customer_Cart.remove_item() | ||
124 | if (command == 'c'): | 126 | if (command == 'c'): | ||
125 | customer_Cart.modify_item() | 127 | customer_Cart.modify_item() | ||
126 | customer_name = str(input('Enter customer\'s name:')) | 128 | customer_name = str(input('Enter customer\'s name:')) | ||
127 | current_date = str(input('\nEnter today\'s date:')) | 129 | current_date = str(input('\nEnter today\'s date:')) | ||
128 | print() | 130 | print() | ||
129 | print('\nCustomer name:', customer_name,) | 131 | print('\nCustomer name:', customer_name,) | ||
130 | print('Today\'s date:', current_date,) | 132 | print('Today\'s date:', current_date,) | ||
131 | newCart = ShoppingCart(customer_name, current_date) | 133 | newCart = ShoppingCart(customer_name, current_date) | ||
132 | print_menu(newCart)import math | 134 | print_menu(newCart)import math | ||
133 | class pt3d: | 135 | class pt3d: | ||
134 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
135 | self.x = x | 137 | self.x = x | ||
136 | self.y = y | 138 | self.y = y | ||
137 | self.z = z | 139 | self.z = z | ||
138 | def __eq__(self, other): | 140 | def __eq__(self, other): | ||
139 | if self.x == other.x and self.y == other.y and self.z == other.z: | 141 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||
140 | return True | 142 | return True | ||
141 | else: | 143 | else: | ||
142 | return False | 144 | return False | ||
143 | def __add__(self,other): | 145 | def __add__(self,other): | ||
144 | addition_x = self.x + other.x | 146 | addition_x = self.x + other.x | ||
145 | addition_y = self.y + other.y | 147 | addition_y = self.y + other.y | ||
146 | addition_z = self.z + other.z | 148 | addition_z = self.z + other.z | ||
147 | return pt3d(addition_x,addition_y,addition_z) | 149 | return pt3d(addition_x,addition_y,addition_z) | ||
n | 148 | def __eq__(self, other): | n | 150 | def __eq__(self, other): |
149 | if self.x == other.x and self.y == other.y and self.z == other.z: | 151 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||
150 | return True | 152 | return True | ||
151 | else: | 153 | else: | ||
152 | return False | 154 | return False | ||
153 | def __add__(self,other): | 155 | def __add__(self,other): | ||
n | 154 | add_x = self.x + other.x | n | 156 | addition_x = self.x + other.x |
155 | add_y = self.y + other.y | 157 | addition_y = self.y + other.y | ||
156 | add_z = self.z + other.z | 158 | addition_z = self.z + other.z | ||
157 | return pt3d(add_x,add_y,add_z) | 159 | return pt3d(addition_x,addition_y,addition_z) | ||
158 | def __sub__(self, other): | 160 | def __sub__(self, other): | ||
t | 159 | sub_x = self.x - other.x | t | 161 | subtraction_x = self.x - other.x |
160 | sub_y = self.y - other.y | 162 | subtraction_y = self.y - other.y | ||
161 | sub_z = self.z - other.z | 163 | subtraction_z = self.z - other.z | ||
162 | dist = math.sqrt(sub_x**2 + sub_y**2 + sub_z**2) | 164 | distance = math.sqrt(subtraction_x**2 + subtraction_y**2 + subtraction_z | ||
> | **2) | ||||
163 | return dist | 165 | return distance | ||
164 | def __str__(self): | 166 | def __str__(self): | ||
165 | return (f'<{self.x},{self.y},{self.z}>') | 167 | return (f'<{self.x},{self.y},{self.z}>') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius=0): | n | 3 | def __init__(self,radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area = math.pi*self.radius**2 | n | 6 | return math.pi*self.radius*self.radius |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter = 2*math.pi*self.radius | n | 8 | return 2*math.pi*self.radius |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
17 | self.item_name = item_name | 15 | self.item_name = item_name | ||
18 | self.item_price = item_price | 16 | self.item_price = item_price | ||
19 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
20 | self.item_description = item_description | 18 | self.item_description = item_description | ||
21 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
22 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
23 | (self.item_quantity * self.item_price)) | 21 | (self.item_quantity * self.item_price)) | ||
24 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
25 | return string, cost | 23 | return string, cost | ||
26 | def print_item_description(self): | 24 | def print_item_description(self): | ||
27 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
28 | print(string) | 26 | print(string) | ||
29 | return string | 27 | return string | ||
30 | class ShoppingCart: | 28 | class ShoppingCart: | ||
n | 31 | def __init__(self, customer_name = 'none', current_date = 'February 1, 2016' | n | 29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', |
> | , cart_items = []): | > | cart_items = []): | ||
32 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
33 | self.current_date = current_date | 31 | self.current_date = current_date | ||
34 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
35 | def add_item(self): | 33 | def add_item(self): | ||
36 | print('ADD ITEM TO CART') | 34 | print('ADD ITEM TO CART') | ||
37 | item_name = str(input('Enter the item name:')) | 35 | item_name = str(input('Enter the item name:')) | ||
38 | print() | 36 | print() | ||
39 | item_description = str(input('Enter the item description:')) | 37 | item_description = str(input('Enter the item description:')) | ||
40 | print() | 38 | print() | ||
41 | item_price = int(input('Enter the item price:')) | 39 | item_price = int(input('Enter the item price:')) | ||
42 | print() | 40 | print() | ||
43 | item_quantity = int(input('Enter the item quantity:')) | 41 | item_quantity = int(input('Enter the item quantity:')) | ||
44 | print() | 42 | print() | ||
45 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
46 | def remove_item(self): | 44 | def remove_item(self): | ||
47 | print('REMOVE ITEM FROM CART') | 45 | print('REMOVE ITEM FROM CART') | ||
48 | string = str(input('Enter name of item to remove:')) | 46 | string = str(input('Enter name of item to remove:')) | ||
49 | print() | 47 | print() | ||
50 | i = 0 | 48 | i = 0 | ||
51 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
52 | if(item.item_name == string): | 50 | if(item.item_name == string): | ||
53 | del self.cart_items[i] | 51 | del self.cart_items[i] | ||
54 | flag=True | 52 | flag=True | ||
55 | break | 53 | break | ||
56 | else: | 54 | else: | ||
57 | flag=False | 55 | flag=False | ||
58 | i += 1 | 56 | i += 1 | ||
59 | if(flag==False): | 57 | if(flag==False): | ||
60 | print('Item not found in cart. Nothing removed.') | 58 | print('Item not found in cart. Nothing removed.') | ||
61 | def modify_item(self): | 59 | def modify_item(self): | ||
62 | print('CHANGE ITEM QUANTITY') | 60 | print('CHANGE ITEM QUANTITY') | ||
63 | name = str(input('Enter the item name:')) | 61 | name = str(input('Enter the item name:')) | ||
64 | print() | 62 | print() | ||
65 | for item in self.cart_items: | 63 | for item in self.cart_items: | ||
66 | if(item.item_name == name): | 64 | if(item.item_name == name): | ||
67 | quantity = int(input('Enter the new quantity:')) | 65 | quantity = int(input('Enter the new quantity:')) | ||
68 | print() | 66 | print() | ||
69 | item.item_quantity = quantity | 67 | item.item_quantity = quantity | ||
70 | flag=True | 68 | flag=True | ||
71 | break | 69 | break | ||
72 | else: | 70 | else: | ||
73 | flag=False | 71 | flag=False | ||
74 | if(flag==False): | 72 | if(flag==False): | ||
75 | print('Item not found in cart. Nothing modified.') | 73 | print('Item not found in cart. Nothing modified.') | ||
76 | print() | 74 | print() | ||
77 | def get_num_items_in_cart(self): | 75 | def get_num_items_in_cart(self): | ||
78 | num_items=0 | 76 | num_items=0 | ||
n | n | 77 | for item in self.cart_items: | ||
79 | num_items= num_items+ item.item_quantity | 78 | num_items= num_items+item.item_quantity | ||
80 | return num_items | 79 | return num_items | ||
81 | def get_cost_of_cart(self): | 80 | def get_cost_of_cart(self): | ||
82 | total_cost = 0 | 81 | total_cost = 0 | ||
83 | cost = 0 | 82 | cost = 0 | ||
84 | for item in self.cart_items: | 83 | for item in self.cart_items: | ||
85 | cost = (item.item_quantity * item.item_price) | 84 | cost = (item.item_quantity * item.item_price) | ||
86 | total_cost += cost | 85 | total_cost += cost | ||
87 | return total_cost | 86 | return total_cost | ||
88 | def print_total(self): | 87 | def print_total(self): | ||
89 | total_cost = self.get_cost_of_cart() | 88 | total_cost = self.get_cost_of_cart() | ||
90 | if (total_cost == 0): | 89 | if (total_cost == 0): | ||
91 | print('SHOPPING CART IS EMPTY') | 90 | print('SHOPPING CART IS EMPTY') | ||
92 | else: | 91 | else: | ||
93 | self.output_cart() | 92 | self.output_cart() | ||
94 | def print_descriptions(self): | 93 | def print_descriptions(self): | ||
95 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
96 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
97 | print('\nItem Descriptions') | 96 | print('\nItem Descriptions') | ||
98 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
99 | print('{}: {}'.format(item.item_name, item.item_description)) | 98 | print('{}: {}'.format(item.item_name, item.item_description)) | ||
100 | def output_cart(self): | 99 | def output_cart(self): | ||
101 | print('OUTPUT SHOPPING CART') | 100 | print('OUTPUT SHOPPING CART') | ||
102 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
103 | print('Number of Items:', self.get_num_items_in_cart()) | 102 | print('Number of Items:', self.get_num_items_in_cart()) | ||
104 | print() | 103 | print() | ||
105 | tc = 0 | 104 | tc = 0 | ||
106 | for item in self.cart_items: | 105 | for item in self.cart_items: | ||
107 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
108 | item.item_price, (item.item_quantity * item.item_price))) | 107 | item.item_price, (item.item_quantity * item.item_price))) | ||
109 | tc += (item.item_quantity * item.item_price) | 108 | tc += (item.item_quantity * item.item_price) | ||
110 | if len(self.cart_items) == 0: | 109 | if len(self.cart_items) == 0: | ||
111 | print('SHOPPING CART IS EMPTY') | 110 | print('SHOPPING CART IS EMPTY') | ||
112 | print() | 111 | print() | ||
113 | print('Total: ${}'.format(tc)) | 112 | print('Total: ${}'.format(tc)) | ||
114 | def print_menu(customer_Cart): | 113 | def print_menu(customer_Cart): | ||
115 | menu = ('\nMENU\n' | 114 | menu = ('\nMENU\n' | ||
116 | 'a - Add item to cart\n' | 115 | 'a - Add item to cart\n' | ||
117 | 'r - Remove item from cart\n' | 116 | 'r - Remove item from cart\n' | ||
118 | 'c - Change item quantity\n' | 117 | 'c - Change item quantity\n' | ||
119 | 'i - Output items\' descriptions\n' | 118 | 'i - Output items\' descriptions\n' | ||
120 | 'o - Output shopping cart\n' | 119 | 'o - Output shopping cart\n' | ||
121 | 'q - Quit\n') | 120 | 'q - Quit\n') | ||
122 | command = '' | 121 | command = '' | ||
123 | while(command != 'q'): | 122 | while(command != 'q'): | ||
124 | print(menu) | 123 | print(menu) | ||
125 | command = input('Choose an option:') | 124 | command = input('Choose an option:') | ||
126 | print() | 125 | print() | ||
127 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' | > | = 'r' | ||
128 | and command != 'c' and command != 'q'): | 127 | and command != 'c' and command != 'q'): | ||
129 | command = input('Choose an option:') | 128 | command = input('Choose an option:') | ||
130 | print() | 129 | print() | ||
131 | if(command == 'a'): | 130 | if(command == 'a'): | ||
132 | customer_Cart.add_item() | 131 | customer_Cart.add_item() | ||
133 | if(command == 'o'): | 132 | if(command == 'o'): | ||
134 | customer_Cart.output_cart() | 133 | customer_Cart.output_cart() | ||
135 | if(command == 'i'): | 134 | if(command == 'i'): | ||
136 | customer_Cart.print_descriptions() | 135 | customer_Cart.print_descriptions() | ||
137 | if(command == 'r'): | 136 | if(command == 'r'): | ||
138 | customer_Cart.remove_item() | 137 | customer_Cart.remove_item() | ||
139 | if(command == 'c'): | 138 | if(command == 'c'): | ||
140 | customer_Cart.modify_item() | 139 | customer_Cart.modify_item() | ||
141 | def main(): | 140 | def main(): | ||
142 | customer_name = str(input('Enter customer\'s name:')) | 141 | customer_name = str(input('Enter customer\'s name:')) | ||
143 | print() | 142 | print() | ||
144 | current_date = str(input('Enter today\'s date:')) | 143 | current_date = str(input('Enter today\'s date:')) | ||
145 | print('\n') | 144 | print('\n') | ||
146 | print('Customer name:', customer_name, end='\n') | 145 | print('Customer name:', customer_name, end='\n') | ||
147 | print('Today\'s date:', current_date, end='\n') | 146 | print('Today\'s date:', current_date, end='\n') | ||
148 | newCart = ShoppingCart(customer_name, current_date) | 147 | newCart = ShoppingCart(customer_name, current_date) | ||
149 | print_menu(newCart) | 148 | print_menu(newCart) | ||
150 | if __name__ == '__main__': | 149 | if __name__ == '__main__': | ||
n | 151 | main()import math | n | 150 | main()from math import sqrt |
152 | class pt3d: | 151 | class pt3d: | ||
153 | def __init__(self, x=0, y=0, z=0): | 152 | def __init__(self, x=0, y=0, z=0): | ||
154 | self.x = x | 153 | self.x = x | ||
155 | self.y = y | 154 | self.y = y | ||
156 | self.z = z | 155 | self.z = z | ||
n | n | 156 | def __add__(self, other): | ||
157 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||||
158 | def __sub__(self, other): | ||||
159 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||||
> | her.z)**2) | ||||
157 | def __eq__(self, other): | 160 | def __eq__(self, other): | ||
n | 158 | if self.x == other.x and self.y == other.y and self.z == other.z: | n | 161 | return self.x == other.x & self.y == other.y & self.z == other.z |
159 | return True | ||||
160 | else: | ||||
161 | return False | ||||
162 | def __add__(self,other): | ||||
163 | addition_x = self.x + other.x | ||||
164 | addition_y = self.y + other.y | ||||
165 | addition_z = self.z + other.z | ||||
166 | return pt3d(addition_x,addition_y,addition_z) | ||||
167 | def __eq__(self, other): | ||||
168 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||||
169 | return True | ||||
170 | else: | ||||
171 | return False | ||||
172 | def __add__(self,other): | ||||
173 | addition_x = self.x + other.x | ||||
174 | addition_y = self.y + other.y | ||||
175 | addition_z = self.z + other.z | ||||
176 | return pt3d(addition_x,addition_y,addition_z) | ||||
177 | def __sub__(self,other): | ||||
178 | subtraction_x = self.x - other.x | ||||
179 | subtraction_y = self.y - other.y | ||||
180 | subtraction_z = self.z - other.z | ||||
181 | distance = math.sqrt(subtraction_x**2 + subtraction_y**2 + subtraction_z | ||||
> | **2) | ||||
182 | return distance | ||||
183 | def __str__(self): | 162 | def __str__(self): | ||
t | 184 | return(f'<{self.x},{self.y},{self.z}>') | t | 163 | return (f'<{self.x}, {self.y}, {self.z}>') |
164 | p1 = pt3d(1, 1, 1) | ||||
165 | p2 = pt3d(2, 2, 2) | ||||
166 | print(p1 + p2) | ||||
167 | print(p1 - p2) | ||||
168 | print(p1 == p2) | ||||
169 | print(p1+p1 == p2) | ||||
170 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, x): | n | 3 | def __init__(self, radius): |
4 | self.radius = x | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius**2*math.pi | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*math.pi | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
n | 20 | print('%s: %s' % (self.item_name, self.item_description)) | n | 20 | print('%s: %s' % (self.item_name, self.item_description)) |
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
n | 25 | self.cart_items = cart_items | n | 25 | self.cart_items = cart_items |
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
n | 36 | print('Item not found in cart. Nothing removed.') | n | 36 | print('Item not found in the cart. Nothing removed.') |
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 41 | tmodify_item = True | n | 41 | tmodify_item = True |
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
n | 45 | print('Item not found in cart. Nothing modified.') | n | 45 | print('Item not found in the cart. Nothing modified.') |
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
n | 57 | return total_cost | n | 57 | return total_cost |
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
n | 62 | print('Total: $0') | n | ||
63 | else: | 62 | else: | ||
n | 64 | print("{}'s Shopping Cart - {}".format(self.customer_name, self.curre | n | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | nt_date)) | > | ent_date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
67 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
69 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
70 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
71 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
72 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
73 | else: | 72 | else: | ||
n | 74 | print("{}'s Shopping Cart - {}\n".format(self.customer_name, self.cur | n | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr |
> | rent_date)) | > | ent_date)) | ||
75 | print('Item Descriptions') | 74 | print('\nItem Descriptions') | ||
76 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
77 | item.print_item_description() | 76 | item.print_item_description() | ||
78 | def print_menu(newCart): | 77 | def print_menu(newCart): | ||
n | 79 | customer_Cart = newCart | n | 78 | customer_Cart = newCart |
80 | menu = ('\nMENU\n') | 79 | menu = ('\nMENU\n' | ||
81 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
82 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
83 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
84 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
85 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
86 | 'q - Quit\n' | 85 | 'q - Quit\n') | ||
87 | command = '' | 86 | command = '' | ||
88 | while(command != 'q'): | 87 | while(command != 'q'): | ||
89 | print(menu) | 88 | print(menu) | ||
90 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
91 | while(command != 'a' and command != 'o' and command != 'i' and command != 'q | 90 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | ' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
92 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
93 | if(command == 'a'): | 92 | if(command == 'a'): | ||
94 | print("ADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
95 | print('Enter the item name:') | ||||
96 | print('Enter the item description:') | ||||
97 | print('Enter the item price:') | ||||
98 | print('Enter the item quantity:') | ||||
99 | item_name = input('Enter the item name:') | 94 | item_name = input('Enter the item name:\n') | ||
100 | item_description = input('Enter the item description:') | 95 | item_description = input('Enter the item description:\n') | ||
101 | item_price = int(input('Enter the item price:')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
102 | item_quantity = int(input('Enter the item quantity:')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
n | 104 | customer_Cart.add_item(itemtoPurchase) | n | 99 | customer_Cart.add_item(itemtoPurchase) |
105 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
106 | print('OUTPUT SHOPPING CART') | 101 | print('\nOUTPUT SHOPPING CART') | ||
107 | customer_Cart.print_total() | 102 | customer_Cart.print_total() | ||
108 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
109 | print("OUTPUT ITEMS' DESCRIPTIONS") | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
110 | customer_Cart.print_descriptions() | 105 | customer_Cart.print_descriptions() | ||
n | 111 | elif(command == 'r'): | n | 106 | elif(command == 'r'): |
112 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
n | 113 | itemName = input('Enter name of item to remove:\n') | n | 108 | itemName = input('Enter name of item to remove :\n') |
114 | customer_Cart.remove_item(itemName) | 109 | customer_Cart.remove_item(itemName) | ||
n | 115 | elif(command == 'c'): | n | 110 | elif(command == 'c'): |
116 | print('CHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
117 | itemName = input('Enter the item name:\n') | 112 | itemName = input('Enter name of item :\n') | ||
118 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity :\n')) | ||
119 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
120 | customer_Cart.modify_item(itemToPurchase) | 115 | customer_Cart.modify_item(itemToPurchase) | ||
121 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
n | 122 | customer_name = input("Enter customer's name:\n") | n | 117 | customer_name = input("Enter customer's name:\n") |
123 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
124 | print() | ||||
125 | print("Customer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
126 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
127 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
128 | print_menu(newCart) | 122 | print_menu(newCart)from math import sqrt | ||
129 | from math import sqrt | ||||
130 | class pt3d: | 123 | class pt3d: | ||
n | 131 | def __init__(self, x=0, y=0, z=0): | n | 124 | def __init__(self, x, y, z): |
132 | self.x = x | 125 | self.x = x | ||
133 | self.y = y | 126 | self.y = y | ||
134 | self.z = z | 127 | self.z = z | ||
135 | def __add__(self, other): | 128 | def __add__(self, other): | ||
136 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
137 | def __sub__(self, other): | 130 | def __sub__(self, other): | ||
n | 138 | return sqrt(((self.x - other.x)**2) + ((self.y - other.y)**2) + ((self.z | n | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot |
> | - other.z)**2)) | > | her.z)**2) | ||
139 | def __eq__(self, other): | 132 | def __eq__(self, other): | ||
n | 140 | return ((self.x == other.x) & (self.y == other.y) & (self.z == other.z)) | n | 133 | return self.x == other.x and self.y == other.y and self.z == other.z |
141 | def __str__(self): | 134 | def __str__(self): | ||
t | 142 | return '<{},{},{}>'.format(self.x, self.y, self.z) | t | 135 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
143 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
144 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
145 | print(p1 + p2) | 138 | print(p1 + p2) | ||
146 | print(p1 - p2) | 139 | print(p1 - p2) | ||
147 | print(p1 == p2) | 140 | print(p1 == p2) | ||
148 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
149 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math as azmain | n | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius): | n | 3 | def __init__(self,radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | n | 6 | import math as m | ||
6 | return (azmain.pi * (self.radius ** 2)) | 7 | return m.pi*(self.radius)**2 | ||
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | n | 9 | import math as m | ||
8 | return (azmain.pi * self.radius * 2) | 10 | return 2*m.pi*self.radius | ||
9 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
10 | x = int(input()) | 12 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))choball = {} | 15 | print('{:.5f}'.format(NewCircle.perimeter()))choball = {} | ||
14 | class ItemToPurchase: | 16 | class ItemToPurchase: | ||
15 | global choball | 17 | global choball | ||
16 | def __init__(self): | 18 | def __init__(self): | ||
17 | self.item_name = 'none' | 19 | self.item_name = 'none' | ||
18 | self.item_price = 0 | 20 | self.item_price = 0 | ||
19 | self.item_quantity = 0 | 21 | self.item_quantity = 0 | ||
20 | self.item_description = 'none' | 22 | self.item_description = 'none' | ||
21 | def __str__(self): | 23 | def __str__(self): | ||
22 | cost = self.item_price * self.item_quantity | 24 | cost = self.item_price * self.item_quantity | ||
23 | return f'{self.item_name} {self.item_quantity} @ ${self.item_price} = ${ | 25 | return f'{self.item_name} {self.item_quantity} @ ${self.item_price} = ${ | ||
> | cost}' | > | cost}' | ||
24 | def print_item_cost(self): | 26 | def print_item_cost(self): | ||
25 | print(self) | 27 | print(self) | ||
26 | def print_item_description(self): | 28 | def print_item_description(self): | ||
27 | print(f'{self.item_name}: {self.item_description}') | 29 | print(f'{self.item_name}: {self.item_description}') | ||
28 | class ShoppingCart: | 30 | class ShoppingCart: | ||
29 | def __init__(self, name='none', date='January 1, 2016'): | 31 | def __init__(self, name='none', date='January 1, 2016'): | ||
30 | self.customer_name = name | 32 | self.customer_name = name | ||
31 | self.current_date = date | 33 | self.current_date = date | ||
32 | self.cart_items = [] | 34 | self.cart_items = [] | ||
33 | def add_item(self, item): | 35 | def add_item(self, item): | ||
34 | self.cart_items.append(item.item_name) | 36 | self.cart_items.append(item.item_name) | ||
35 | def remove_item(self, item): | 37 | def remove_item(self, item): | ||
36 | if item in self.cart_items: | 38 | if item in self.cart_items: | ||
37 | self.cart_items.remove(item) | 39 | self.cart_items.remove(item) | ||
38 | else: | 40 | else: | ||
39 | print('Item not found in cart. Nothing removed.') | 41 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_items(self, item): | 42 | def modify_items(self, item): | ||
41 | newItem = ItemToPurchase() | 43 | newItem = ItemToPurchase() | ||
42 | print('Enter the new quantity:') | 44 | print('Enter the new quantity:') | ||
43 | if item in self.cart_items: | 45 | if item in self.cart_items: | ||
44 | newItem.item_name = item | 46 | newItem.item_name = item | ||
45 | newItem.item_description = choball[item][2] | 47 | newItem.item_description = choball[item][2] | ||
46 | newItem.item_price = choball[item][0] | 48 | newItem.item_price = choball[item][0] | ||
47 | newItem.item_quantity = input() | 49 | newItem.item_quantity = input() | ||
48 | choball[item][1] = newItem.item_quantity | 50 | choball[item][1] = newItem.item_quantity | ||
49 | else: | 51 | else: | ||
50 | print('Item not found in cart. Nothing modified.') | 52 | print('Item not found in cart. Nothing modified.') | ||
51 | def get_num_items_in_cart(self): | 53 | def get_num_items_in_cart(self): | ||
52 | numItems = 0 | 54 | numItems = 0 | ||
53 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
54 | if item in choball: | 56 | if item in choball: | ||
55 | numItems += int(choball[item][1]) | 57 | numItems += int(choball[item][1]) | ||
56 | return numItems | 58 | return numItems | ||
57 | def get_cost_of_cart(self): | 59 | def get_cost_of_cart(self): | ||
58 | totalCost = 0 | 60 | totalCost = 0 | ||
59 | for item in self.cart_items: | 61 | for item in self.cart_items: | ||
60 | if item in choball: | 62 | if item in choball: | ||
61 | p=int(choball[item][0]) | 63 | p=int(choball[item][0]) | ||
62 | q=int(choball[item][1]) | 64 | q=int(choball[item][1]) | ||
n | 63 | itemCost = int(p * q) | n | 65 | itemCost = p * q |
64 | totalCost += itemCost | 66 | totalCost += itemCost | ||
65 | return totalCost | 67 | return totalCost | ||
66 | def print_total(self): | 68 | def print_total(self): | ||
67 | numItems = self.get_num_items_in_cart() | 69 | numItems = self.get_num_items_in_cart() | ||
68 | if numItems == 0: | 70 | if numItems == 0: | ||
69 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 71 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
70 | print(f"Number of Items: {numItems}") | 72 | print(f"Number of Items: {numItems}") | ||
71 | print() | 73 | print() | ||
72 | print('SHOPPING CART IS EMPTY') | 74 | print('SHOPPING CART IS EMPTY') | ||
73 | print() | 75 | print() | ||
74 | print(f"Total: ${self.get_cost_of_cart()}") | 76 | print(f"Total: ${self.get_cost_of_cart()}") | ||
75 | else: | 77 | else: | ||
76 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 78 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
77 | print(f"Number of Items: {numItems}") | 79 | print(f"Number of Items: {numItems}") | ||
78 | print() | 80 | print() | ||
79 | for item in self.cart_items: | 81 | for item in self.cart_items: | ||
80 | cost = int(choball[item][1]) * int(choball[item][0]) | 82 | cost = int(choball[item][1]) * int(choball[item][0]) | ||
81 | print(f'{item} {choball[item][1]} @ ${choball[item][0]} = ${cost | 83 | print(f'{item} {choball[item][1]} @ ${choball[item][0]} = ${cost | ||
> | }') | > | }') | ||
82 | print() | 84 | print() | ||
83 | print(f"Total: ${self.get_cost_of_cart()}") | 85 | print(f"Total: ${self.get_cost_of_cart()}") | ||
84 | def print_descriptions(self): | 86 | def print_descriptions(self): | ||
85 | print("OUTPUT ITEMS' DESCRIPTIONS") | 87 | print("OUTPUT ITEMS' DESCRIPTIONS") | ||
86 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 88 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
87 | print() | 89 | print() | ||
88 | print('Item Descriptions') | 90 | print('Item Descriptions') | ||
89 | for item in self.cart_items: | 91 | for item in self.cart_items: | ||
90 | print(f'{item}: {choball[item][2]}') | 92 | print(f'{item}: {choball[item][2]}') | ||
91 | def print_menu(): | 93 | def print_menu(): | ||
92 | print('MENU') | 94 | print('MENU') | ||
93 | print('a - Add item to cart') | 95 | print('a - Add item to cart') | ||
94 | print('r - Remove item from cart') | 96 | print('r - Remove item from cart') | ||
95 | print('c - Change item quantity') | 97 | print('c - Change item quantity') | ||
96 | print("i - Output items' descriptions") | 98 | print("i - Output items' descriptions") | ||
97 | print('o - Output shopping cart') | 99 | print('o - Output shopping cart') | ||
98 | print('q - Quit') | 100 | print('q - Quit') | ||
99 | def execute_menu(choice, cart): | 101 | def execute_menu(choice, cart): | ||
100 | if choice == 'a': | 102 | if choice == 'a': | ||
101 | item = ItemToPurchase() | 103 | item = ItemToPurchase() | ||
102 | print('ADD ITEM TO CART') | 104 | print('ADD ITEM TO CART') | ||
103 | print('Enter the item name:') | 105 | print('Enter the item name:') | ||
104 | item.item_name = input() | 106 | item.item_name = input() | ||
105 | print('Enter the item description:') | 107 | print('Enter the item description:') | ||
106 | item.item_description = input() | 108 | item.item_description = input() | ||
107 | print('Enter the item price:') | 109 | print('Enter the item price:') | ||
108 | item.item_price = input() | 110 | item.item_price = input() | ||
109 | print('Enter the item quantity:') | 111 | print('Enter the item quantity:') | ||
110 | item.item_quantity = input() | 112 | item.item_quantity = input() | ||
111 | cart.add_item(item) | 113 | cart.add_item(item) | ||
112 | choball[item.item_name] = [item.item_price, item.item_quantity, item.ite | 114 | choball[item.item_name] = [item.item_price, item.item_quantity, item.ite | ||
> | m_description] | > | m_description] | ||
113 | print() | 115 | print() | ||
114 | print_menu() | 116 | print_menu() | ||
115 | print() | 117 | print() | ||
116 | elif choice == 'r': | 118 | elif choice == 'r': | ||
117 | print('REMOVE ITEM FROM CART') | 119 | print('REMOVE ITEM FROM CART') | ||
118 | print('Enter name of item to remove:') | 120 | print('Enter name of item to remove:') | ||
119 | item = input() | 121 | item = input() | ||
120 | cart.remove_item(item) | 122 | cart.remove_item(item) | ||
121 | print() | 123 | print() | ||
122 | print_menu() | 124 | print_menu() | ||
123 | print() | 125 | print() | ||
124 | elif choice == 'c': | 126 | elif choice == 'c': | ||
125 | print('CHANGE ITEM QUANTITY') | 127 | print('CHANGE ITEM QUANTITY') | ||
126 | print('Enter the item name:') | 128 | print('Enter the item name:') | ||
127 | itema = input() | 129 | itema = input() | ||
128 | cart.modify_items(itema) | 130 | cart.modify_items(itema) | ||
129 | print() | 131 | print() | ||
130 | print_menu() | 132 | print_menu() | ||
131 | print() | 133 | print() | ||
132 | elif choice == 'i': | 134 | elif choice == 'i': | ||
133 | print("OUTPUT ITEMS' DESCRIPTIONS") | 135 | print("OUTPUT ITEMS' DESCRIPTIONS") | ||
134 | cart.print_descriptions() | 136 | cart.print_descriptions() | ||
135 | print() | 137 | print() | ||
136 | print_menu() | 138 | print_menu() | ||
137 | print() | 139 | print() | ||
138 | elif choice == 'o': | 140 | elif choice == 'o': | ||
139 | print('OUTPUT SHOPPING CART') | 141 | print('OUTPUT SHOPPING CART') | ||
140 | cart.print_total() | 142 | cart.print_total() | ||
141 | print() | 143 | print() | ||
142 | print_menu() | 144 | print_menu() | ||
143 | print() | 145 | print() | ||
144 | if __name__ == "__main__": | 146 | if __name__ == "__main__": | ||
145 | print("Enter customer's name:") | 147 | print("Enter customer's name:") | ||
146 | name = input() | 148 | name = input() | ||
147 | print("Enter today's date:") | 149 | print("Enter today's date:") | ||
148 | date = input() | 150 | date = input() | ||
149 | print() | 151 | print() | ||
150 | print(f'Customer name: {name}') | 152 | print(f'Customer name: {name}') | ||
151 | print(f"Today's date: {date}") | 153 | print(f"Today's date: {date}") | ||
152 | print() | 154 | print() | ||
153 | cart = ShoppingCart(name,date) | 155 | cart = ShoppingCart(name,date) | ||
154 | print_menu() | 156 | print_menu() | ||
155 | print() | 157 | print() | ||
156 | print("Choose an option:") | 158 | print("Choose an option:") | ||
157 | choice = input() | 159 | choice = input() | ||
158 | while choice != 'q': | 160 | while choice != 'q': | ||
159 | execute_menu(choice, cart) | 161 | execute_menu(choice, cart) | ||
160 | print("Choose an option:") | 162 | print("Choose an option:") | ||
161 | choice = input() | 163 | choice = input() | ||
n | 162 | import math as choball | n | ||
163 | class pt3d: | 164 | class pt3d: | ||
n | 164 | def __init__(self, x=0, y=0, z=0): | n | 165 | def __init__(self,x=0,y=0,z=0): |
165 | self.x = x | 166 | self.x=x | ||
166 | self.y = y | 167 | self.y=y | ||
167 | self.z = z | 168 | self.z=z | ||
168 | def __str__(self) -> str: | 169 | def __str__(self): | ||
169 | return f'<{self.x},{self.y},{self.z}>' | 170 | return "<{},{},{}>".format(self.x,self.y,self.z) | ||
170 | def __add__(self, other): | 171 | def __add__(self,other): | ||
171 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 172 | return pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | ||
172 | def __sub__(self, other): | 173 | def __sub__(self,other): | ||
173 | return choball.sqrt((other.x - self.x)**2 + (other.y - self.y)**2 + (oth | 174 | import math as m | ||
> | er.z - self.z)**2) | ||||
175 | return m.sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other.z)** | ||||
> | 2) | ||||
174 | def __eq__(self, other): | 176 | def __eq__(self,other): | ||
175 | if (self.x == other.x) and (self.y == other.y) and (self.z == other.z): | 177 | if (self.x==other.x) and (self.y==other.y) and (self.z==other.z): | ||
176 | return True | 178 | return True | ||
t | 177 | else: | t | ||
178 | return False | 179 | return False | ||
179 | if __name__ == "__main__": | ||||
180 | a = pt3d(1,1,1) | ||||
181 | b = pt3d(2,2,2) | ||||
182 | print(a+b) | ||||
183 | print(a-b) | ||||
184 | a==b | ||||
185 | a+a==b | ||||
186 | a==b+pt3d(-1,-1,-1) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import math as azmain |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | import math as m | n | ||
7 | return m.pi*(self.radius)**2 | 6 | return (azmain.pi * (self.radius ** 2)) | ||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | import math as m | n | ||
10 | return 2*m.pi*self.radius | 8 | return (azmain.pi * self.radius * 2) | ||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))choball = {} | 13 | print('{:.5f}'.format(NewCircle.perimeter()))choball = {} | ||
16 | class ItemToPurchase: | 14 | class ItemToPurchase: | ||
17 | global choball | 15 | global choball | ||
18 | def __init__(self): | 16 | def __init__(self): | ||
19 | self.item_name = 'none' | 17 | self.item_name = 'none' | ||
20 | self.item_price = 0 | 18 | self.item_price = 0 | ||
21 | self.item_quantity = 0 | 19 | self.item_quantity = 0 | ||
22 | self.item_description = 'none' | 20 | self.item_description = 'none' | ||
23 | def __str__(self): | 21 | def __str__(self): | ||
24 | cost = self.item_price * self.item_quantity | 22 | cost = self.item_price * self.item_quantity | ||
25 | return f'{self.item_name} {self.item_quantity} @ ${self.item_price} = ${ | 23 | return f'{self.item_name} {self.item_quantity} @ ${self.item_price} = ${ | ||
> | cost}' | > | cost}' | ||
26 | def print_item_cost(self): | 24 | def print_item_cost(self): | ||
27 | print(self) | 25 | print(self) | ||
28 | def print_item_description(self): | 26 | def print_item_description(self): | ||
29 | print(f'{self.item_name}: {self.item_description}') | 27 | print(f'{self.item_name}: {self.item_description}') | ||
30 | class ShoppingCart: | 28 | class ShoppingCart: | ||
31 | def __init__(self, name='none', date='January 1, 2016'): | 29 | def __init__(self, name='none', date='January 1, 2016'): | ||
32 | self.customer_name = name | 30 | self.customer_name = name | ||
33 | self.current_date = date | 31 | self.current_date = date | ||
34 | self.cart_items = [] | 32 | self.cart_items = [] | ||
35 | def add_item(self, item): | 33 | def add_item(self, item): | ||
36 | self.cart_items.append(item.item_name) | 34 | self.cart_items.append(item.item_name) | ||
37 | def remove_item(self, item): | 35 | def remove_item(self, item): | ||
38 | if item in self.cart_items: | 36 | if item in self.cart_items: | ||
39 | self.cart_items.remove(item) | 37 | self.cart_items.remove(item) | ||
40 | else: | 38 | else: | ||
41 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
42 | def modify_items(self, item): | 40 | def modify_items(self, item): | ||
43 | newItem = ItemToPurchase() | 41 | newItem = ItemToPurchase() | ||
44 | print('Enter the new quantity:') | 42 | print('Enter the new quantity:') | ||
45 | if item in self.cart_items: | 43 | if item in self.cart_items: | ||
46 | newItem.item_name = item | 44 | newItem.item_name = item | ||
47 | newItem.item_description = choball[item][2] | 45 | newItem.item_description = choball[item][2] | ||
48 | newItem.item_price = choball[item][0] | 46 | newItem.item_price = choball[item][0] | ||
49 | newItem.item_quantity = input() | 47 | newItem.item_quantity = input() | ||
50 | choball[item][1] = newItem.item_quantity | 48 | choball[item][1] = newItem.item_quantity | ||
51 | else: | 49 | else: | ||
52 | print('Item not found in cart. Nothing modified.') | 50 | print('Item not found in cart. Nothing modified.') | ||
53 | def get_num_items_in_cart(self): | 51 | def get_num_items_in_cart(self): | ||
54 | numItems = 0 | 52 | numItems = 0 | ||
55 | for item in self.cart_items: | 53 | for item in self.cart_items: | ||
56 | if item in choball: | 54 | if item in choball: | ||
57 | numItems += int(choball[item][1]) | 55 | numItems += int(choball[item][1]) | ||
58 | return numItems | 56 | return numItems | ||
59 | def get_cost_of_cart(self): | 57 | def get_cost_of_cart(self): | ||
60 | totalCost = 0 | 58 | totalCost = 0 | ||
61 | for item in self.cart_items: | 59 | for item in self.cart_items: | ||
62 | if item in choball: | 60 | if item in choball: | ||
63 | p=int(choball[item][0]) | 61 | p=int(choball[item][0]) | ||
64 | q=int(choball[item][1]) | 62 | q=int(choball[item][1]) | ||
n | 65 | itemCost = p * q | n | 63 | itemCost = int(p * q) |
66 | totalCost += itemCost | 64 | totalCost += itemCost | ||
67 | return totalCost | 65 | return totalCost | ||
68 | def print_total(self): | 66 | def print_total(self): | ||
69 | numItems = self.get_num_items_in_cart() | 67 | numItems = self.get_num_items_in_cart() | ||
70 | if numItems == 0: | 68 | if numItems == 0: | ||
71 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 69 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
72 | print(f"Number of Items: {numItems}") | 70 | print(f"Number of Items: {numItems}") | ||
73 | print() | 71 | print() | ||
74 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
75 | print() | 73 | print() | ||
76 | print(f"Total: ${self.get_cost_of_cart()}") | 74 | print(f"Total: ${self.get_cost_of_cart()}") | ||
77 | else: | 75 | else: | ||
78 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 76 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
79 | print(f"Number of Items: {numItems}") | 77 | print(f"Number of Items: {numItems}") | ||
80 | print() | 78 | print() | ||
81 | for item in self.cart_items: | 79 | for item in self.cart_items: | ||
82 | cost = int(choball[item][1]) * int(choball[item][0]) | 80 | cost = int(choball[item][1]) * int(choball[item][0]) | ||
83 | print(f'{item} {choball[item][1]} @ ${choball[item][0]} = ${cost | 81 | print(f'{item} {choball[item][1]} @ ${choball[item][0]} = ${cost | ||
> | }') | > | }') | ||
84 | print() | 82 | print() | ||
85 | print(f"Total: ${self.get_cost_of_cart()}") | 83 | print(f"Total: ${self.get_cost_of_cart()}") | ||
86 | def print_descriptions(self): | 84 | def print_descriptions(self): | ||
87 | print("OUTPUT ITEMS' DESCRIPTIONS") | 85 | print("OUTPUT ITEMS' DESCRIPTIONS") | ||
88 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | 86 | print(f"{self.customer_name}'s Shopping Cart - {self.current_date}") | ||
89 | print() | 87 | print() | ||
90 | print('Item Descriptions') | 88 | print('Item Descriptions') | ||
91 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
92 | print(f'{item}: {choball[item][2]}') | 90 | print(f'{item}: {choball[item][2]}') | ||
93 | def print_menu(): | 91 | def print_menu(): | ||
94 | print('MENU') | 92 | print('MENU') | ||
95 | print('a - Add item to cart') | 93 | print('a - Add item to cart') | ||
96 | print('r - Remove item from cart') | 94 | print('r - Remove item from cart') | ||
97 | print('c - Change item quantity') | 95 | print('c - Change item quantity') | ||
98 | print("i - Output items' descriptions") | 96 | print("i - Output items' descriptions") | ||
99 | print('o - Output shopping cart') | 97 | print('o - Output shopping cart') | ||
100 | print('q - Quit') | 98 | print('q - Quit') | ||
101 | def execute_menu(choice, cart): | 99 | def execute_menu(choice, cart): | ||
102 | if choice == 'a': | 100 | if choice == 'a': | ||
103 | item = ItemToPurchase() | 101 | item = ItemToPurchase() | ||
104 | print('ADD ITEM TO CART') | 102 | print('ADD ITEM TO CART') | ||
105 | print('Enter the item name:') | 103 | print('Enter the item name:') | ||
106 | item.item_name = input() | 104 | item.item_name = input() | ||
107 | print('Enter the item description:') | 105 | print('Enter the item description:') | ||
108 | item.item_description = input() | 106 | item.item_description = input() | ||
109 | print('Enter the item price:') | 107 | print('Enter the item price:') | ||
110 | item.item_price = input() | 108 | item.item_price = input() | ||
111 | print('Enter the item quantity:') | 109 | print('Enter the item quantity:') | ||
112 | item.item_quantity = input() | 110 | item.item_quantity = input() | ||
113 | cart.add_item(item) | 111 | cart.add_item(item) | ||
114 | choball[item.item_name] = [item.item_price, item.item_quantity, item.ite | 112 | choball[item.item_name] = [item.item_price, item.item_quantity, item.ite | ||
> | m_description] | > | m_description] | ||
115 | print() | 113 | print() | ||
116 | print_menu() | 114 | print_menu() | ||
117 | print() | 115 | print() | ||
118 | elif choice == 'r': | 116 | elif choice == 'r': | ||
119 | print('REMOVE ITEM FROM CART') | 117 | print('REMOVE ITEM FROM CART') | ||
120 | print('Enter name of item to remove:') | 118 | print('Enter name of item to remove:') | ||
121 | item = input() | 119 | item = input() | ||
122 | cart.remove_item(item) | 120 | cart.remove_item(item) | ||
123 | print() | 121 | print() | ||
124 | print_menu() | 122 | print_menu() | ||
125 | print() | 123 | print() | ||
126 | elif choice == 'c': | 124 | elif choice == 'c': | ||
127 | print('CHANGE ITEM QUANTITY') | 125 | print('CHANGE ITEM QUANTITY') | ||
128 | print('Enter the item name:') | 126 | print('Enter the item name:') | ||
129 | itema = input() | 127 | itema = input() | ||
130 | cart.modify_items(itema) | 128 | cart.modify_items(itema) | ||
131 | print() | 129 | print() | ||
132 | print_menu() | 130 | print_menu() | ||
133 | print() | 131 | print() | ||
134 | elif choice == 'i': | 132 | elif choice == 'i': | ||
135 | print("OUTPUT ITEMS' DESCRIPTIONS") | 133 | print("OUTPUT ITEMS' DESCRIPTIONS") | ||
136 | cart.print_descriptions() | 134 | cart.print_descriptions() | ||
137 | print() | 135 | print() | ||
138 | print_menu() | 136 | print_menu() | ||
139 | print() | 137 | print() | ||
140 | elif choice == 'o': | 138 | elif choice == 'o': | ||
141 | print('OUTPUT SHOPPING CART') | 139 | print('OUTPUT SHOPPING CART') | ||
142 | cart.print_total() | 140 | cart.print_total() | ||
143 | print() | 141 | print() | ||
144 | print_menu() | 142 | print_menu() | ||
145 | print() | 143 | print() | ||
146 | if __name__ == "__main__": | 144 | if __name__ == "__main__": | ||
147 | print("Enter customer's name:") | 145 | print("Enter customer's name:") | ||
148 | name = input() | 146 | name = input() | ||
149 | print("Enter today's date:") | 147 | print("Enter today's date:") | ||
150 | date = input() | 148 | date = input() | ||
151 | print() | 149 | print() | ||
152 | print(f'Customer name: {name}') | 150 | print(f'Customer name: {name}') | ||
153 | print(f"Today's date: {date}") | 151 | print(f"Today's date: {date}") | ||
154 | print() | 152 | print() | ||
155 | cart = ShoppingCart(name,date) | 153 | cart = ShoppingCart(name,date) | ||
156 | print_menu() | 154 | print_menu() | ||
157 | print() | 155 | print() | ||
158 | print("Choose an option:") | 156 | print("Choose an option:") | ||
159 | choice = input() | 157 | choice = input() | ||
160 | while choice != 'q': | 158 | while choice != 'q': | ||
161 | execute_menu(choice, cart) | 159 | execute_menu(choice, cart) | ||
162 | print("Choose an option:") | 160 | print("Choose an option:") | ||
163 | choice = input() | 161 | choice = input() | ||
n | n | 162 | import math as choball | ||
164 | class pt3d: | 163 | class pt3d: | ||
n | 165 | def __init__(self,x=0,y=0,z=0): | n | 164 | def __init__(self, x=0, y=0, z=0): |
166 | self.x=x | 165 | self.x = x | ||
167 | self.y=y | 166 | self.y = y | ||
168 | self.z=z | 167 | self.z = z | ||
169 | def __str__(self): | 168 | def __str__(self) -> str: | ||
170 | return "<{},{},{}>".format(self.x,self.y,self.z) | 169 | return f'<{self.x},{self.y},{self.z}>' | ||
171 | def __add__(self,other): | 170 | def __add__(self, other): | ||
172 | return pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | 171 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
173 | def __sub__(self,other): | 172 | def __sub__(self, other): | ||
174 | import math as m | 173 | return choball.sqrt((other.x - self.x)**2 + (other.y - self.y)**2 + (oth | ||
> | er.z - self.z)**2) | ||||
175 | return m.sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other.z)** | ||||
> | 2) | ||||
176 | def __eq__(self,other): | 174 | def __eq__(self, other): | ||
177 | if (self.x==other.x) and (self.y==other.y) and (self.z==other.z): | 175 | if (self.x == other.x) and (self.y == other.y) and (self.z == other.z): | ||
178 | return True | 176 | return True | ||
t | t | 177 | else: | ||
179 | return False | 178 | return False | ||
179 | if __name__ == "__main__": | ||||
180 | a = pt3d(1,1,1) | ||||
181 | b = pt3d(2,2,2) | ||||
182 | print(a+b) | ||||
183 | print(a-b) | ||||
184 | a==b | ||||
185 | a+a==b | ||||
186 | a==b+pt3d(-1,-1,-1) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import math | ||
1 | class Circle(): | 2 | class Circle: | ||
2 | def __init__(self, r): | 3 | def __init__(self,radius): | ||
3 | self.radius = r | 4 | self.radius=radius | ||
4 | def area(self): | 5 | def area(self): | ||
n | 5 | return self.radius**2*3.14 | n | 6 | area=math.pi*self.radius**2 |
7 | return area | ||||
6 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 7 | return 2*self.radius*3.14 | n | 9 | perimeter=2*math.pi*self.radius |
10 | return perimeter | ||||
11 | if __name__=='__main__': | ||||
12 | x = int(input()) | ||||
8 | NewCircle = Circle() | 13 | NewCircle = Circle(x) | ||
9 | print(NewCircle.area()) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
10 | print(NewCircle.perimeter()) | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
11 | class ItemToPurchase: | ||||
12 | def __init__(self, item_name= 'none', item_price=0, item_quantity=0, item_de | 16 | def __init__(self, item_name= 'none', item_price=0, item_quantity=0, item_de | ||
> | scription = 'none'): | > | scription = 'none'): | ||
13 | self.item_name = item_name | 17 | self.item_name = item_name | ||
14 | self.item_price = item_price | 18 | self.item_price = item_price | ||
15 | self.item_quantity = item_quantity | 19 | self.item_quantity = item_quantity | ||
16 | self.item_description = item_description | 20 | self.item_description = item_description | ||
17 | def print_item_cost(self): | 21 | def print_item_cost(self): | ||
18 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 22 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, (self.item_quantity | > | self.item_price, (self.item_quantity | ||
19 | * self.item_price)) | 23 | * self.item_price)) | ||
20 | cost = self.item_quantity * self.item_price | 24 | cost = self.item_quantity * self.item_price | ||
21 | return string, cost | 25 | return string, cost | ||
22 | def print_item_description(self): | 26 | def print_item_description(self): | ||
23 | string = '{}: {}'.format(self.item_name, self.item_description) | 27 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
24 | print(string, end=' ') | 28 | print(string, end=' ') | ||
25 | return string | 29 | return string | ||
26 | class ShoppingCart: | 30 | class ShoppingCart: | ||
27 | def __init__(self,customer_name= None ,current_date='January 1,2016',cart_it | 31 | def __init__(self,customer_name= None ,current_date='January 1,2016',cart_it | ||
> | ems=[]): | > | ems=[]): | ||
28 | self.customer_name = customer_name | 32 | self.customer_name = customer_name | ||
29 | self.current_date = current_date | 33 | self.current_date = current_date | ||
30 | self.cart_items = cart_items | 34 | self.cart_items = cart_items | ||
31 | def add_item(self,): | 35 | def add_item(self,): | ||
32 | print('\nADD ITEM TO CART', end='\n') | 36 | print('\nADD ITEM TO CART', end='\n') | ||
33 | item_name = str(input('Enter the item name:')); | 37 | item_name = str(input('Enter the item name:')); | ||
34 | item_description = str(input('\nEnter the item description:')); | 38 | item_description = str(input('\nEnter the item description:')); | ||
35 | item_price = int(input('\nEnter the item price:')); | 39 | item_price = int(input('\nEnter the item price:')); | ||
36 | item_quantity = int(input('\nEnter the item quantity:\n')) | 40 | item_quantity = int(input('\nEnter the item quantity:\n')) | ||
37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, | 41 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description)) | > | item_description)) | ||
38 | def remove_item(self): | 42 | def remove_item(self): | ||
39 | print() | 43 | print() | ||
40 | print('REMOVE ITEM FROM CART', end='\n') | 44 | print('REMOVE ITEM FROM CART', end='\n') | ||
41 | string = str(input('Enter name of item to remove:\n')) | 45 | string = str(input('Enter name of item to remove:\n')) | ||
42 | i = 0 | 46 | i = 0 | ||
43 | for item in self.cart_items: | 47 | for item in self.cart_items: | ||
44 | if(item.item_name == string): | 48 | if(item.item_name == string): | ||
45 | del self.cart_items[i] | 49 | del self.cart_items[i] | ||
46 | i += 1 | 50 | i += 1 | ||
47 | flag=True | 51 | flag=True | ||
48 | break | 52 | break | ||
49 | else: | 53 | else: | ||
50 | flag=False | 54 | flag=False | ||
51 | if(flag==False): | 55 | if(flag==False): | ||
52 | print('Item not found in cart. Nothing removed.') | 56 | print('Item not found in cart. Nothing removed.') | ||
53 | def modify_item(self): | 57 | def modify_item(self): | ||
54 | print('\nCHANGE ITEM QUANTITY', end='\n') | 58 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
55 | name = str(input('Enter the item name:')) | 59 | name = str(input('Enter the item name:')) | ||
56 | for item in self.cart_items: | 60 | for item in self.cart_items: | ||
57 | if(item.item_name == name): | 61 | if(item.item_name == name): | ||
58 | quantity = int(input('Enter the new quantity:')) | 62 | quantity = int(input('Enter the new quantity:')) | ||
59 | item.item_quantity = quantity | 63 | item.item_quantity = quantity | ||
60 | flag=True | 64 | flag=True | ||
61 | break | 65 | break | ||
62 | else: | 66 | else: | ||
63 | flag=False | 67 | flag=False | ||
64 | if(flag==False): | 68 | if(flag==False): | ||
65 | print('Item not found in cart. Nothing modified.') | 69 | print('Item not found in cart. Nothing modified.') | ||
66 | def get_num_items_in_cart(self): | 70 | def get_num_items_in_cart(self): | ||
67 | num_items = 0 | 71 | num_items = 0 | ||
68 | for item in self.cart_items: | 72 | for item in self.cart_items: | ||
69 | num_items += item.item_quantity | 73 | num_items += item.item_quantity | ||
70 | return num_items | 74 | return num_items | ||
71 | def get_cost_of_cart(self): | 75 | def get_cost_of_cart(self): | ||
72 | total_cost = 0 | 76 | total_cost = 0 | ||
73 | cost = 0 | 77 | cost = 0 | ||
74 | for item in self.cart_items: | 78 | for item in self.cart_items: | ||
75 | cost = (item.item_quantity * item.item_price) | 79 | cost = (item.item_quantity * item.item_price) | ||
76 | total_cost += cost | 80 | total_cost += cost | ||
77 | return total_cost | 81 | return total_cost | ||
78 | def print_total(): | 82 | def print_total(): | ||
79 | total_cost = self.get_cost_of_cart() | 83 | total_cost = self.get_cost_of_cart() | ||
80 | if (total_cost == 0): | 84 | if (total_cost == 0): | ||
81 | print('SHOPPING CART IS EMPTY') | 85 | print('SHOPPING CART IS EMPTY') | ||
82 | else: | 86 | else: | ||
83 | output_cart() | 87 | output_cart() | ||
84 | def print_descriptions(self): | 88 | def print_descriptions(self): | ||
85 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 89 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
86 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | 90 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | ||
> | e),end='\n') | > | e),end='\n') | ||
87 | print('\nItem Descriptions', end='\n') | 91 | print('\nItem Descriptions', end='\n') | ||
88 | for item in self.cart_items: | 92 | for item in self.cart_items: | ||
89 | print('{}: {}'.format(item.item_name, item.item_description), end='\n') | 93 | print('{}: {}'.format(item.item_name, item.item_description), end='\n') | ||
90 | def output_cart(self): | 94 | def output_cart(self): | ||
91 | new=ShoppingCart() | 95 | new=ShoppingCart() | ||
92 | print('OUTPUT SHOPPING CART', end='\n') | 96 | print('OUTPUT SHOPPING CART', end='\n') | ||
93 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | 97 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | ||
> | e),end='\n') | > | e),end='\n') | ||
94 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 98 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
95 | self.total_cost = self.get_cost_of_cart() | 99 | self.total_cost = self.get_cost_of_cart() | ||
96 | if (self.total_cost == 0): | 100 | if (self.total_cost == 0): | ||
97 | print('SHOPPING CART IS EMPTY') | 101 | print('SHOPPING CART IS EMPTY') | ||
98 | else: | 102 | else: | ||
99 | pass | 103 | pass | ||
100 | tc = 0 | 104 | tc = 0 | ||
101 | for item in self.cart_items: | 105 | for item in self.cart_items: | ||
102 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
103 | item.item_price, (item.item_quantity * | 107 | item.item_price, (item.item_quantity * | ||
> | item.item_price)), end='\n') | > | item.item_price)), end='\n') | ||
104 | tc += (item.item_quantity * item.item_price) | 108 | tc += (item.item_quantity * item.item_price) | ||
105 | print('\nTotal: ${}'.format(tc), end='\n') | 109 | print('\nTotal: ${}'.format(tc), end='\n') | ||
106 | def print_menu(new_cart): | 110 | def print_menu(new_cart): | ||
107 | customer_Cart = newCart | 111 | customer_Cart = newCart | ||
108 | string='' | 112 | string='' | ||
n | 109 | menu = ('\nMENU\n' | n | 113 | menu = ('\nMENU\n') |
110 | 'a - Add item to cart\n' | 114 | 'a - Add item to cart\n' | ||
111 | 'r - Remove item from cart\n' | 115 | 'r - Remove item from cart\n' | ||
112 | 'c - Change item quantity\n' | 116 | 'c - Change item quantity\n' | ||
113 | 'i - Output items\' descriptions\n' | 117 | 'i - Output items\' descriptions\n' | ||
114 | 'o - Output shopping cart\n' | 118 | 'o - Output shopping cart\n' | ||
115 | 'q - Quit\n') | 119 | 'q - Quit\n') | ||
116 | command = '' | 120 | command = '' | ||
117 | while(command != 'q'): | 121 | while(command != 'q'): | ||
118 | string='' | 122 | string='' | ||
119 | print('\nMENU\n' | 123 | print('\nMENU\n' | ||
120 | 'a - Add item to cart\n' | 124 | 'a - Add item to cart\n' | ||
121 | 'r - Remove item from cart\n' | 125 | 'r - Remove item from cart\n' | ||
122 | 'c - Change item quantity\n' | 126 | 'c - Change item quantity\n' | ||
123 | 'i - Output items\' descriptions\n' | 127 | 'i - Output items\' descriptions\n' | ||
124 | 'o - Output shopping cart\n' | 128 | 'o - Output shopping cart\n' | ||
125 | 'q - Quit\n', end='\n') | 129 | 'q - Quit\n', end='\n') | ||
126 | command = input('Choose an option:') | 130 | command = input('Choose an option:') | ||
127 | print() | 131 | print() | ||
128 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 132 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' and command != 'c' and command != 'q'): | > | = 'r' and command != 'c' and command != 'q'): | ||
129 | command = input('Choose an option:\n') | 133 | command = input('Choose an option:\n') | ||
130 | if(command == 'a'): | 134 | if(command == 'a'): | ||
131 | customer_Cart.add_item() | 135 | customer_Cart.add_item() | ||
132 | if(command == 'o'): | 136 | if(command == 'o'): | ||
133 | customer_Cart.output_cart() | 137 | customer_Cart.output_cart() | ||
134 | if(command == 'i'): | 138 | if(command == 'i'): | ||
135 | customer_Cart.print_descriptions() | 139 | customer_Cart.print_descriptions() | ||
136 | if(command == 'r'): | 140 | if(command == 'r'): | ||
137 | customer_Cart.remove_item() | 141 | customer_Cart.remove_item() | ||
138 | if(command == 'c'): | 142 | if(command == 'c'): | ||
139 | customer_Cart.modify_item() | 143 | customer_Cart.modify_item() | ||
140 | if __name__ == "__main__": | 144 | if __name__ == "__main__": | ||
141 | customer_name = str(input('Enter customer\'s name:')) | 145 | customer_name = str(input('Enter customer\'s name:')) | ||
142 | current_date = str(input('\nEnter today\'s date:')) | 146 | current_date = str(input('\nEnter today\'s date:')) | ||
143 | print() | 147 | print() | ||
144 | print() | 148 | print() | ||
145 | print('Customer name:', customer_name, end='\n') | 149 | print('Customer name:', customer_name, end='\n') | ||
146 | print('Today\'s date:', current_date, end='\n') | 150 | print('Today\'s date:', current_date, end='\n') | ||
147 | newCart = ShoppingCart(customer_name, current_date) | 151 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 148 | print_menu(newCart) | n | 152 | print_menu(newCart) |
153 | import math | ||||
149 | class pt3d: | 154 | class pt3d: | ||
n | 150 | def __init__(self, x, y, z): | n | 155 | def __init__(self,x=0,y=0,z=0): |
151 | self.x = x | 156 | self.x=x | ||
152 | self.y = y | 157 | self.y=y | ||
153 | self.z = z | 158 | self.z=z | ||
154 | def __add__(self,other): | 159 | def __add__ (self,other): | ||
155 | return pt3d(self.x + other.x, selfy + other.y, self.z + other.z) | 160 | return pt3d((self.x+other.x),(self.y+other.y),(self.z+other.z)) | ||
156 | def __sub__(self, other): | 161 | def __sub__ (self,other): | ||
157 | return sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other.z)**2) | 162 | return math.sqrt(((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z-s | ||
> | elf.z)**2)) | ||||
158 | def __eq__(self, other): | 163 | def __eq__(self,other): | ||
159 | return self.x==other.x & self.y==other.y & self.z==other.z | 164 | if (self.x == other.x) and (self.y == other.y) and (self.z == other.z): | ||
165 | return True | ||||
166 | else: | ||||
167 | return False | ||||
160 | def __str__(self): | 168 | def __str__(self): | ||
t | 161 | return '<{}, {}, {}>'.format(self.x,self.y,self.z) | t | 169 | return "<{},{},{}>".format(self.x,self.y,self.z) |
162 | p1 = pt3d(1,1,1) | 170 | if __name__=="__main__": | ||
163 | p2 = pt3d(2,2,2) | 171 | p2 = pt3d() | ||
164 | print(p1 + p2) | 172 | print(pt3d()+p2) | ||
165 | print(p1-p2) | 173 | print(pt3d()-p2) | ||
166 | print(p1==p2) | ||||
167 | print(p1+p1 == p2) | ||||
168 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | ||
2 | class Circle: | 1 | class Circle(): | ||
3 | def __init__(self,radius): | 2 | def __init__(self, r): | ||
4 | self.radius=radius | 3 | self.radius = r | ||
5 | def area(self): | 4 | def area(self): | ||
n | 6 | area=math.pi*self.radius**2 | n | 5 | return self.radius**2*3.14 |
7 | return area | ||||
8 | def perimeter(self): | 6 | def perimeter(self): | ||
n | 9 | perimeter=2*math.pi*self.radius | n | 7 | return 2*self.radius*3.14 |
10 | return perimeter | ||||
11 | if __name__=='__main__': | ||||
12 | x = int(input()) | ||||
13 | NewCircle = Circle(x) | 8 | NewCircle = Circle() | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 9 | print(NewCircle.area()) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 10 | print(NewCircle.perimeter()) | ||
11 | class ItemToPurchase: | ||||
16 | def __init__(self, item_name= 'none', item_price=0, item_quantity=0, item_de | 12 | def __init__(self, item_name= 'none', item_price=0, item_quantity=0, item_de | ||
> | scription = 'none'): | > | scription = 'none'): | ||
17 | self.item_name = item_name | 13 | self.item_name = item_name | ||
18 | self.item_price = item_price | 14 | self.item_price = item_price | ||
19 | self.item_quantity = item_quantity | 15 | self.item_quantity = item_quantity | ||
20 | self.item_description = item_description | 16 | self.item_description = item_description | ||
21 | def print_item_cost(self): | 17 | def print_item_cost(self): | ||
22 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 18 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, (self.item_quantity | > | self.item_price, (self.item_quantity | ||
23 | * self.item_price)) | 19 | * self.item_price)) | ||
24 | cost = self.item_quantity * self.item_price | 20 | cost = self.item_quantity * self.item_price | ||
25 | return string, cost | 21 | return string, cost | ||
26 | def print_item_description(self): | 22 | def print_item_description(self): | ||
27 | string = '{}: {}'.format(self.item_name, self.item_description) | 23 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
28 | print(string, end=' ') | 24 | print(string, end=' ') | ||
29 | return string | 25 | return string | ||
30 | class ShoppingCart: | 26 | class ShoppingCart: | ||
31 | def __init__(self,customer_name= None ,current_date='January 1,2016',cart_it | 27 | def __init__(self,customer_name= None ,current_date='January 1,2016',cart_it | ||
> | ems=[]): | > | ems=[]): | ||
32 | self.customer_name = customer_name | 28 | self.customer_name = customer_name | ||
33 | self.current_date = current_date | 29 | self.current_date = current_date | ||
34 | self.cart_items = cart_items | 30 | self.cart_items = cart_items | ||
35 | def add_item(self,): | 31 | def add_item(self,): | ||
36 | print('\nADD ITEM TO CART', end='\n') | 32 | print('\nADD ITEM TO CART', end='\n') | ||
37 | item_name = str(input('Enter the item name:')); | 33 | item_name = str(input('Enter the item name:')); | ||
38 | item_description = str(input('\nEnter the item description:')); | 34 | item_description = str(input('\nEnter the item description:')); | ||
39 | item_price = int(input('\nEnter the item price:')); | 35 | item_price = int(input('\nEnter the item price:')); | ||
40 | item_quantity = int(input('\nEnter the item quantity:\n')) | 36 | item_quantity = int(input('\nEnter the item quantity:\n')) | ||
41 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, | 37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description)) | > | item_description)) | ||
42 | def remove_item(self): | 38 | def remove_item(self): | ||
43 | print() | 39 | print() | ||
44 | print('REMOVE ITEM FROM CART', end='\n') | 40 | print('REMOVE ITEM FROM CART', end='\n') | ||
45 | string = str(input('Enter name of item to remove:\n')) | 41 | string = str(input('Enter name of item to remove:\n')) | ||
46 | i = 0 | 42 | i = 0 | ||
47 | for item in self.cart_items: | 43 | for item in self.cart_items: | ||
48 | if(item.item_name == string): | 44 | if(item.item_name == string): | ||
49 | del self.cart_items[i] | 45 | del self.cart_items[i] | ||
50 | i += 1 | 46 | i += 1 | ||
51 | flag=True | 47 | flag=True | ||
52 | break | 48 | break | ||
53 | else: | 49 | else: | ||
54 | flag=False | 50 | flag=False | ||
55 | if(flag==False): | 51 | if(flag==False): | ||
56 | print('Item not found in cart. Nothing removed.') | 52 | print('Item not found in cart. Nothing removed.') | ||
57 | def modify_item(self): | 53 | def modify_item(self): | ||
58 | print('\nCHANGE ITEM QUANTITY', end='\n') | 54 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
59 | name = str(input('Enter the item name:')) | 55 | name = str(input('Enter the item name:')) | ||
60 | for item in self.cart_items: | 56 | for item in self.cart_items: | ||
61 | if(item.item_name == name): | 57 | if(item.item_name == name): | ||
62 | quantity = int(input('Enter the new quantity:')) | 58 | quantity = int(input('Enter the new quantity:')) | ||
63 | item.item_quantity = quantity | 59 | item.item_quantity = quantity | ||
64 | flag=True | 60 | flag=True | ||
65 | break | 61 | break | ||
66 | else: | 62 | else: | ||
67 | flag=False | 63 | flag=False | ||
68 | if(flag==False): | 64 | if(flag==False): | ||
69 | print('Item not found in cart. Nothing modified.') | 65 | print('Item not found in cart. Nothing modified.') | ||
70 | def get_num_items_in_cart(self): | 66 | def get_num_items_in_cart(self): | ||
71 | num_items = 0 | 67 | num_items = 0 | ||
72 | for item in self.cart_items: | 68 | for item in self.cart_items: | ||
73 | num_items += item.item_quantity | 69 | num_items += item.item_quantity | ||
74 | return num_items | 70 | return num_items | ||
75 | def get_cost_of_cart(self): | 71 | def get_cost_of_cart(self): | ||
76 | total_cost = 0 | 72 | total_cost = 0 | ||
77 | cost = 0 | 73 | cost = 0 | ||
78 | for item in self.cart_items: | 74 | for item in self.cart_items: | ||
79 | cost = (item.item_quantity * item.item_price) | 75 | cost = (item.item_quantity * item.item_price) | ||
80 | total_cost += cost | 76 | total_cost += cost | ||
81 | return total_cost | 77 | return total_cost | ||
82 | def print_total(): | 78 | def print_total(): | ||
83 | total_cost = self.get_cost_of_cart() | 79 | total_cost = self.get_cost_of_cart() | ||
84 | if (total_cost == 0): | 80 | if (total_cost == 0): | ||
85 | print('SHOPPING CART IS EMPTY') | 81 | print('SHOPPING CART IS EMPTY') | ||
86 | else: | 82 | else: | ||
87 | output_cart() | 83 | output_cart() | ||
88 | def print_descriptions(self): | 84 | def print_descriptions(self): | ||
89 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 85 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
90 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | 86 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | ||
> | e),end='\n') | > | e),end='\n') | ||
91 | print('\nItem Descriptions', end='\n') | 87 | print('\nItem Descriptions', end='\n') | ||
92 | for item in self.cart_items: | 88 | for item in self.cart_items: | ||
93 | print('{}: {}'.format(item.item_name, item.item_description), end='\n') | 89 | print('{}: {}'.format(item.item_name, item.item_description), end='\n') | ||
94 | def output_cart(self): | 90 | def output_cart(self): | ||
95 | new=ShoppingCart() | 91 | new=ShoppingCart() | ||
96 | print('OUTPUT SHOPPING CART', end='\n') | 92 | print('OUTPUT SHOPPING CART', end='\n') | ||
97 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | 93 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_dat | ||
> | e),end='\n') | > | e),end='\n') | ||
98 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 94 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
99 | self.total_cost = self.get_cost_of_cart() | 95 | self.total_cost = self.get_cost_of_cart() | ||
100 | if (self.total_cost == 0): | 96 | if (self.total_cost == 0): | ||
101 | print('SHOPPING CART IS EMPTY') | 97 | print('SHOPPING CART IS EMPTY') | ||
102 | else: | 98 | else: | ||
103 | pass | 99 | pass | ||
104 | tc = 0 | 100 | tc = 0 | ||
105 | for item in self.cart_items: | 101 | for item in self.cart_items: | ||
106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 102 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
107 | item.item_price, (item.item_quantity * | 103 | item.item_price, (item.item_quantity * | ||
> | item.item_price)), end='\n') | > | item.item_price)), end='\n') | ||
108 | tc += (item.item_quantity * item.item_price) | 104 | tc += (item.item_quantity * item.item_price) | ||
109 | print('\nTotal: ${}'.format(tc), end='\n') | 105 | print('\nTotal: ${}'.format(tc), end='\n') | ||
110 | def print_menu(new_cart): | 106 | def print_menu(new_cart): | ||
111 | customer_Cart = newCart | 107 | customer_Cart = newCart | ||
112 | string='' | 108 | string='' | ||
n | 113 | menu = ('\nMENU\n') | n | 109 | menu = ('\nMENU\n' |
114 | 'a - Add item to cart\n' | 110 | 'a - Add item to cart\n' | ||
115 | 'r - Remove item from cart\n' | 111 | 'r - Remove item from cart\n' | ||
116 | 'c - Change item quantity\n' | 112 | 'c - Change item quantity\n' | ||
117 | 'i - Output items\' descriptions\n' | 113 | 'i - Output items\' descriptions\n' | ||
118 | 'o - Output shopping cart\n' | 114 | 'o - Output shopping cart\n' | ||
119 | 'q - Quit\n') | 115 | 'q - Quit\n') | ||
120 | command = '' | 116 | command = '' | ||
121 | while(command != 'q'): | 117 | while(command != 'q'): | ||
122 | string='' | 118 | string='' | ||
123 | print('\nMENU\n' | 119 | print('\nMENU\n' | ||
124 | 'a - Add item to cart\n' | 120 | 'a - Add item to cart\n' | ||
125 | 'r - Remove item from cart\n' | 121 | 'r - Remove item from cart\n' | ||
126 | 'c - Change item quantity\n' | 122 | 'c - Change item quantity\n' | ||
127 | 'i - Output items\' descriptions\n' | 123 | 'i - Output items\' descriptions\n' | ||
128 | 'o - Output shopping cart\n' | 124 | 'o - Output shopping cart\n' | ||
129 | 'q - Quit\n', end='\n') | 125 | 'q - Quit\n', end='\n') | ||
130 | command = input('Choose an option:') | 126 | command = input('Choose an option:') | ||
131 | print() | 127 | print() | ||
132 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 128 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' and command != 'c' and command != 'q'): | > | = 'r' and command != 'c' and command != 'q'): | ||
133 | command = input('Choose an option:\n') | 129 | command = input('Choose an option:\n') | ||
134 | if(command == 'a'): | 130 | if(command == 'a'): | ||
135 | customer_Cart.add_item() | 131 | customer_Cart.add_item() | ||
136 | if(command == 'o'): | 132 | if(command == 'o'): | ||
137 | customer_Cart.output_cart() | 133 | customer_Cart.output_cart() | ||
138 | if(command == 'i'): | 134 | if(command == 'i'): | ||
139 | customer_Cart.print_descriptions() | 135 | customer_Cart.print_descriptions() | ||
140 | if(command == 'r'): | 136 | if(command == 'r'): | ||
141 | customer_Cart.remove_item() | 137 | customer_Cart.remove_item() | ||
142 | if(command == 'c'): | 138 | if(command == 'c'): | ||
143 | customer_Cart.modify_item() | 139 | customer_Cart.modify_item() | ||
144 | if __name__ == "__main__": | 140 | if __name__ == "__main__": | ||
145 | customer_name = str(input('Enter customer\'s name:')) | 141 | customer_name = str(input('Enter customer\'s name:')) | ||
146 | current_date = str(input('\nEnter today\'s date:')) | 142 | current_date = str(input('\nEnter today\'s date:')) | ||
147 | print() | 143 | print() | ||
148 | print() | 144 | print() | ||
149 | print('Customer name:', customer_name, end='\n') | 145 | print('Customer name:', customer_name, end='\n') | ||
150 | print('Today\'s date:', current_date, end='\n') | 146 | print('Today\'s date:', current_date, end='\n') | ||
151 | newCart = ShoppingCart(customer_name, current_date) | 147 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 152 | print_menu(newCart) | n | 148 | print_menu(newCart) |
153 | import math | ||||
154 | class pt3d: | 149 | class pt3d: | ||
n | 155 | def __init__(self,x=0,y=0,z=0): | n | 150 | def __init__(self, x, y, z): |
156 | self.x=x | 151 | self.x = x | ||
157 | self.y=y | 152 | self.y = y | ||
158 | self.z=z | 153 | self.z = z | ||
159 | def __add__ (self,other): | 154 | def __add__(self,other): | ||
160 | return pt3d((self.x+other.x),(self.y+other.y),(self.z+other.z)) | 155 | return pt3d(self.x + other.x, selfy + other.y, self.z + other.z) | ||
161 | def __sub__ (self,other): | 156 | def __sub__(self, other): | ||
162 | return math.sqrt(((other.x-self.x)**2)+((other.y-self.y)**2)+((other.z-s | 157 | return sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other.z)**2) | ||
> | elf.z)**2)) | ||||
163 | def __eq__(self,other): | 158 | def __eq__(self, other): | ||
164 | if (self.x == other.x) and (self.y == other.y) and (self.z == other.z): | 159 | return self.x==other.x & self.y==other.y & self.z==other.z | ||
165 | return True | ||||
166 | else: | ||||
167 | return False | ||||
168 | def __str__(self): | 160 | def __str__(self): | ||
t | 169 | return "<{},{},{}>".format(self.x,self.y,self.z) | t | 161 | return '<{}, {}, {}>'.format(self.x,self.y,self.z) |
170 | if __name__=="__main__": | 162 | p1 = pt3d(1,1,1) | ||
171 | p2 = pt3d() | 163 | p2 = pt3d(2,2,2) | ||
172 | print(pt3d()+p2) | 164 | print(p1 + p2) | ||
173 | print(pt3d()-p2) | 165 | print(p1-p2) | ||
166 | print(p1==p2) | ||||
167 | print(p1+p1 == p2) | ||||
168 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self, radius): | 3 | def __init__(self, radius): | ||
n | 4 | self.radius=radius | n | 4 | self.radius = radius |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*(self.radius**2) | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 8 | return 2 * math.pi * self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription='none'): | > | cription='none'): | ||
15 | self.item_name = item_name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
18 | self.item_description = item_description | 18 | self.item_description = item_description | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
21 | (self.item_quantity * self.item_pric | 21 | (self.item_quantity * self.item_pric | ||
> | e)) | > | e)) | ||
22 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 23 | return string, cost | ||
24 | def print_item_description(self): | 24 | def print_item_description(self): | ||
25 | string = '{}: {}'.format(self.item_name, self.item_description) | 25 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
26 | print(string, end='\n') | 26 | print(string, end='\n') | ||
27 | return string | 27 | return string | ||
28 | class ShoppingCart: | 28 | class ShoppingCart: | ||
29 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | 29 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | ||
> | t_items=[]): | > | t_items=[]): | ||
30 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
31 | self.current_date = current_date | 31 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
33 | def add_item(self, string): | 33 | def add_item(self, string): | ||
n | 34 | print('ADD ITEM TO CART', end='\n') | n | 34 | print('\nADD ITEM TO CART', end='\n') |
35 | item_name = str(input('Enter the item name:')) | 35 | item_name = str(input('Enter the item name: ')) | ||
36 | item_description = str(input('\nEnter the item description:')) | 36 | item_description = str(input('\nEnter the item description: ')) | ||
37 | item_price = int(input('\nEnter the item price:')) | 37 | item_price = int(input('\nEnter the item price: ')) | ||
38 | item_quantity = int(input('\nEnter the item quantity:\n')) | 38 | item_quantity = int(input('\nEnter the item quantity: ')) | ||
39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
40 | def remove_item(self): | 40 | def remove_item(self): | ||
41 | print('\nREMOVE ITEM FROM CART', end='\n') | 41 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
n | 42 | string = str(input('Enter name of item to remove:\n')) | n | 42 | string = str(input('Enter name of item to remove: ')) |
43 | i = 0 | ||||
43 | for item in self.cart_items: | 44 | for item in self.cart_items: | ||
44 | if (item.item_name == string): | 45 | if (item.item_name == string): | ||
n | 45 | self.cart_items.remove(item) | n | 46 | del self.cart_items[i] |
47 | i += 1 | ||||
46 | flag = True | 48 | flag = True | ||
47 | break | 49 | break | ||
48 | else: | 50 | else: | ||
49 | flag = False | 51 | flag = False | ||
50 | if (flag == False): | 52 | if (flag == False): | ||
n | 51 | print('Item not found in cart. Nothing removed.') | n | 53 | print('Item not found in cart. Nothing removed') |
52 | def modify_item(self): | 54 | def modify_item(self): | ||
53 | print('\nCHANGE ITEM QUANTITY', end='\n') | 55 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
54 | name = str(input('Enter the item name: ')) | 56 | name = str(input('Enter the item name: ')) | ||
55 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
56 | if (item.item_name == name): | 58 | if (item.item_name == name): | ||
57 | quantity = int(input('Enter the new quantity: ')) | 59 | quantity = int(input('Enter the new quantity: ')) | ||
58 | item.item_quantity = quantity | 60 | item.item_quantity = quantity | ||
59 | flag = True | 61 | flag = True | ||
60 | break | 62 | break | ||
61 | else: | 63 | else: | ||
62 | flag = False | 64 | flag = False | ||
63 | if (flag == False): | 65 | if (flag == False): | ||
n | 64 | print('Item not found in cart. Nothing modified.') | n | 66 | print('Item not found in cart. Nothing modified') |
65 | def get_num_items_in_cart(self): | 67 | def get_num_items_in_cart(self): | ||
66 | num_items = 0 | 68 | num_items = 0 | ||
67 | for item in self.cart_items: | 69 | for item in self.cart_items: | ||
68 | num_items = num_items + item.item_quantity | 70 | num_items = num_items + item.item_quantity | ||
69 | return num_items | 71 | return num_items | ||
70 | def get_cost_of_cart(self): | 72 | def get_cost_of_cart(self): | ||
71 | total_cost = 0 | 73 | total_cost = 0 | ||
72 | cost = 0 | 74 | cost = 0 | ||
73 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
74 | cost = (item.item_quantity * item.item_price) | 76 | cost = (item.item_quantity * item.item_price) | ||
75 | total_cost += cost | 77 | total_cost += cost | ||
76 | return total_cost | 78 | return total_cost | ||
77 | def print_total(self): | 79 | def print_total(self): | ||
n | 78 | total_cost = get_cost_of_cart() | n | 80 | total_cost = self.get_cost_of_cart() |
79 | if (total_cost == 0): | 81 | if (total_cost == 0): | ||
80 | print('SHOPPING CART IS EMPTY') | 82 | print('SHOPPING CART IS EMPTY') | ||
81 | else: | 83 | else: | ||
n | 82 | output_cart() | n | 84 | self.output_cart() |
83 | def print_descriptions(self): | 85 | def print_descriptions(self): | ||
84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 86 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
86 | print('\nItem Descriptions', end='\n') | 88 | print('\nItem Descriptions', end='\n') | ||
87 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 90 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
89 | def output_cart(self): | 91 | def output_cart(self): | ||
90 | new = ShoppingCart() | 92 | new = ShoppingCart() | ||
n | 91 | print('OUTPUT SHOPPING CART', end='\n') | n | 93 | print('\nOUTPUT SHOPPING CART', end='\n') |
92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 94 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 95 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
n | 94 | if new.get_num_items_in_cart() == 0: | n | ||
95 | print('SHOPPING CART IS EMPTY') | ||||
96 | tc = 0 | 96 | tc = 0 | ||
97 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
98 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 98 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
99 | item.item_price, (item.item_quantit | 99 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
100 | tc += (item.item_quantity * item.item_price) | 100 | tc += (item.item_quantity * item.item_price) | ||
101 | print('\nTotal: ${}'.format(tc), end='\n') | 101 | print('\nTotal: ${}'.format(tc), end='\n') | ||
102 | def print_menu(ShoppingCart): | 102 | def print_menu(ShoppingCart): | ||
103 | customer_Cart = newCart | 103 | customer_Cart = newCart | ||
104 | string = '' | 104 | string = '' | ||
105 | menu = ('\nMENU\n' | 105 | menu = ('\nMENU\n' | ||
106 | 'a - Add item to cart\n' | 106 | 'a - Add item to cart\n' | ||
107 | 'r - Remove item from cart\n' | 107 | 'r - Remove item from cart\n' | ||
108 | 'c - Change item quantity\n' | 108 | 'c - Change item quantity\n' | ||
109 | 'i - Output items\' descriptions\n' | 109 | 'i - Output items\' descriptions\n' | ||
110 | 'o - Output shopping cart\n' | 110 | 'o - Output shopping cart\n' | ||
111 | 'q - Quit\n') | 111 | 'q - Quit\n') | ||
112 | command = '' | 112 | command = '' | ||
113 | while (command != 'q'): | 113 | while (command != 'q'): | ||
114 | string = '' | 114 | string = '' | ||
115 | print(menu, end='\n') | 115 | print(menu, end='\n') | ||
n | 116 | command = input('Choose an option:\n') | n | 116 | command = input('Choose an option: ') |
117 | while (command != 'a' and command != 'o' and command != 'i' and command | 117 | while (command != 'a' and command != 'o' and command != 'i' and command | ||
> | != 'r' | > | != 'r' | ||
118 | and command != 'c' and command != 'q'): | 118 | and command != 'c' and command != 'q'): | ||
n | 119 | command = input('Choose an option:\n') | n | 119 | command = input('Choose an option: ') |
120 | if (command == 'a'): | 120 | if (command == 'a'): | ||
121 | customer_Cart.add_item(string) | 121 | customer_Cart.add_item(string) | ||
122 | if (command == 'o'): | 122 | if (command == 'o'): | ||
123 | customer_Cart.output_cart() | 123 | customer_Cart.output_cart() | ||
124 | if (command == 'i'): | 124 | if (command == 'i'): | ||
125 | customer_Cart.print_descriptions() | 125 | customer_Cart.print_descriptions() | ||
126 | if (command == 'r'): | 126 | if (command == 'r'): | ||
127 | customer_Cart.remove_item() | 127 | customer_Cart.remove_item() | ||
128 | if (command == 'c'): | 128 | if (command == 'c'): | ||
129 | customer_Cart.modify_item() | 129 | customer_Cart.modify_item() | ||
n | 130 | if __name__=='__main__': | n | ||
131 | customer_name = str(input('Enter customer\'s name:')) | 130 | customer_name = str(input('Enter customer\'s name: ')) | ||
132 | current_date = str(input('\nEnter today\'s date:\n\n')) | 131 | current_date = str(input('\nEnter today\'s date: ')) | ||
133 | print('Customer name:', customer_name, end='\n') | 132 | print('Customer name:', customer_name, end='\n') | ||
134 | print('Today\'s date:', current_date) | 133 | print('Today\'s date:', current_date, end='\n') | ||
135 | newCart = ShoppingCart(customer_name, current_date) | 134 | newCart = ShoppingCart(customer_name, current_date) | ||
136 | print_menu(newCart)from pt3d import pt3d | 135 | print_menu(newCart)from math import sqrt | ||
137 | class pt3d: | 136 | class pt3d: | ||
138 | def __init__(self, x=0, y=0, z=0): | 137 | def __init__(self, x=0, y=0, z=0): | ||
139 | self.x = x | 138 | self.x = x | ||
140 | self.y = y | 139 | self.y = y | ||
141 | self.z = z | 140 | self.z = z | ||
n | 142 | p1 = pt3d(1, 1, 1) | n | ||
143 | p2 = pt3d(2, 2, 2) | ||||
144 | def __add__(self, other): | 141 | def __add__(self, other): | ||
t | 145 | print(p1+p2).format'<{},{},{}>' | t | 142 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) |
146 | def __str__(self, other): | 143 | def __sub__(self, other): | ||
147 | return "point : ({:d}, {:d}, {:d})".format(self.x,self.y,self.z) | ||||
148 | def distance(self, other): | ||||
149 | d = sqrt( (self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - othe | 144 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | r.z)**2 ) | > | her.z)**2) | ||
150 | return distance | 145 | def __eq__(self, other): | ||
151 | print(p2.distance(p1)) | 146 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
147 | def __str__(self): | ||||
148 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) | ||||
149 | if __name__ == '__main__': | ||||
150 | p1 = pt3d(1, 1, 1) | ||||
151 | p2 = pt3d(2, 2, 2) | ||||
152 | print(p1+p2) | ||||
153 | print(p1-p2) | ||||
154 | print(p1==p2) | ||||
155 | print(p1+p1==p2) | ||||
156 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
4 | self.radius=radius | 4 | self.radius=radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area=math.pi*(self.radius**2) | n | 6 | return math.pi*(self.radius**2) |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter=math.pi*(self.radius*2) | n | 8 | return 2*self.radius*math.pi |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
17 | self.item_name=name | 15 | self.item_name=name | ||
18 | self.item_description=description | 16 | self.item_description=description | ||
19 | self.item_price=price | 17 | self.item_price=price | ||
20 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
21 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
22 | total = self.item_price * self.item_quantity | 20 | total = self.item_price * self.item_quantity | ||
23 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.ite | 21 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.ite | ||
> | m_price, total)) | > | m_price, total)) | ||
24 | def print_item_description(self): | 22 | def print_item_description(self): | ||
25 | print('%s: %s' % (self.item_name, self.item_description)) | 23 | print('%s: %s' % (self.item_name, self.item_description)) | ||
26 | class ShoppingCart: | 24 | class ShoppingCart: | ||
27 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 25 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
28 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
29 | self.current_date = current_date | 27 | self.current_date = current_date | ||
30 | self.cart_items = cart_items | 28 | self.cart_items = cart_items | ||
31 | def add_item(self, itemToPurchase): | 29 | def add_item(self, itemToPurchase): | ||
32 | self.cart_items.append(itemToPurchase) | 30 | self.cart_items.append(itemToPurchase) | ||
33 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
34 | tremove_item = False | 32 | tremove_item = False | ||
35 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
36 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
37 | self.cart_items.remove(item) | 35 | self.cart_items.remove(item) | ||
38 | tremove_item = True | 36 | tremove_item = True | ||
39 | break | 37 | break | ||
40 | if not tremove_item: | 38 | if not tremove_item: | ||
41 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
42 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
43 | tmodify_item = False | 41 | tmodify_item = False | ||
44 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
45 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
46 | tmodify_item = True | 44 | tmodify_item = True | ||
47 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 45 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
48 | break | 46 | break | ||
49 | if not tmodify_item: | 47 | if not tmodify_item: | ||
50 | print('Item not found in cart. Nothing modified.') | 48 | print('Item not found in cart. Nothing modified.') | ||
51 | def get_num_items_in_cart(self): | 49 | def get_num_items_in_cart(self): | ||
52 | num_items = 0 | 50 | num_items = 0 | ||
53 | for item in self.cart_items: | 51 | for item in self.cart_items: | ||
54 | num_items = num_items + item.item_quantity | 52 | num_items = num_items + item.item_quantity | ||
55 | return num_items | 53 | return num_items | ||
56 | def get_cost_of_cart(self): | 54 | def get_cost_of_cart(self): | ||
57 | total_cost = 0 | 55 | total_cost = 0 | ||
58 | cost = 0 | 56 | cost = 0 | ||
59 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
60 | cost = (item.item_quantity * item.item_price) | 58 | cost = (item.item_quantity * item.item_price) | ||
61 | total_cost += cost | 59 | total_cost += cost | ||
62 | return total_cost | 60 | return total_cost | ||
63 | def print_total(self): | 61 | def print_total(self): | ||
64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_ | 62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_ | ||
> | date)) | > | date)) | ||
65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 63 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
66 | total_cost = self.get_cost_of_cart() | 64 | total_cost = self.get_cost_of_cart() | ||
67 | if (total_cost == 0): | 65 | if (total_cost == 0): | ||
68 | print('SHOPPING CART IS EMPTY') | 66 | print('SHOPPING CART IS EMPTY') | ||
69 | else: | 67 | else: | ||
70 | for item in self.cart_items: | 68 | for item in self.cart_items: | ||
71 | item.print_item_cost() | 69 | item.print_item_cost() | ||
72 | print('\nTotal: $%d' %(total_cost)) | 70 | print('\nTotal: $%d' %(total_cost)) | ||
73 | def print_descriptions(self): | 71 | def print_descriptions(self): | ||
74 | if len(self.cart_items) == 0: | 72 | if len(self.cart_items) == 0: | ||
75 | print('SHOPPING CART IS EMPTY') | 73 | print('SHOPPING CART IS EMPTY') | ||
76 | else: | 74 | else: | ||
77 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
78 | print('\nItem Descriptions') | 76 | print('\nItem Descriptions') | ||
79 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
80 | item.print_item_description() | 78 | item.print_item_description() | ||
81 | def print_menu(newCart): | 79 | def print_menu(newCart): | ||
n | 82 | customer_Cart = newCart | n | 80 | customer_Cart = newCart |
83 | menu = ('\nMENU\n' | 81 | menu = ('\nMENU\n' | ||
84 | 'a - Add item to cart\n' | 82 | 'a - Add item to cart\n' | ||
85 | 'r - Remove item from cart\n' | 83 | 'r - Remove item from cart\n' | ||
86 | 'c - Change item quantity\n' | 84 | 'c - Change item quantity\n' | ||
87 | "i - Output items' descriptions\n" | 85 | 'i - Output items\' descriptions\n' | ||
88 | 'o - Output shopping cart\n' | 86 | 'o - Output shopping cart\n' | ||
89 | 'q - Quit\n') | 87 | 'q - Quit\n') | ||
90 | command = '' | 88 | command = '' | ||
91 | while(command != 'q'): | 89 | while(command != 'q'): | ||
92 | print(menu) | 90 | print(menu) | ||
93 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
94 | while(command != 'a' and command != 'o' and command != 'i' and command != | 92 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
95 | command = input('Choose an option:\n') | 93 | command = input('Choose an option:\n') | ||
96 | if(command == 'a'): | 94 | if(command == 'a'): | ||
97 | print("\nADD ITEM TO CART") | 95 | print("\nADD ITEM TO CART") | ||
98 | item_name = input('Enter the item name:\n') | 96 | item_name = input('Enter the item name:\n') | ||
99 | item_description = input('Enter the item description:\n') | 97 | item_description = input('Enter the item description:\n') | ||
n | 100 | item_price = int(input('Enter the item price:\n')) | n | 98 | item_price = float(input('Enter the item price:\n')) |
101 | item_quantity = int(input('Enter the item quantity:\n')) | 99 | item_quantity = int(input('Enter the item quantity:\n')) | ||
102 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 100 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
103 | customer_Cart.add_item(itemtoPurchase) | 101 | customer_Cart.add_item(itemtoPurchase) | ||
104 | elif(command == 'o'): | 102 | elif(command == 'o'): | ||
105 | print('OUTPUT SHOPPING CART') | 103 | print('OUTPUT SHOPPING CART') | ||
106 | customer_Cart.print_total() | 104 | customer_Cart.print_total() | ||
107 | elif(command == 'i'): | 105 | elif(command == 'i'): | ||
108 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 106 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
109 | customer_Cart.print_descriptions() | 107 | customer_Cart.print_descriptions() | ||
110 | elif(command == 'r'): | 108 | elif(command == 'r'): | ||
111 | print('REMOVE ITEM FROM CART') | 109 | print('REMOVE ITEM FROM CART') | ||
112 | itemName = input('Enter name of item to remove:\n') | 110 | itemName = input('Enter name of item to remove:\n') | ||
113 | customer_Cart.remove_item(itemName) | 111 | customer_Cart.remove_item(itemName) | ||
114 | elif(command == 'c'): | 112 | elif(command == 'c'): | ||
115 | print('\nCHANGE ITEM QUANTITY') | 113 | print('\nCHANGE ITEM QUANTITY') | ||
116 | itemName = input('Enter the item name:\n') | 114 | itemName = input('Enter the item name:\n') | ||
n | 117 | qty = int(input('Enter the new quantity:\n')) | n | 115 | quantity = int(input('Enter the new quantity:\n')) |
118 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 116 | itemToPurchase = ItemToPurchase(itemName,0,quantity) | ||
119 | customer_Cart.modify_item(itemToPurchase) | 117 | customer_Cart.modify_item(itemToPurchase) | ||
n | n | 118 | def execute_menu(command, my_cart): | ||
119 | customer_Cart = my_cart | ||||
120 | if __name__ == "__main__": | 120 | if __name__ == "__main__": | ||
121 | customer_name = input("Enter customer's name:\n") | 121 | customer_name = input("Enter customer's name:\n") | ||
122 | current_date = input("Enter today's date:\n") | 122 | current_date = input("Enter today's date:\n") | ||
123 | print("\nCustomer name: %s" %customer_name) | 123 | print("\nCustomer name: %s" %customer_name) | ||
124 | print("Today's date: %s" %current_date) | 124 | print("Today's date: %s" %current_date) | ||
125 | newCart = ShoppingCart(customer_name, current_date) | 125 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 126 | print_menu(newCart)import math | n | 126 | print_menu(newCart)from math import sqrt |
127 | class pt3d: | 127 | class pt3d: | ||
n | 128 | def __init__(self,x=0,y=0,z=0): | n | 128 | def __init__(self, x=0, y=0, z=0): |
129 | self.x=x | 129 | self.x = x | ||
130 | self.y=y | 130 | self.y = y | ||
131 | self.z=z | 131 | self.z = z | ||
132 | def __add__(self,other): | 132 | def __add__(self, other): | ||
133 | x=self.x+other.x | 133 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
134 | y=self.y+other.y | ||||
135 | z=self.z+other.z | ||||
136 | return pt3d(x,y,z) | ||||
137 | def __sub__(self,other): | 134 | def __sub__(self, other): | ||
138 | distance=math.sqrt((((other.x-self.x)**2)+((other.y-self.y))**2+((other. | 135 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | z-self.z))**2)) | > | her.z)**2) | ||
139 | return distance | ||||
140 | def __eq__(self,other): | 136 | def __eq__(self, other): | ||
141 | if self.x==other.x and self.y==other.y and self.z==other.z: | 137 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
142 | return True | ||||
143 | else: | ||||
144 | return False | ||||
145 | def __str__(self): | 138 | def __str__(self): | ||
t | 146 | return (f'<{self.x},{self.y},{self.z}>') | t | 139 | return '<{0},{1},{2}>'.format(self.x, self.y, self.z) |
147 | if __name__=='__main__': | 140 | p1 = pt3d(1,1,1) | ||
148 | a=pt3d(1,1,1) | 141 | p2 = pt3d(2,2,2) | ||
149 | b=pt3d(2,2,2) | 142 | print(p1 + p2) | ||
150 | print(a+b) | 143 | print(p1 - p2) | ||
151 | print(a-b) | 144 | print(p1 == p2) | ||
152 | print(a==b) | 145 | print(p1+p1 == p2) | ||
153 | print(a+a==b) | ||||
154 | print(a==b+pt3d(-1,-1,-1)) | 146 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle : | n | 2 | class Circle: |
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*self.radius*self.radius | n | 6 | return math.pi * self.radius * self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 8 | return 2 * math.pi * self.radius |
9 | if __name__ == '__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description= | 14 | def __init__(self, name='none', price=0, quantity=0, description= | ||
> | 'none'): | > | 'none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | total = self.item_price * self.item_quantity | 20 | total = self.item_price * self.item_quantity | ||
21 | print('%s %d @ $%d = $%d' % (self.item_name, self. | 21 | print('%s %d @ $%d = $%d' % (self.item_name, self. | ||
> | item_quantity, self.item_price, total)) | > | item_quantity, self.item_price, total)) | ||
22 | def print_item_description(self): | 22 | def print_item_description(self): | ||
23 | print('%s: %s' % (self.item_name, self.item_descri | 23 | print('%s: %s' % (self.item_name, self.item_descri | ||
> | ption)) | > | ption)) | ||
24 | class ShoppingCart: | 24 | class ShoppingCart: | ||
25 | def __init__(self, customer_name = 'none', current_date = 'Januar | 25 | def __init__(self, customer_name = 'none', current_date = 'Januar | ||
> | y 1, 2016', cart_items = []): | > | y 1, 2016', cart_items = []): | ||
26 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 27 | self.current_date = current_date | ||
28 | self.cart_items = cart_items | 28 | self.cart_items = cart_items | ||
29 | def add_item(self, itemToPurchase): | 29 | def add_item(self, itemToPurchase): | ||
n | 30 | self.cart_items.append(itemToPurchase) | n | 30 | self.cart_items.append(itemToPurchase) |
> | |||||
31 | def get_cost_of_cart(self): | ||||
32 | total_cost = 0 | ||||
33 | cost = 0 | ||||
34 | for item in self.cart_items: | ||||
35 | cost = (item.item_quantity * item.i | ||||
> | tem_price) | ||||
36 | total_cost += cost | ||||
37 | return total_cost | ||||
38 | def print_total(): | ||||
39 | total_cost = self.get_cost_of_cart() | ||||
40 | if (total_cost == 0): | ||||
41 | print('SHOPPING CART IS EMPTY') | ||||
42 | else: | ||||
43 | self.output_cart() | ||||
> | |||||
44 | def print_descriptions(self): | ||||
45 | if len(self.cart_items) == 0: | ||||
46 | print('SHOPPING CART IS EMPTY') | ||||
47 | else: | ||||
48 | print('\nOUTPUT ITEMS\' DESCRIPTION | ||||
> | S') | ||||
49 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
50 | print('\nItem Descriptions') | ||||
51 | for item in self.cart_items: | ||||
52 | item.print_item_desc | ||||
> | ription() | ||||
53 | def output_cart(self): | ||||
54 | print('\nOUTPUT SHOPPING CART') | ||||
55 | print('{}\'s Shopping Cart - {}'.format(self.custo | ||||
> | mer_name, self.current_date)) | ||||
56 | print('Number of Items:'+ str(self.get_num_items_i | ||||
> | n_cart())+'\n') | ||||
57 | if len(self.cart_items) == 0: | ||||
58 | print('SHOPPING CART IS EMPTY') | ||||
59 | else: | ||||
60 | tc = 0 | ||||
61 | for item in self.cart_items: | ||||
62 | print('{} {} @ ${} = | ||||
> | ${}'.format(item.item_name, item.item_quantity, item.item_price, (item.item_qua | ||||
> | ntity * item.item_price))) | ||||
63 | tc += (item.item_qua | ||||
> | ntity * item.item_price) | ||||
64 | print('\nTotal: ${}'.format(tc)) | ||||
31 | def remove_item(self, itemName): | 65 | def remove_item(self, itemName): | ||
32 | tremove_item = False | 66 | tremove_item = False | ||
33 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 68 | if item.item_name == itemName: | ||
35 | self.cart_items.remo | 69 | self.cart_items.remo | ||
> | ve(item) | > | ve(item) | ||
36 | tremove_item = True | 70 | tremove_item = True | ||
37 | break | 71 | break | ||
38 | if not tremove_item: | 72 | if not tremove_item: | ||
> | > | ||||
39 | print('Item not found in the cart. | 73 | print('Item not found in the cart. | ||
> | Nothing removed') | > | Nothing removed') | ||
40 | def modify_item(self, itemToPurchase): | 74 | def modify_item(self, itemToPurchase): | ||
41 | tmodify_item = False | 75 | tmodify_item = False | ||
42 | for i in range(len(self.cart_items)): | 76 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == | 77 | if self.cart_items[i].item_name == | ||
> | itemToPurchase.item_name: | > | itemToPurchase.item_name: | ||
44 | tmodify_item = True | 78 | tmodify_item = True | ||
45 | if(itemToPurchase.it | 79 | if(itemToPurchase.it | ||
> | em_price == 0 and itemToPurchase.item_quantity == 0 and itemToPurchase.item_desc | > | em_price == 0 and itemToPurchase.item_quantity == 0 and itemToPurchase.item_desc | ||
> | ription == 'none'): | > | ription == 'none'): | ||
46 | break | 80 | break | ||
47 | else: | 81 | else: | ||
48 | if(it | 82 | if(it | ||
> | emToPurchase.item_price != 0): | > | emToPurchase.item_price != 0): | ||
49 | 83 | ||||
> | self.cart_items[i].item_price = itemToPurchase.item_price | > | self.cart_items[i].item_price = itemToPurchase.item_price | ||
50 | if(it | 84 | if(it | ||
> | emToPurchase.item_quantity != 0): | > | emToPurchase.item_quantity != 0): | ||
51 | 85 | ||||
> | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | > | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
52 | if(it | 86 | if(it | ||
> | emToPurchase.item_description != 'none'): | > | emToPurchase.item_description != 'none'): | ||
53 | 87 | ||||
> | self.cart_items[i].item_description = itemToPurchase.item_description | > | self.cart_items[i].item_description = itemToPurchase.item_description | ||
54 | break | 88 | break | ||
55 | if not tmodify_item: | 89 | if not tmodify_item: | ||
56 | print('Item not found in the cart. | 90 | print('Item not found in the cart. | ||
> | Nothing modified') | > | Nothing modified') | ||
57 | def get_num_items_in_cart(self): | 91 | def get_num_items_in_cart(self): | ||
58 | num_items = 0 | 92 | num_items = 0 | ||
59 | for item in self.cart_items: | 93 | for item in self.cart_items: | ||
60 | num_items = num_items + item.item_q | 94 | num_items = num_items + item.item_q | ||
> | uantity | > | uantity | ||
61 | return num_items | 95 | return num_items | ||
n | 62 | def get_cost_of_cart(self): | n | ||
63 | total_cost = 0 | ||||
64 | cost = 0 | ||||
65 | for item in self.cart_items: | ||||
66 | cost = (item.item_quantity * item.i | ||||
> | tem_price) | ||||
67 | total_cost += cost | ||||
68 | return total_cost | ||||
69 | def print_total(self): | ||||
70 | total_cost = self.get_cost_of_cart() | ||||
71 | if (total_cost == 0): | ||||
72 | print('SHOPPING CART IS EMPTY') | ||||
73 | else: | ||||
74 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
75 | print('Number of Items: %d\n' %self | ||||
> | .get_num_items_in_cart()) | ||||
76 | for item in self.cart_items: | ||||
77 | item.print_item_cost | ||||
> | () | ||||
78 | print('\nTotal: $%d' %(total_cost)) | ||||
79 | def print_descriptions(self): | ||||
80 | if len(self.cart_items) == 0: | ||||
81 | print('SHOPPING CART IS EMPTY') | ||||
82 | else: | ||||
83 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
84 | print('\nItem Descriptions') | ||||
85 | for item in self.cart_items: | ||||
86 | item.print_item_desc | ||||
> | ription() | ||||
87 | def print_menu(newCart): | 96 | def print_menu(newCart): | ||
88 | customer_Cart = newCart | 97 | customer_Cart = newCart | ||
89 | menu = ('\nMENU\n' | 98 | menu = ('\nMENU\n' | ||
90 | 'a - Add item to cart\n' | 99 | 'a - Add item to cart\n' | ||
n | 91 | 'r - Remove item from cart\n' | n | 100 | 'r - Remove item from the cart\n' |
92 | 'c - Change item quantity\n' | 101 | 'c - Change item quantity\n' | ||
n | 93 | "i - Output items' descriptions\n" | n | 102 | "i - Output item's descriptions\n" |
94 | 'o - Output shopping cart\n' | 103 | 'o - Output shopping cart\n' | ||
95 | 'q - Quit\n') | 104 | 'q - Quit\n') | ||
96 | command = '' | 105 | command = '' | ||
97 | while(command != 'q'): | 106 | while(command != 'q'): | ||
n | n | 107 | string='' | ||
98 | print(menu) | 108 | print(menu) | ||
n | 99 | command = input('Choose an option:\n') | n | 109 | command = raw_input('Choose an option: ') |
100 | while(command != 'a' and command != 'o' and comman | 110 | while(command != 'a' and command != 'o' and comman | ||
> | d != 'i' and command != 'q' and command != 'r' and command != 'c'): | > | d != 'i' and command != 'q' and command != 'r' and command != 'c'): | ||
n | 101 | command = input('Choose an option:\ | n | 111 | command = raw_input('Choose an opti |
> | n') | > | on: ') | ||
102 | if(command == 'a'): | 112 | if(command == 'a'): | ||
103 | print("\nADD ITEM TO CART") | 113 | print("\nADD ITEM TO CART") | ||
n | 104 | item_name = input('Enter the item n | n | 114 | item_name = raw_input('Enter the it |
> | ame:\n') | > | em name: ') | ||
105 | item_description = input('Enter the | 115 | item_description = raw_input('Enter | ||
> | item description:\n') | > | the item description: ') | ||
106 | item_price = int(input('Enter the i | 116 | item_price = float(raw_input('Enter | ||
> | tem price:\n')) | > | the item price: ')) | ||
107 | item_quantity = int(input('Enter th | 117 | item_quantity = int(raw_input('Ente | ||
> | e item quantity:\n')) | > | r the item quantity: ')) | ||
108 | itemtoPurchase = ItemToPurchase(ite | 118 | itemtoPurchase = ItemToPurchase(ite | ||
> | m_name, item_price, item_quantity, item_description) | > | m_name, item_price, item_quantity, item_description) | ||
109 | customer_Cart.add_item(itemtoPurcha | 119 | customer_Cart.add_item(itemtoPurcha | ||
> | se) | > | se) | ||
n | 110 | elif(command == 'o'): | n | 120 | if(command == 'o'): |
111 | print('\nOUTPUT SHOPPING CART') | ||||
112 | customer_Cart.print_total() | 121 | customer_Cart.output_cart() | ||
113 | elif(command == 'i'): | 122 | if(command == 'i'): | ||
114 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||||
115 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
116 | elif(command == 'r'): | 124 | if(command == 'r'): | ||
117 | print('REMOVE ITEM FROM CART') | 125 | print('REMOVE ITEM FROM CART') | ||
n | 118 | itemName = input('Enter the name of | n | 126 | itemName = raw_input('Enter the nam |
> | the item to remove :\n') | > | e of the item to remove : ') | ||
119 | customer_Cart.remove_item(itemName) | 127 | customer_Cart.remove_item(itemName) | ||
n | 120 | elif(command == 'c'): | n | 128 | if(command == 'c'): |
121 | print('\nCHANGE ITEM QUANTITY') | 129 | print('\nCHANGE ITEM QUANTITY') | ||
n | 122 | itemName = input('Enter the name of | n | 130 | itemName = raw_input('Enter the nam |
> | the item :\n') | > | e of the item : ') | ||
123 | qty = int(input('Enter the new quan | 131 | qty = int(raw_input('Enter the new | ||
> | tity :\n')) | > | quantity : ')) | ||
124 | itemToPurchase = ItemToPurchase(ite | 132 | itemToPurchase = ItemToPurchase(ite | ||
> | mName,0,qty) | > | mName,0,qty) | ||
n | 125 | customer_Cart.modify_item(itemToPur | n | 133 | customer_Cart.modify_item(itemToPur |
> | chase) | > | chase) | ||
126 | if __name__ == "__main__": | 134 | if __name__ == "__main__": | ||
n | 127 | customer_name = input("Enter customer's name:\n") | n | 135 | customer_name = raw_input("Enter customer's name:") |
128 | current_date = input("Enter today's date:\n") | 136 | current_date = raw_input("Enter today's date:") | ||
129 | print("\nCustomer name: %s" %customer_name) | 137 | print("\nCustomer name:"+ customer_name) | ||
130 | print("Today's date: %s" %current_date) | 138 | print("Today's date:"+ current_date) | ||
131 | newCart = ShoppingCart(customer_name, current_date) | 139 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 132 | print_menu(newCart) import math | n | 140 | print_menu(newCart)import math |
133 | class pt3d: | 141 | class pt3d: | ||
n | 134 | def __init__(self,x=0,y=0,z=0): | n | 142 | def __init__(self,x,y,z): |
135 | self.x= x | 143 | self.x = x | ||
136 | self.y= y | 144 | self.y = y | ||
137 | self.z= z | 145 | self.z = z | ||
138 | def __add__(self, other): | 146 | def __add__(self, other): | ||
139 | x = self.x + other.x | 147 | x = self.x + other.x | ||
140 | y = self.y + other.y | 148 | y = self.y + other.y | ||
141 | z = self.z + other.z | 149 | z = self.z + other.z | ||
n | 142 | return pt3d(x, y,z) | n | 150 | return pt3d(x,y,z) |
143 | def __sub__(self, other): | 151 | def __sub__(self, other): | ||
144 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 152 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
145 | def __eq__(self, other): | 153 | def __eq__(self, other): | ||
146 | return other.x==self.x and other.y==self.y and other.z==self.z | 154 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
147 | def __str__(self): | 155 | def __str__(self): | ||
t | 148 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | t | 156 | return "<"+str(self.x)+","+str(self.y)+","+str(self.z)+">" |
149 | if __name__ == '__main__': | ||||
150 | p1 = pt3d(1, 1, 1) | ||||
151 | p2 = pt3d(2, 2, 2) | ||||
152 | print(p1+p2) | ||||
153 | print(p1-p2) | ||||
154 | print(p1==p2) | ||||
155 | print(p1+p1==p2) | ||||
156 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle : |
3 | def __init__(self,radius): | 3 | def __init__(self,radius): | ||
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__ == '__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description= | 14 | def __init__(self, name='none', price=0, quantity=0, description= | ||
> | 'none'): | > | 'none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | total = self.item_price * self.item_quantity | 20 | total = self.item_price * self.item_quantity | ||
21 | print('%s %d @ $%d = $%d' % (self.item_name, self. | 21 | print('%s %d @ $%d = $%d' % (self.item_name, self. | ||
> | item_quantity, self.item_price, total)) | > | item_quantity, self.item_price, total)) | ||
22 | def print_item_description(self): | 22 | def print_item_description(self): | ||
23 | print('%s: %s' % (self.item_name, self.item_descri | 23 | print('%s: %s' % (self.item_name, self.item_descri | ||
> | ption)) | > | ption)) | ||
24 | class ShoppingCart: | 24 | class ShoppingCart: | ||
25 | def __init__(self, customer_name = 'none', current_date = 'Januar | 25 | def __init__(self, customer_name = 'none', current_date = 'Januar | ||
> | y 1, 2016', cart_items = []): | > | y 1, 2016', cart_items = []): | ||
26 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 27 | self.current_date = current_date | ||
28 | self.cart_items = cart_items | 28 | self.cart_items = cart_items | ||
29 | def add_item(self, itemToPurchase): | 29 | def add_item(self, itemToPurchase): | ||
n | 30 | self.cart_items.append(itemToPurchase) | n | 30 | self.cart_items.append(itemToPurchase) |
> | |||||
31 | def get_cost_of_cart(self): | ||||
32 | total_cost = 0 | ||||
33 | cost = 0 | ||||
34 | for item in self.cart_items: | ||||
35 | cost = (item.item_quantity * item.i | ||||
> | tem_price) | ||||
36 | total_cost += cost | ||||
37 | return total_cost | ||||
38 | def print_total(): | ||||
39 | total_cost = self.get_cost_of_cart() | ||||
40 | if (total_cost == 0): | ||||
41 | print('SHOPPING CART IS EMPTY') | ||||
42 | else: | ||||
43 | self.output_cart() | ||||
> | |||||
44 | def print_descriptions(self): | ||||
45 | if len(self.cart_items) == 0: | ||||
46 | print('SHOPPING CART IS EMPTY') | ||||
47 | else: | ||||
48 | print('\nOUTPUT ITEMS\' DESCRIPTION | ||||
> | S') | ||||
49 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
50 | print('\nItem Descriptions') | ||||
51 | for item in self.cart_items: | ||||
52 | item.print_item_desc | ||||
> | ription() | ||||
53 | def output_cart(self): | ||||
54 | print('\nOUTPUT SHOPPING CART') | ||||
55 | print('{}\'s Shopping Cart - {}'.format(self.custo | ||||
> | mer_name, self.current_date)) | ||||
56 | print('Number of Items:'+ str(self.get_num_items_i | ||||
> | n_cart())+'\n') | ||||
57 | if len(self.cart_items) == 0: | ||||
58 | print('SHOPPING CART IS EMPTY') | ||||
59 | else: | ||||
60 | tc = 0 | ||||
61 | for item in self.cart_items: | ||||
62 | print('{} {} @ ${} = | ||||
> | ${}'.format(item.item_name, item.item_quantity, item.item_price, (item.item_qua | ||||
> | ntity * item.item_price))) | ||||
63 | tc += (item.item_qua | ||||
> | ntity * item.item_price) | ||||
64 | print('\nTotal: ${}'.format(tc)) | ||||
65 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
66 | tremove_item = False | 32 | tremove_item = False | ||
67 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
68 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
69 | self.cart_items.remo | 35 | self.cart_items.remo | ||
> | ve(item) | > | ve(item) | ||
70 | tremove_item = True | 36 | tremove_item = True | ||
71 | break | 37 | break | ||
72 | if not tremove_item: | 38 | if not tremove_item: | ||
> | > | ||||
73 | print('Item not found in the cart. | 39 | print('Item not found in the cart. | ||
> | Nothing removed') | > | Nothing removed') | ||
74 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
75 | tmodify_item = False | 41 | tmodify_item = False | ||
76 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
77 | if self.cart_items[i].item_name == | 43 | if self.cart_items[i].item_name == | ||
> | itemToPurchase.item_name: | > | itemToPurchase.item_name: | ||
78 | tmodify_item = True | 44 | tmodify_item = True | ||
79 | if(itemToPurchase.it | 45 | if(itemToPurchase.it | ||
> | em_price == 0 and itemToPurchase.item_quantity == 0 and itemToPurchase.item_desc | > | em_price == 0 and itemToPurchase.item_quantity == 0 and itemToPurchase.item_desc | ||
> | ription == 'none'): | > | ription == 'none'): | ||
80 | break | 46 | break | ||
81 | else: | 47 | else: | ||
82 | if(it | 48 | if(it | ||
> | emToPurchase.item_price != 0): | > | emToPurchase.item_price != 0): | ||
83 | 49 | ||||
> | self.cart_items[i].item_price = itemToPurchase.item_price | > | self.cart_items[i].item_price = itemToPurchase.item_price | ||
84 | if(it | 50 | if(it | ||
> | emToPurchase.item_quantity != 0): | > | emToPurchase.item_quantity != 0): | ||
85 | 51 | ||||
> | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | > | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
86 | if(it | 52 | if(it | ||
> | emToPurchase.item_description != 'none'): | > | emToPurchase.item_description != 'none'): | ||
87 | 53 | ||||
> | self.cart_items[i].item_description = itemToPurchase.item_description | > | self.cart_items[i].item_description = itemToPurchase.item_description | ||
88 | break | 54 | break | ||
89 | if not tmodify_item: | 55 | if not tmodify_item: | ||
90 | print('Item not found in the cart. | 56 | print('Item not found in the cart. | ||
> | Nothing modified') | > | Nothing modified') | ||
91 | def get_num_items_in_cart(self): | 57 | def get_num_items_in_cart(self): | ||
92 | num_items = 0 | 58 | num_items = 0 | ||
93 | for item in self.cart_items: | 59 | for item in self.cart_items: | ||
94 | num_items = num_items + item.item_q | 60 | num_items = num_items + item.item_q | ||
> | uantity | > | uantity | ||
95 | return num_items | 61 | return num_items | ||
n | n | 62 | def get_cost_of_cart(self): | ||
63 | total_cost = 0 | ||||
64 | cost = 0 | ||||
65 | for item in self.cart_items: | ||||
66 | cost = (item.item_quantity * item.i | ||||
> | tem_price) | ||||
67 | total_cost += cost | ||||
68 | return total_cost | ||||
69 | def print_total(self): | ||||
70 | total_cost = self.get_cost_of_cart() | ||||
71 | if (total_cost == 0): | ||||
72 | print('SHOPPING CART IS EMPTY') | ||||
73 | else: | ||||
74 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
75 | print('Number of Items: %d\n' %self | ||||
> | .get_num_items_in_cart()) | ||||
76 | for item in self.cart_items: | ||||
77 | item.print_item_cost | ||||
> | () | ||||
78 | print('\nTotal: $%d' %(total_cost)) | ||||
79 | def print_descriptions(self): | ||||
80 | if len(self.cart_items) == 0: | ||||
81 | print('SHOPPING CART IS EMPTY') | ||||
82 | else: | ||||
83 | print('{}\'s Shopping Cart - {}'.fo | ||||
> | rmat(self.customer_name, self.current_date)) | ||||
84 | print('\nItem Descriptions') | ||||
85 | for item in self.cart_items: | ||||
86 | item.print_item_desc | ||||
> | ription() | ||||
96 | def print_menu(newCart): | 87 | def print_menu(newCart): | ||
97 | customer_Cart = newCart | 88 | customer_Cart = newCart | ||
98 | menu = ('\nMENU\n' | 89 | menu = ('\nMENU\n' | ||
99 | 'a - Add item to cart\n' | 90 | 'a - Add item to cart\n' | ||
n | 100 | 'r - Remove item from the cart\n' | n | 91 | 'r - Remove item from cart\n' |
101 | 'c - Change item quantity\n' | 92 | 'c - Change item quantity\n' | ||
n | 102 | "i - Output item's descriptions\n" | n | 93 | "i - Output items' descriptions\n" |
103 | 'o - Output shopping cart\n' | 94 | 'o - Output shopping cart\n' | ||
104 | 'q - Quit\n') | 95 | 'q - Quit\n') | ||
105 | command = '' | 96 | command = '' | ||
106 | while(command != 'q'): | 97 | while(command != 'q'): | ||
n | 107 | string='' | n | ||
108 | print(menu) | 98 | print(menu) | ||
n | 109 | command = raw_input('Choose an option: ') | n | 99 | command = input('Choose an option:\n') |
110 | while(command != 'a' and command != 'o' and comman | 100 | while(command != 'a' and command != 'o' and comman | ||
> | d != 'i' and command != 'q' and command != 'r' and command != 'c'): | > | d != 'i' and command != 'q' and command != 'r' and command != 'c'): | ||
n | 111 | command = raw_input('Choose an opti | n | 101 | command = input('Choose an option:\ |
> | on: ') | > | n') | ||
112 | if(command == 'a'): | 102 | if(command == 'a'): | ||
113 | print("\nADD ITEM TO CART") | 103 | print("\nADD ITEM TO CART") | ||
n | 114 | item_name = raw_input('Enter the it | n | 104 | item_name = input('Enter the item n |
> | em name: ') | > | ame:\n') | ||
115 | item_description = raw_input('Enter | 105 | item_description = input('Enter the | ||
> | the item description: ') | > | item description:\n') | ||
116 | item_price = float(raw_input('Enter | 106 | item_price = int(input('Enter the i | ||
> | the item price: ')) | > | tem price:\n')) | ||
117 | item_quantity = int(raw_input('Ente | 107 | item_quantity = int(input('Enter th | ||
> | r the item quantity: ')) | > | e item quantity:\n')) | ||
118 | itemtoPurchase = ItemToPurchase(ite | 108 | itemtoPurchase = ItemToPurchase(ite | ||
> | m_name, item_price, item_quantity, item_description) | > | m_name, item_price, item_quantity, item_description) | ||
119 | customer_Cart.add_item(itemtoPurcha | 109 | customer_Cart.add_item(itemtoPurcha | ||
> | se) | > | se) | ||
n | 120 | if(command == 'o'): | n | 110 | elif(command == 'o'): |
111 | print('\nOUTPUT SHOPPING CART') | ||||
121 | customer_Cart.output_cart() | 112 | customer_Cart.print_total() | ||
122 | if(command == 'i'): | 113 | elif(command == 'i'): | ||
114 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||||
123 | customer_Cart.print_descriptions() | 115 | customer_Cart.print_descriptions() | ||
124 | if(command == 'r'): | 116 | elif(command == 'r'): | ||
125 | print('REMOVE ITEM FROM CART') | 117 | print('REMOVE ITEM FROM CART') | ||
n | 126 | itemName = raw_input('Enter the nam | n | 118 | itemName = input('Enter the name of |
> | e of the item to remove : ') | > | the item to remove :\n') | ||
127 | customer_Cart.remove_item(itemName) | 119 | customer_Cart.remove_item(itemName) | ||
n | 128 | if(command == 'c'): | n | 120 | elif(command == 'c'): |
129 | print('\nCHANGE ITEM QUANTITY') | 121 | print('\nCHANGE ITEM QUANTITY') | ||
n | 130 | itemName = raw_input('Enter the nam | n | 122 | itemName = input('Enter the name of |
> | e of the item : ') | > | the item :\n') | ||
131 | qty = int(raw_input('Enter the new | 123 | qty = int(input('Enter the new quan | ||
> | quantity : ')) | > | tity :\n')) | ||
132 | itemToPurchase = ItemToPurchase(ite | 124 | itemToPurchase = ItemToPurchase(ite | ||
> | mName,0,qty) | > | mName,0,qty) | ||
n | 133 | customer_Cart.modify_item(itemToPur | n | 125 | customer_Cart.modify_item(itemToPur |
> | chase) | > | chase) | ||
134 | if __name__ == "__main__": | 126 | if __name__ == "__main__": | ||
n | 135 | customer_name = raw_input("Enter customer's name:") | n | 127 | customer_name = input("Enter customer's name:\n") |
136 | current_date = raw_input("Enter today's date:") | 128 | current_date = input("Enter today's date:\n") | ||
137 | print("\nCustomer name:"+ customer_name) | 129 | print("\nCustomer name: %s" %customer_name) | ||
138 | print("Today's date:"+ current_date) | 130 | print("Today's date: %s" %current_date) | ||
139 | newCart = ShoppingCart(customer_name, current_date) | 131 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 140 | print_menu(newCart)import math | n | 132 | print_menu(newCart) import math |
141 | class pt3d: | 133 | class pt3d: | ||
n | 142 | def __init__(self,x,y,z): | n | 134 | def __init__(self,x=0,y=0,z=0): |
143 | self.x = x | 135 | self.x= x | ||
144 | self.y = y | 136 | self.y= y | ||
145 | self.z = z | 137 | self.z= z | ||
146 | def __add__(self, other): | 138 | def __add__(self, other): | ||
147 | x = self.x + other.x | 139 | x = self.x + other.x | ||
148 | y = self.y + other.y | 140 | y = self.y + other.y | ||
149 | z = self.z + other.z | 141 | z = self.z + other.z | ||
n | 150 | return pt3d(x,y,z) | n | 142 | return pt3d(x, y,z) |
151 | def __sub__(self, other): | 143 | def __sub__(self, other): | ||
152 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 144 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
153 | def __eq__(self, other): | 145 | def __eq__(self, other): | ||
154 | return other.x==self.x and other.y==self.y and other.z==self.z | 146 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
155 | def __str__(self): | 147 | def __str__(self): | ||
t | 156 | return "<"+str(self.x)+","+str(self.y)+","+str(self.z)+">" | t | 148 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) |
149 | if __name__ == '__main__': | ||||
150 | p1 = pt3d(1, 1, 1) | ||||
151 | p2 = pt3d(2, 2, 2) | ||||
152 | print(p1+p2) | ||||
153 | print(p1-p2) | ||||
154 | print(p1==p2) | ||||
155 | print(p1+p1==p2) | ||||
156 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, r): |
4 | self.radius = radius | 4 | self.radius = r | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (math.pi*(self.radius*self.radius)) | n | 6 | return (self.radius**2)*(math.pi) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 8 | return 2*(math.pi)*(self.radius) |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | n | 14 | def __init__(self, item_name = "none", item_price = 0, item_quantity = 0, it |
> | em_description = "none"): | ||||
15 | self.item_name = name | 15 | self.item_name = item_name | ||
16 | self.item_price = item_price | ||||
17 | self.item_quantity = item_quantity | ||||
16 | self.item_description = description | 18 | self.item_description = item_description | ||
17 | self.item_price = price | ||||
18 | self.item_quantity = quantity | ||||
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
n | n | 20 | print(self.item_name + " " + str(self.item_quantity) + " @ $" + str(self | ||
> | .item_price) + " = $" + | ||||
20 | total = self.item_price * self.item_quantity | 21 | str(self.item_price * self.item_quantity)) | ||
21 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.it | ||||
> | em_price, total)) | ||||
22 | def print_item_description(self): | 22 | def print_item_description(self): | ||
n | 23 | print('%s: %s' % (self.item_name, self.item_description)) | n | 23 | print(self.item_name + ": " + str(self.item_description)) |
24 | class ShoppingCart: | 24 | class ShoppingCart: | ||
n | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | n | 25 | def __init__(self, customer_name='none', current_date='January 1, 2016'): |
> | t_items=[]): | ||||
26 | self.customer_name = customer_name | 26 | self.customer_name = customer_name | ||
27 | self.current_date = current_date | 27 | self.current_date = current_date | ||
n | 28 | self.cart_items = cart_items | n | 28 | self.cart_items = [] |
29 | def add_item(self, itemToPurchase): | 29 | def add_item(self, ItemToPurchase): | ||
30 | self.cart_items.append(itemToPurchase) | 30 | self.cart_items.append(ItemToPurchase) | ||
31 | def remove_item(self, itemName): | 31 | def remove_item(self, itemName): | ||
n | 32 | tremove_item = False | n | 32 | RemoveIt = False |
33 | for item in self.cart_items: | 33 | for item in self.cart_items: | ||
34 | if item.item_name == itemName: | 34 | if item.item_name == itemName: | ||
35 | self.cart_items.remove(item) | 35 | self.cart_items.remove(item) | ||
n | 36 | tremove_item = True | n | 36 | RemoveIt = True |
37 | break | 37 | break | ||
n | 38 | if not tremove_item: | n | 38 | if not RemoveIt: |
39 | print('Item not found in cart. Nothing removed.') | 39 | print('Item not found in cart. Nothing removed.') | ||
40 | def modify_item(self, itemToPurchase): | 40 | def modify_item(self, itemToPurchase): | ||
n | 41 | tmodify_item = False | n | 41 | ModifyIt = False |
42 | for i in range(len(self.cart_items)): | 42 | for i in range(len(self.cart_items)): | ||
43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 43 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
n | 44 | tmodify_item = True | n | 44 | ModifyIt = True |
45 | if ( | ||||
46 | itemToPurchase.item_price == 0 and itemToPurchase.item_q | ||||
> | uantity == 0 and itemToPurchase.item_description == 'none'): | ||||
47 | break | ||||
48 | else: | ||||
49 | if (itemToPurchase.item_price != 0): | ||||
50 | self.cart_items[i].item_price = itemToPurchase.item_pric | ||||
> | e | ||||
51 | if (itemToPurchase.item_quantity != 0): | ||||
45 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 52 | self.cart_items[i].item_quantity = itemToPurchase.item_q | ||
> | uantity | ||||
53 | if (itemToPurchase.item_description != 'none'): | ||||
54 | self.cart_items[i].item_description = itemToPurchase.ite | ||||
> | m_description | ||||
46 | break | 55 | break | ||
47 | if not tmodify_item: | 56 | if not ModifyIt: | ||
48 | print('Item not found in cart. Nothing modified.') | 57 | print('Item not found in cart. Nothing modified.') | ||
49 | def get_num_items_in_cart(self): | 58 | def get_num_items_in_cart(self): | ||
50 | num_items = 0 | 59 | num_items = 0 | ||
51 | for item in self.cart_items: | 60 | for item in self.cart_items: | ||
52 | num_items = num_items + item.item_quantity | 61 | num_items = num_items + item.item_quantity | ||
53 | return num_items | 62 | return num_items | ||
54 | def get_cost_of_cart(self): | 63 | def get_cost_of_cart(self): | ||
55 | total_cost = 0 | 64 | total_cost = 0 | ||
56 | cost = 0 | 65 | cost = 0 | ||
57 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
58 | cost = (item.item_quantity * item.item_price) | 67 | cost = (item.item_quantity * item.item_price) | ||
59 | total_cost += cost | 68 | total_cost += cost | ||
60 | return total_cost | 69 | return total_cost | ||
61 | def print_total(self): | 70 | def print_total(self): | ||
n | n | 71 | total_cost = self.get_cost_of_cart() | ||
62 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 72 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
63 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | 73 | print('Number of Items: %d\n' % self.get_num_items_in_cart()) | ||
n | 64 | total_cost = self.get_cost_of_cart() | n | 74 | for item in self.cart_items: |
75 | item.print_item_cost() | ||||
65 | if total_cost == 0: | 76 | if (total_cost == 0): | ||
66 | print('SHOPPING CART IS EMPTY') | 77 | print('SHOPPING CART IS EMPTY') | ||
n | 67 | print('\nTotal: $%d' % total_cost) | n | 78 | print('\nTotal: $%d' % (total_cost)) |
68 | else: | ||||
69 | for item in self.cart_items: | ||||
70 | item.print_item_cost() | ||||
71 | print('\nTotal: $%d' % total_cost) | ||||
72 | def print_descriptions(self): | 79 | def print_descriptions(self): | ||
73 | if len(self.cart_items) == 0: | 80 | if len(self.cart_items) == 0: | ||
74 | print('SHOPPING CART IS EMPTY') | 81 | print('SHOPPING CART IS EMPTY') | ||
75 | else: | 82 | else: | ||
76 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 83 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
77 | print('\nItem Descriptions') | 84 | print('\nItem Descriptions') | ||
78 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
79 | item.print_item_description() | 86 | item.print_item_description() | ||
80 | def print_menu(): | 87 | def print_menu(): | ||
n | 81 | print('\nMENU\n' | n | 88 | print('MENU\n' |
82 | 'a - Add item to cart\n' | 89 | 'a - Add item to cart\n' | ||
83 | 'r - Remove item from cart\n' | 90 | 'r - Remove item from cart\n' | ||
84 | 'c - Change item quantity\n' | 91 | 'c - Change item quantity\n' | ||
85 | "i - Output items' descriptions\n" | 92 | "i - Output items' descriptions\n" | ||
86 | 'o - Output shopping cart\n' | 93 | 'o - Output shopping cart\n' | ||
87 | 'q - Quit\n') | 94 | 'q - Quit\n') | ||
88 | def execute_menu(command, my_cart): | 95 | def execute_menu(command, my_cart): | ||
89 | customer_Cart = my_cart | 96 | customer_Cart = my_cart | ||
90 | if command == 'a': | 97 | if command == 'a': | ||
91 | print("\nADD ITEM TO CART") | 98 | print("\nADD ITEM TO CART") | ||
92 | item_name = input('Enter the item name:\n') | 99 | item_name = input('Enter the item name:\n') | ||
93 | item_description = input('Enter the item description:\n') | 100 | item_description = input('Enter the item description:\n') | ||
94 | item_price = int(input('Enter the item price:\n')) | 101 | item_price = int(input('Enter the item price:\n')) | ||
95 | item_quantity = int(input('Enter the item quantity:\n')) | 102 | item_quantity = int(input('Enter the item quantity:\n')) | ||
96 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | 103 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, it | ||
> | em_description) | > | em_description) | ||
97 | customer_Cart.add_item(itemtoPurchase) | 104 | customer_Cart.add_item(itemtoPurchase) | ||
98 | elif command == 'o': | 105 | elif command == 'o': | ||
99 | print('OUTPUT SHOPPING CART') | 106 | print('OUTPUT SHOPPING CART') | ||
100 | customer_Cart.print_total() | 107 | customer_Cart.print_total() | ||
101 | elif command == 'i': | 108 | elif command == 'i': | ||
102 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 109 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
103 | customer_Cart.print_descriptions() | 110 | customer_Cart.print_descriptions() | ||
104 | elif command == 'r': | 111 | elif command == 'r': | ||
105 | print('REMOVE ITEM FROM CART') | 112 | print('REMOVE ITEM FROM CART') | ||
106 | itemName = input('Enter name of item to remove:\n') | 113 | itemName = input('Enter name of item to remove:\n') | ||
107 | customer_Cart.remove_item(itemName) | 114 | customer_Cart.remove_item(itemName) | ||
108 | elif command == 'c': | 115 | elif command == 'c': | ||
109 | print('\nCHANGE ITEM QUANTITY') | 116 | print('\nCHANGE ITEM QUANTITY') | ||
110 | itemName = input('Enter the item name:\n') | 117 | itemName = input('Enter the item name:\n') | ||
111 | qty = int(input('Enter the new quantity:\n')) | 118 | qty = int(input('Enter the new quantity:\n')) | ||
112 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | 119 | itemToPurchase = ItemToPurchase(itemName, 0, qty) | ||
113 | customer_Cart.modify_item(itemToPurchase) | 120 | customer_Cart.modify_item(itemToPurchase) | ||
114 | if __name__ == "__main__": | 121 | if __name__ == "__main__": | ||
115 | customer_name = input("Enter customer's name:\n") | 122 | customer_name = input("Enter customer's name:\n") | ||
116 | current_date = input("Enter today's date:\n") | 123 | current_date = input("Enter today's date:\n") | ||
117 | print("\nCustomer name: %s" % customer_name) | 124 | print("\nCustomer name: %s" % customer_name) | ||
118 | print("Today's date: %s" % current_date) | 125 | print("Today's date: %s" % current_date) | ||
119 | newCart = ShoppingCart(customer_name, current_date) | 126 | newCart = ShoppingCart(customer_name, current_date) | ||
120 | command = '' | 127 | command = '' | ||
121 | while command != 'q': | 128 | while command != 'q': | ||
n | n | 129 | print() | ||
122 | print_menu() | 130 | print_menu() | ||
123 | command = input('Choose an option:\n') | 131 | command = input('Choose an option:\n') | ||
124 | while command != 'a' and command != 'o' and command != 'i' and command ! | 132 | while command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c': | > | = 'q' and command != 'r' and command != 'c': | ||
125 | command = input('Choose an option:\n') | 133 | command = input('Choose an option:\n') | ||
126 | execute_menu(command, newCart)import math | 134 | execute_menu(command, newCart)import math | ||
127 | class pt3d: | 135 | class pt3d: | ||
n | 128 | def __init__(self,x,y,z): | n | 136 | def __init__(self,x=0,y=0,z=0): |
129 | self.x= x | 137 | self.x= x | ||
130 | self.y= y | 138 | self.y= y | ||
131 | self.z= z | 139 | self.z= z | ||
132 | def __add__(self, other): | 140 | def __add__(self, other): | ||
t | 133 | x1 = self.x + other.x | t | 141 | x = self.x + other.x |
134 | y1 = self.y + other.y | 142 | y = self.y + other.y | ||
135 | z1 = self.z + other.z | 143 | z = self.z + other.z | ||
136 | return pt3d(x1, y1,z1) | 144 | return pt3d(x, y,z) | ||
137 | def __sub__(self, other): | 145 | def __sub__(self, other): | ||
138 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | 146 | return math.sqrt(math.pow(other.x-self.x, 2) + math.pow(other.y-self.y, | ||
> | 2)+math.pow(other.z-self.z, 2)) | > | 2)+math.pow(other.z-self.z, 2)) | ||
139 | def __eq__(self, other): | 147 | def __eq__(self, other): | ||
140 | return other.x==self.x and other.y==self.y and other.z==self.z | 148 | return other.x==self.x and other.y==self.y and other.z==self.z | ||
141 | def __str__(self): | 149 | def __str__(self): | ||
142 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | 150 | return "<{0},{1},{2}>".format(self.x, self.y, self.z) | ||
143 | if __name__ == '__main__': | 151 | if __name__ == '__main__': | ||
144 | p1 = pt3d(1, 1, 1) | 152 | p1 = pt3d(1, 1, 1) | ||
145 | p2 = pt3d(2, 2, 2) | 153 | p2 = pt3d(2, 2, 2) | ||
146 | print(p1+p2) | 154 | print(p1+p2) | ||
147 | print(p1-p2) | 155 | print(p1-p2) | ||
148 | print(p1==p2) | 156 | print(p1==p2) | ||
149 | print(p1+p1==p2) | 157 | print(p1+p1==p2) | ||
150 | print(p1==p2+pt3d(-1,-1,-1)) | 158 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self,circ): | 3 | def __init__(self,x): | ||
4 | self.circ = circ*2 | ||||
5 | self.radius = circ | 4 | self.radius = x | ||
6 | def area(self): | 5 | def area(self): | ||
n | 7 | return math.pi*self.radius*self.radius | n | 6 | return math.pi*self.radius**2 |
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | return self.circ*math.pi | n | 8 | return 2*math.pi*self.radius |
10 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
11 | x = int(input()) | 10 | x = int(input()) | ||
12 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
13 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 14 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: |
15 | def __init__(self,name='none',price=0,quantity=0,description='none'): | 14 | def __init__(self,name='none',price=0,quantity=0,description='none'): | ||
16 | self.item_name=name | 15 | self.item_name=name | ||
17 | self.item_price=price | 16 | self.item_price=price | ||
18 | self.item_quantity=quantity | 17 | self.item_quantity=quantity | ||
19 | self.item_description=description | 18 | self.item_description=description | ||
20 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
21 | totalcost = self.item_price * self.item_quantity | 20 | totalcost = self.item_price * self.item_quantity | ||
22 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 21 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price, totalcost)) | > | item_price, totalcost)) | ||
23 | def print_item_description(self): | 22 | def print_item_description(self): | ||
24 | print('{}: {}, {}'.format(self.item_name,self.item_description,self.item | 23 | print('{}: {}, {}'.format(self.item_name,self.item_description,self.item | ||
> | _quantity)) | > | _quantity)) | ||
n | 25 | class ShoppingCart: | n | 24 | class ShoppingCart: |
26 | def __init__(self,name='none',date='January 1, 2016',list=[],total_cost=0): | 25 | def __init__(self,name='none',date='January 1, 2016',list=[],total_cost=0): | ||
> | |||||
27 | self.customer_name=name | 26 | self.customer_name=name | ||
28 | self.current_date=date | 27 | self.current_date=date | ||
29 | self.cart_items=list | 28 | self.cart_items=list | ||
30 | self.total_cost = total_cost | 29 | self.total_cost = total_cost | ||
31 | def add_item(self,ItemToPurchase): | 30 | def add_item(self,ItemToPurchase): | ||
32 | self.cart_items.append(ItemToPurchase) | 31 | self.cart_items.append(ItemToPurchase) | ||
33 | def remove_item(self,TempName): | 32 | def remove_item(self,TempName): | ||
n | 34 | tempmsg = False | n | 33 | item = False |
35 | for x in self.cart_items: | 34 | for i in self.cart_items: | ||
36 | if x.item_name==TempName: | 35 | if i.item_name==TempName: | ||
37 | self.cart_items.remove(item) | 36 | self.cart_items.remove(i) | ||
38 | tempmsg=True | 37 | item = True | ||
39 | break | 38 | break | ||
40 | if not TempName: | 39 | if not TempName: | ||
41 | print('Item not found in the cart. Nothing removed') | 40 | print('Item not found in the cart. Nothing removed') | ||
n | 42 | def modify_item(self, itemToPurchase): | n | ||
43 | tmodify_item = False | ||||
44 | for i in range(len(self.cart_items)): | ||||
45 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||||
46 | tmodify_item = True | ||||
47 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||||
48 | break | ||||
49 | if not tmodify_item: | ||||
50 | print('Item not found in the cart. Nothing modified') | ||||
51 | def get_num_items_in_cart(self): | 41 | def get_num_items_in_cart(self): | ||
n | 52 | num_items = 0 | n | 42 | num_items = 0 |
53 | for item in self.cart_items: | 43 | for item in self.cart_items: | ||
54 | num_items = num_items + item.item_quantity | 44 | num_items = num_items + item.item_quantity | ||
55 | return num_items | 45 | return num_items | ||
56 | def get_cost_of_cart(self): | 46 | def get_cost_of_cart(self): | ||
n | 57 | total_cost = 0 | n | 47 | total_cost = 0 |
58 | cost = 0 | 48 | cost = 0 | ||
59 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
60 | cost = (item.item_quantity * item.item_price) | 50 | cost = (item.item_quantity * item.item_price) | ||
61 | total_cost += cost | 51 | total_cost += cost | ||
62 | return total_cost | 52 | return total_cost | ||
63 | def execute_menu(command='',newCart=0): | 53 | def execute_menu(command='',newCart=0): | ||
64 | while(command != 'a' and command != 'o' and command != 'i' and command != 'q | 54 | while(command != 'a' and command != 'o' and command != 'i' and command != 'q | ||
> | ' and command != 'r' and command != 'c'): | > | ' and command != 'r' and command != 'c'): | ||
65 | command = input('Choose an option:\n') | 55 | command = input('Choose an option:\n') | ||
66 | if(command == 'o'): | 56 | if(command == 'o'): | ||
67 | print(f"OUTPUT SHOPPING CART\n{newCart.customer_name}'s Shopping Cart - | 57 | print(f"OUTPUT SHOPPING CART\n{newCart.customer_name}'s Shopping Cart - | ||
> | {newCart.current_date}\nNumber of Items: {len(newCart.cart_items)}\n\nSHOPPING C | > | {newCart.current_date}\nNumber of Items: {len(newCart.cart_items)}\n\nSHOPPING C | ||
> | ART IS EMPTY\n\nTotal: ${newCart.total_cost}\n") | > | ART IS EMPTY\n\nTotal: ${newCart.total_cost}\n") | ||
68 | def print_menu(newCart=0): | 58 | def print_menu(newCart=0): | ||
69 | print('MENU') | 59 | print('MENU') | ||
70 | print('a - Add item to cart') | 60 | print('a - Add item to cart') | ||
71 | print('r - Remove item from cart') | 61 | print('r - Remove item from cart') | ||
72 | print('c - Change item quantity') | 62 | print('c - Change item quantity') | ||
73 | print('i - Output items\' descriptions') | 63 | print('i - Output items\' descriptions') | ||
74 | print('o - Output shopping cart') | 64 | print('o - Output shopping cart') | ||
75 | print('q - Quit') | 65 | print('q - Quit') | ||
76 | print() | 66 | print() | ||
77 | if __name__ == '__main__': | 67 | if __name__ == '__main__': | ||
78 | customer_name = input('Enter customer\'s name:\n') | 68 | customer_name = input('Enter customer\'s name:\n') | ||
79 | current_date = input('Enter today\'s date:\n') | 69 | current_date = input('Enter today\'s date:\n') | ||
80 | print() | 70 | print() | ||
81 | print('Customer name: %s' %customer_name) | 71 | print('Customer name: %s' %customer_name) | ||
82 | print('Today\'s date: %s' %current_date) | 72 | print('Today\'s date: %s' %current_date) | ||
83 | print() | 73 | print() | ||
84 | my_cart = ShoppingCart(customer_name, current_date) | 74 | my_cart = ShoppingCart(customer_name, current_date) | ||
85 | execute_menu(input('Choose an option:\n'), my_cart) | 75 | execute_menu(input('Choose an option:\n'), my_cart) | ||
86 | print_menu() | 76 | print_menu() | ||
87 | execute_menu(input('Choose an option:\n'), my_cart)from math import sqrt | 77 | execute_menu(input('Choose an option:\n'), my_cart)from math import sqrt | ||
88 | class pt3d: | 78 | class pt3d: | ||
n | 89 | def __init__(self, x=0, y=0, z=0): | n | 79 | def __init__(self, x=0, y=0, z=0): |
90 | self.x = x | 80 | self.x = x | ||
91 | self.y = y | 81 | self.y = y | ||
92 | self.z = z | 82 | self.z = z | ||
n | 93 | def __add__(self, other): | n | 83 | def __add__(self, other): |
94 | new_points = pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | 84 | new_points = pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | ||
95 | return new_points | 85 | return new_points | ||
n | 96 | def __sub__(self, other): | n | 86 | def __sub__(self, other): |
97 | new_points = sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other. | 87 | new_points = sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other. | ||
> | z)**2) | > | z)**2) | ||
98 | return new_points | 88 | return new_points | ||
t | 99 | def __eq__(self, other): | t | 89 | def __eq__(self, other): |
100 | return self.x == other.x and self.y == other.y and self.z == other.z | 90 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
101 | def __str__(self): | 91 | def __str__(self): | ||
102 | return '<{},{},{}>'.format(self.x, self.y, self.z) | 92 | return '<{},{},{}>'.format(self.x, self.y, self.z) | ||
103 | p1 = pt3d(1, 1, 1) | 93 | p1 = pt3d(1, 1, 1) | ||
104 | p2 = pt3d(2, 2, 2) | 94 | p2 = pt3d(2, 2, 2) | ||
105 | print(p1 + p2) | 95 | print(p1 + p2) | ||
106 | print(p1 - p2) | 96 | print(p1 - p2) | ||
107 | print(p1 == p2) | 97 | print(p1 == p2) | ||
108 | print(p1+p1 == p2) | 98 | print(p1+p1 == p2) | ||
109 | print(p1==p2+pt3d(-1, -1, -1)) | 99 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle(): | n | 2 | class Circle: |
3 | def __init__(self,x): | 3 | def __init__(self,circ): | ||
4 | self.circ = circ*2 | ||||
4 | self.radius = x | 5 | self.radius = circ | ||
5 | def area(self): | 6 | def area(self): | ||
n | 6 | return math.pi*self.radius**2 | n | 7 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 8 | return 2*math.pi*self.radius | n | 9 | return self.circ*math.pi |
9 | if __name__=='__main__': | 10 | if __name__=='__main__': | ||
10 | x = int(input()) | 11 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 12 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 13 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 14 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: |
14 | def __init__(self,name='none',price=0,quantity=0,description='none'): | 15 | def __init__(self,name='none',price=0,quantity=0,description='none'): | ||
15 | self.item_name=name | 16 | self.item_name=name | ||
16 | self.item_price=price | 17 | self.item_price=price | ||
17 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
18 | self.item_description=description | 19 | self.item_description=description | ||
19 | def print_item_cost(self): | 20 | def print_item_cost(self): | ||
20 | totalcost = self.item_price * self.item_quantity | 21 | totalcost = self.item_price * self.item_quantity | ||
21 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 22 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price, totalcost)) | > | item_price, totalcost)) | ||
22 | def print_item_description(self): | 23 | def print_item_description(self): | ||
23 | print('{}: {}, {}'.format(self.item_name,self.item_description,self.item | 24 | print('{}: {}, {}'.format(self.item_name,self.item_description,self.item | ||
> | _quantity)) | > | _quantity)) | ||
n | 24 | class ShoppingCart: | n | 25 | class ShoppingCart: |
25 | def __init__(self,name='none',date='January 1, 2016',list=[],total_cost=0): | 26 | def __init__(self,name='none',date='January 1, 2016',list=[],total_cost=0): | ||
> | |||||
26 | self.customer_name=name | 27 | self.customer_name=name | ||
27 | self.current_date=date | 28 | self.current_date=date | ||
28 | self.cart_items=list | 29 | self.cart_items=list | ||
29 | self.total_cost = total_cost | 30 | self.total_cost = total_cost | ||
30 | def add_item(self,ItemToPurchase): | 31 | def add_item(self,ItemToPurchase): | ||
31 | self.cart_items.append(ItemToPurchase) | 32 | self.cart_items.append(ItemToPurchase) | ||
32 | def remove_item(self,TempName): | 33 | def remove_item(self,TempName): | ||
n | 33 | item = False | n | 34 | tempmsg = False |
34 | for i in self.cart_items: | 35 | for x in self.cart_items: | ||
35 | if i.item_name==TempName: | 36 | if x.item_name==TempName: | ||
36 | self.cart_items.remove(i) | 37 | self.cart_items.remove(item) | ||
37 | item = True | 38 | tempmsg=True | ||
38 | break | 39 | break | ||
39 | if not TempName: | 40 | if not TempName: | ||
40 | print('Item not found in the cart. Nothing removed') | 41 | print('Item not found in the cart. Nothing removed') | ||
n | n | 42 | def modify_item(self, itemToPurchase): | ||
43 | tmodify_item = False | ||||
44 | for i in range(len(self.cart_items)): | ||||
45 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||||
46 | tmodify_item = True | ||||
47 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||||
48 | break | ||||
49 | if not tmodify_item: | ||||
50 | print('Item not found in the cart. Nothing modified') | ||||
41 | def get_num_items_in_cart(self): | 51 | def get_num_items_in_cart(self): | ||
n | 42 | num_items = 0 | n | 52 | num_items = 0 |
43 | for item in self.cart_items: | 53 | for item in self.cart_items: | ||
44 | num_items = num_items + item.item_quantity | 54 | num_items = num_items + item.item_quantity | ||
45 | return num_items | 55 | return num_items | ||
46 | def get_cost_of_cart(self): | 56 | def get_cost_of_cart(self): | ||
n | 47 | total_cost = 0 | n | 57 | total_cost = 0 |
48 | cost = 0 | 58 | cost = 0 | ||
49 | for item in self.cart_items: | 59 | for item in self.cart_items: | ||
50 | cost = (item.item_quantity * item.item_price) | 60 | cost = (item.item_quantity * item.item_price) | ||
51 | total_cost += cost | 61 | total_cost += cost | ||
52 | return total_cost | 62 | return total_cost | ||
53 | def execute_menu(command='',newCart=0): | 63 | def execute_menu(command='',newCart=0): | ||
54 | while(command != 'a' and command != 'o' and command != 'i' and command != 'q | 64 | while(command != 'a' and command != 'o' and command != 'i' and command != 'q | ||
> | ' and command != 'r' and command != 'c'): | > | ' and command != 'r' and command != 'c'): | ||
55 | command = input('Choose an option:\n') | 65 | command = input('Choose an option:\n') | ||
56 | if(command == 'o'): | 66 | if(command == 'o'): | ||
57 | print(f"OUTPUT SHOPPING CART\n{newCart.customer_name}'s Shopping Cart - | 67 | print(f"OUTPUT SHOPPING CART\n{newCart.customer_name}'s Shopping Cart - | ||
> | {newCart.current_date}\nNumber of Items: {len(newCart.cart_items)}\n\nSHOPPING C | > | {newCart.current_date}\nNumber of Items: {len(newCart.cart_items)}\n\nSHOPPING C | ||
> | ART IS EMPTY\n\nTotal: ${newCart.total_cost}\n") | > | ART IS EMPTY\n\nTotal: ${newCart.total_cost}\n") | ||
58 | def print_menu(newCart=0): | 68 | def print_menu(newCart=0): | ||
59 | print('MENU') | 69 | print('MENU') | ||
60 | print('a - Add item to cart') | 70 | print('a - Add item to cart') | ||
61 | print('r - Remove item from cart') | 71 | print('r - Remove item from cart') | ||
62 | print('c - Change item quantity') | 72 | print('c - Change item quantity') | ||
63 | print('i - Output items\' descriptions') | 73 | print('i - Output items\' descriptions') | ||
64 | print('o - Output shopping cart') | 74 | print('o - Output shopping cart') | ||
65 | print('q - Quit') | 75 | print('q - Quit') | ||
66 | print() | 76 | print() | ||
67 | if __name__ == '__main__': | 77 | if __name__ == '__main__': | ||
68 | customer_name = input('Enter customer\'s name:\n') | 78 | customer_name = input('Enter customer\'s name:\n') | ||
69 | current_date = input('Enter today\'s date:\n') | 79 | current_date = input('Enter today\'s date:\n') | ||
70 | print() | 80 | print() | ||
71 | print('Customer name: %s' %customer_name) | 81 | print('Customer name: %s' %customer_name) | ||
72 | print('Today\'s date: %s' %current_date) | 82 | print('Today\'s date: %s' %current_date) | ||
73 | print() | 83 | print() | ||
74 | my_cart = ShoppingCart(customer_name, current_date) | 84 | my_cart = ShoppingCart(customer_name, current_date) | ||
75 | execute_menu(input('Choose an option:\n'), my_cart) | 85 | execute_menu(input('Choose an option:\n'), my_cart) | ||
76 | print_menu() | 86 | print_menu() | ||
77 | execute_menu(input('Choose an option:\n'), my_cart)from math import sqrt | 87 | execute_menu(input('Choose an option:\n'), my_cart)from math import sqrt | ||
78 | class pt3d: | 88 | class pt3d: | ||
n | 79 | def __init__(self, x=0, y=0, z=0): | n | 89 | def __init__(self, x=0, y=0, z=0): |
80 | self.x = x | 90 | self.x = x | ||
81 | self.y = y | 91 | self.y = y | ||
82 | self.z = z | 92 | self.z = z | ||
n | 83 | def __add__(self, other): | n | 93 | def __add__(self, other): |
84 | new_points = pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | 94 | new_points = pt3d(self.x+other.x,self.y+other.y,self.z+other.z) | ||
85 | return new_points | 95 | return new_points | ||
n | 86 | def __sub__(self, other): | n | 96 | def __sub__(self, other): |
87 | new_points = sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other. | 97 | new_points = sqrt((self.x-other.x)**2+(self.y-other.y)**2+(self.z-other. | ||
> | z)**2) | > | z)**2) | ||
88 | return new_points | 98 | return new_points | ||
t | 89 | def __eq__(self, other): | t | 99 | def __eq__(self, other): |
90 | return self.x == other.x and self.y == other.y and self.z == other.z | 100 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
91 | def __str__(self): | 101 | def __str__(self): | ||
92 | return '<{},{},{}>'.format(self.x, self.y, self.z) | 102 | return '<{},{},{}>'.format(self.x, self.y, self.z) | ||
93 | p1 = pt3d(1, 1, 1) | 103 | p1 = pt3d(1, 1, 1) | ||
94 | p2 = pt3d(2, 2, 2) | 104 | p2 = pt3d(2, 2, 2) | ||
95 | print(p1 + p2) | 105 | print(p1 + p2) | ||
96 | print(p1 - p2) | 106 | print(p1 - p2) | ||
97 | print(p1 == p2) | 107 | print(p1 == p2) | ||
98 | print(p1+p1 == p2) | 108 | print(p1+p1 == p2) | ||
99 | print(p1==p2+pt3d(-1, -1, -1)) | 109 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius): | n | 3 | def __init__(self,radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius*self.radius*math.pi | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return self.radius* math.pi *2 | n | 8 | return 2 * math.pi * self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 14 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
15 | self.item_name=name | 15 | self.item_name=name | ||
16 | self.item_description=description | 16 | self.item_description=description | ||
17 | self.item_price=price | 17 | self.item_price=price | ||
18 | self.item_quantity=quantity | 18 | self.item_quantity=quantity | ||
19 | def print_item_description(self): | 19 | def print_item_description(self): | ||
20 | print('%s: %s' % (self.item_name, self.item_description)) | 20 | print('%s: %s' % (self.item_name, self.item_description)) | ||
21 | class ShoppingCart: | 21 | class ShoppingCart: | ||
22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 22 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
23 | self.customer_name = customer_name | 23 | self.customer_name = customer_name | ||
24 | self.current_date = current_date | 24 | self.current_date = current_date | ||
25 | self.cart_items = cart_items | 25 | self.cart_items = cart_items | ||
26 | def add_item(self, itemToPurchase): | 26 | def add_item(self, itemToPurchase): | ||
27 | self.cart_items.append(itemToPurchase) | 27 | self.cart_items.append(itemToPurchase) | ||
28 | def remove_item(self, itemName): | 28 | def remove_item(self, itemName): | ||
29 | tremove_item = False | 29 | tremove_item = False | ||
30 | for item in self.cart_items: | 30 | for item in self.cart_items: | ||
31 | if item.item_name == itemName: | 31 | if item.item_name == itemName: | ||
32 | self.cart_items.remove(item) | 32 | self.cart_items.remove(item) | ||
33 | tremove_item = True | 33 | tremove_item = True | ||
34 | break | 34 | break | ||
35 | if not tremove_item: | 35 | if not tremove_item: | ||
36 | print('Item not found in cart. Nothing removed.') | 36 | print('Item not found in cart. Nothing removed.') | ||
37 | def modify_item(self, itemToPurchase): | 37 | def modify_item(self, itemToPurchase): | ||
38 | tmodify_item = False | 38 | tmodify_item = False | ||
39 | for i in range(len(self.cart_items)): | 39 | for i in range(len(self.cart_items)): | ||
40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 40 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
41 | tmodify_item = True | 41 | tmodify_item = True | ||
42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | 42 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
43 | break | 43 | break | ||
44 | if not tmodify_item: | 44 | if not tmodify_item: | ||
45 | print('Item not found in cart. Nothing modified.') | 45 | print('Item not found in cart. Nothing modified.') | ||
46 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
47 | num_items = 0 | 47 | num_items = 0 | ||
48 | for item in self.cart_items: | 48 | for item in self.cart_items: | ||
49 | num_items = num_items + item.item_quantity | 49 | num_items = num_items + item.item_quantity | ||
50 | return num_items | 50 | return num_items | ||
51 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
52 | total_cost = 0 | 52 | total_cost = 0 | ||
53 | cost = 0 | 53 | cost = 0 | ||
54 | for item in self.cart_items: | 54 | for item in self.cart_items: | ||
55 | cost = (item.item_quantity * item.item_price) | 55 | cost = (item.item_quantity * item.item_price) | ||
56 | total_cost += cost | 56 | total_cost += cost | ||
57 | return total_cost | 57 | return total_cost | ||
58 | def print_total(self): | 58 | def print_total(self): | ||
59 | total_cost = self.get_cost_of_cart() | 59 | total_cost = self.get_cost_of_cart() | ||
60 | if (total_cost == 0): | 60 | if (total_cost == 0): | ||
61 | print('SHOPPING CART IS EMPTY') | 61 | print('SHOPPING CART IS EMPTY') | ||
n | 62 | print() | n | ||
63 | print('Total: $0') | ||||
64 | else: | 62 | else: | ||
65 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 63 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
66 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 64 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
67 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
68 | total = item.item_price * item.item_quantity | 66 | total = item.item_price * item.item_quantity | ||
69 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | 67 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||
> | item.item_price, total)) | > | item.item_price, total)) | ||
70 | print('\nTotal: $%d' %(total_cost)) | 68 | print('\nTotal: $%d' %(total_cost)) | ||
71 | def print_descriptions(self): | 69 | def print_descriptions(self): | ||
72 | if len(self.cart_items) == 0: | 70 | if len(self.cart_items) == 0: | ||
73 | print('SHOPPING CART IS EMPTY') | 71 | print('SHOPPING CART IS EMPTY') | ||
74 | else: | 72 | else: | ||
75 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | 73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.cur | ||
> | rent_date)) | > | rent_date)) | ||
76 | print('\nItem Descriptions') | 74 | print('\nItem Descriptions') | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
n | 78 | item.print_item_description() | n | 76 | item.print_item_description() |
79 | def output_cart(self): | ||||
80 | new=ShoppingCart() | ||||
81 | print('\nOUTPUT SHOPPING CART', end='\n') | ||||
82 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||||
> | _date), end='\n') | ||||
83 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||||
84 | tc = 0 | ||||
85 | for item in self.cart_items: | ||||
86 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||||
> | item.item_price, (item.item_quantity * item.item_price)), end='\n') | ||||
87 | tc += (item.item_quantity * item.item_price) | ||||
88 | print('\nTotal: ${}'.format(tc), end='\n') | ||||
89 | def print_menu(ShoppingCart): | 77 | def print_menu(newCart=' '): | ||
90 | cCart = newCart | 78 | customer_Cart = newCart | ||
91 | menu = ('\nMENU\n' | 79 | menu = ('\nMENU\n' | ||
92 | 'a - Add item to cart\n' | 80 | 'a - Add item to cart\n' | ||
93 | 'r - Remove item from cart\n' | 81 | 'r - Remove item from cart\n' | ||
94 | 'c - Change item quantity\n' | 82 | 'c - Change item quantity\n' | ||
95 | "i - Output items' descriptions\n" | 83 | "i - Output items' descriptions\n" | ||
96 | 'o - Output shopping cart\n' | 84 | 'o - Output shopping cart\n' | ||
97 | 'q - Quit\n') | 85 | 'q - Quit\n') | ||
98 | command = '' | 86 | command = '' | ||
99 | while(command != 'q'): | 87 | while(command != 'q'): | ||
100 | print(menu) | 88 | print(menu) | ||
101 | command = input('Choose an option:\n') | 89 | command = input('Choose an option:\n') | ||
102 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 90 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'q' and command != 'r' and command != 'c'): | > | = 'q' and command != 'r' and command != 'c'): | ||
103 | command = input('Choose an option:\n') | 91 | command = input('Choose an option:\n') | ||
104 | if(command == 'a'): | 92 | if(command == 'a'): | ||
105 | print("\nADD ITEM TO CART") | 93 | print("\nADD ITEM TO CART") | ||
106 | item_name = input('Enter the item name:\n') | 94 | item_name = input('Enter the item name:\n') | ||
107 | item_description = input('Enter the item description:\n') | 95 | item_description = input('Enter the item description:\n') | ||
108 | item_price = int(input('Enter the item price:\n')) | 96 | item_price = int(input('Enter the item price:\n')) | ||
109 | item_quantity = int(input('Enter the item quantity:\n')) | 97 | item_quantity = int(input('Enter the item quantity:\n')) | ||
110 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | 98 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity | ||
> | , item_description) | > | , item_description) | ||
n | 111 | cCart.add_item(itemtoPurchase) | n | 99 | customer_Cart.add_item(itemtoPurchase) |
112 | elif(command == 'o'): | 100 | elif(command == 'o'): | ||
113 | print('OUTPUT SHOPPING CART') | 101 | print('OUTPUT SHOPPING CART') | ||
n | 114 | cCart.print_total() | n | 102 | customer_Cart.print_total() |
115 | elif(command == 'i'): | 103 | elif(command == 'i'): | ||
116 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 104 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
n | 117 | cCart.print_descriptions() | n | 105 | customer_Cart.print_descriptions() |
118 | elif(command == 'r'): | 106 | elif(command == 'r'): | ||
119 | print('REMOVE ITEM FROM CART') | 107 | print('REMOVE ITEM FROM CART') | ||
120 | itemName = input('Enter name of item to remove:\n') | 108 | itemName = input('Enter name of item to remove:\n') | ||
n | 121 | cCart.remove_item(itemName) | n | 109 | customer_Cart.remove_item(itemName) |
122 | elif(command == 'c'): | 110 | elif(command == 'c'): | ||
123 | print('\nCHANGE ITEM QUANTITY') | 111 | print('\nCHANGE ITEM QUANTITY') | ||
124 | itemName = input('Enter the item name:\n') | 112 | itemName = input('Enter the item name:\n') | ||
125 | qty = int(input('Enter the new quantity:\n')) | 113 | qty = int(input('Enter the new quantity:\n')) | ||
126 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 114 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
n | 127 | cCart.modify_item(itemToPurchase) | n | 115 | customer_Cart.modify_item(itemToPurchase) |
128 | if __name__ == "__main__": | 116 | if __name__ == "__main__": | ||
129 | customer_name = input("Enter customer's name:\n") | 117 | customer_name = input("Enter customer's name:\n") | ||
130 | current_date = input("Enter today's date:\n") | 118 | current_date = input("Enter today's date:\n") | ||
131 | print("\nCustomer name: %s" %customer_name) | 119 | print("\nCustomer name: %s" %customer_name) | ||
132 | print("Today's date: %s" %current_date) | 120 | print("Today's date: %s" %current_date) | ||
133 | newCart = ShoppingCart(customer_name, current_date) | 121 | newCart = ShoppingCart(customer_name, current_date) | ||
n | 134 | print_menu(newCart) class pt3d: | n | 122 | print_menu(newCart) from math import sqrt |
123 | class pt3d: | ||||
135 | def __init__(self, x=0, y=0, z=0): | 124 | def __init__(self, x=0, y=0, z=0): | ||
136 | self.x = x | 125 | self.x = x | ||
137 | self.y = y | 126 | self.y = y | ||
138 | self.z = z | 127 | self.z = z | ||
139 | def __add__(self, other): | 128 | def __add__(self, other): | ||
n | 140 | x = self.x +other.x | n | 129 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) |
141 | y = self.y +other.y | ||||
142 | z = self.z +other.z | ||||
143 | return pt3d(x, y, z) | ||||
144 | def __sub__(self,other): | 130 | def __sub__(self, other): | ||
145 | return (((self.x-other.x)**2) + ((self.y-other.y)**2) + ((self.z-other.z | 131 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | )**2))**0.5 | > | her.z)**2) | ||
146 | def __eq__(self,other): | 132 | def __eq__(self, other): | ||
147 | return self.x==other.x and self.y==other.y and self.z ==other.z | 133 | return self.x == other.x and self.y == other.y and self.z == other.z | ||
148 | def __str__(self): | 134 | def __str__(self): | ||
t | 149 | return'<{0},{1},{2}>'.format(self.x, self.y, self.z) | t | 135 | return '<{},{},{}>'.format(self.x, self.y, self.z) |
150 | if __name__ == '__main__': | ||||
151 | p1 = pt3d(1, 1, 1) | 136 | p1 = pt3d(1, 1, 1) | ||
152 | p2 = pt3d(2, 2, 2) | 137 | p2 = pt3d(2, 2, 2) | ||
153 | print(p1 + p2) | 138 | print(p1 + p2) | ||
154 | print(p1 - p2) | 139 | print(p1 - p2) | ||
155 | print(p1 == p2) | 140 | print(p1 == p2) | ||
156 | print(p1+p1 == p2) | 141 | print(p1+p1 == p2) | ||
157 | print(p1==p2+pt3d(-1, -1, -1)) | 142 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self, radius): | n | 3 | def __init__(self,radius): |
4 | self.radius = radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi * self.radius * self.radius | n | 6 | return math.pi*self.radius*self.radius |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2 * math.pi * self.radius | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | n | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: |
14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription = 'none'): | > | cription = 'none'): | ||
n | 15 | self.item_name = item_name | n | 15 | self.item_name = item_name |
16 | self.item_price = item_price | 16 | self.item_price = item_price | ||
17 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
n | 18 | self.item_description = item_description | n | 18 | self.item_description = item_description |
19 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
n | 21 | (self.item_quantity * self.item_price)) | n | 21 | (self.item_quantity * self.item_price)) |
22 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
23 | return string, cost | 23 | return string, cost | ||
24 | def print_item_description(self): | 24 | def print_item_description(self): | ||
25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | 25 | string = '{}: {}, %d oz.'.format(self.item_name, self.item_description, | ||
> | self.item_quantity) | > | self.item_quantity) | ||
n | 26 | print(string) | n | 26 | print(string) |
27 | return string | 27 | return string | ||
28 | class ShoppingCart: | 28 | class ShoppingCart: | ||
29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | 29 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', | ||
> | cart_items = []): | > | cart_items = []): | ||
n | 30 | self.customer_name = customer_name | n | 30 | self.customer_name = customer_name |
31 | self.current_date = current_date | 31 | self.current_date = current_date | ||
32 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
33 | def add_item(self): | 33 | def add_item(self): | ||
34 | print('ADD ITEM TO CART') | 34 | print('ADD ITEM TO CART') | ||
35 | item_name = str(input('Enter the item name:')) | 35 | item_name = str(input('Enter the item name:')) | ||
36 | print() | ||||
37 | item_description = str(input('Enter the item description:')) | ||||
38 | print() | 36 | print() | ||
n | 39 | item_price = int(input('Enter the item price:')) | n | 37 | item_description = str(input('Enter the item description:')) |
40 | print() | 38 | print() | ||
n | n | 39 | item_price = int(input('Enter the item price:')) | ||
40 | print() | ||||
41 | item_quantity = int(input('Enter the item quantity:')) | 41 | item_quantity = int(input('Enter the item quantity:')) | ||
42 | print() | 42 | print() | ||
43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 43 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
44 | def remove_item(self): | 44 | def remove_item(self): | ||
45 | print('REMOVE ITEM FROM CART') | 45 | print('REMOVE ITEM FROM CART') | ||
46 | string = str(input('Enter name of item to remove:')) | 46 | string = str(input('Enter name of item to remove:')) | ||
47 | print() | 47 | print() | ||
48 | i = 0 | 48 | i = 0 | ||
n | 49 | for item in self.cart_items: | n | 49 | for item in self.cart_items: |
50 | if(item.item_name == string): | 50 | if(item.item_name == string): | ||
51 | del self.cart_items[i] | 51 | del self.cart_items[i] | ||
52 | flag=True | 52 | flag=True | ||
53 | break | 53 | break | ||
n | 54 | else: | n | 54 | else: |
55 | flag=False | 55 | flag=False | ||
56 | i += 1 | 56 | i += 1 | ||
57 | if(flag==False): | 57 | if(flag==False): | ||
n | 58 | print('Item not found in cart. Nothing removed.') | n | 58 | print('Item not found in cart. Nothing removed.') |
59 | def modify_item(self): | 59 | def modify_item(self): | ||
n | 60 | print('CHANGE ITEM QUANTITY') | n | 60 | print('CHANGE ITEM QUANTITY') |
61 | name = str(input('Enter the item name:')) | 61 | name = str(input('Enter the item name:')) | ||
62 | print() | 62 | print() | ||
n | 63 | for item in self.cart_items: | n | 63 | for item in self.cart_items: |
64 | if(item.item_name == name): | 64 | if(item.item_name == name): | ||
65 | quantity = int(input('Enter the new quantity:')) | 65 | quantity = int(input('Enter the new quantity:')) | ||
n | 66 | print() | n | 66 | print() |
67 | item.item_quantity = quantity | 67 | item.item_quantity = quantity | ||
68 | flag=True | 68 | flag=True | ||
n | 69 | break | n | 69 | break |
70 | else: | 70 | else: | ||
n | 71 | flag=False | n | 71 | flag=False |
72 | if(flag==False): | 72 | if(flag==False): | ||
73 | print('Item not found in cart. Nothing modified.') | 73 | print('Item not found in cart. Nothing modified.') | ||
n | 74 | print() | n | 74 | print() |
75 | def get_num_items_in_cart(self): | 75 | def get_num_items_in_cart(self): | ||
76 | num_items=0 | 76 | num_items=0 | ||
77 | for item in self.cart_items: | 77 | for item in self.cart_items: | ||
n | 78 | num_items= num_items+item.item_quantity | n | 78 | num_items= num_items+item.item_quantity |
79 | return num_items | 79 | return num_items | ||
n | 80 | def get_cost_of_cart(self): | n | 80 | def get_cost_of_cart(self): |
81 | total_cost = 0 | 81 | total_cost = 0 | ||
n | 82 | cost = 0 | n | 82 | cost = 0 |
83 | for item in self.cart_items: | 83 | for item in self.cart_items: | ||
84 | cost = (item.item_quantity * item.item_price) | 84 | cost = (item.item_quantity * item.item_price) | ||
85 | total_cost += cost | 85 | total_cost += cost | ||
86 | return total_cost | 86 | return total_cost | ||
87 | def print_total(self): | 87 | def print_total(self): | ||
88 | total_cost = self.get_cost_of_cart() | 88 | total_cost = self.get_cost_of_cart() | ||
n | 89 | if (total_cost == 0): | n | 89 | if (total_cost == 0): |
90 | print('SHOPPING CART IS EMPTY') | 90 | print('SHOPPING CART IS EMPTY') | ||
91 | else: | 91 | else: | ||
n | 92 | self.output_cart() | n | 92 | self.output_cart() |
93 | def print_descriptions(self): | 93 | def print_descriptions(self): | ||
94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | 94 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||
95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 95 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date),end='\n') | > | _date),end='\n') | ||
96 | print('\nItem Descriptions') | 96 | print('\nItem Descriptions') | ||
97 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
n | 98 | print('{}: {}'.format(item.item_name, item.item_description)) | n | 98 | print('{}: {}'.format(item.item_name, item.item_description)) |
99 | def output_cart(self): | 99 | def output_cart(self): | ||
100 | print('OUTPUT SHOPPING CART') | 100 | print('OUTPUT SHOPPING CART') | ||
101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 101 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date)) | > | _date)) | ||
102 | print('Number of Items:', self.get_num_items_in_cart()) | 102 | print('Number of Items:', self.get_num_items_in_cart()) | ||
103 | print() | 103 | print() | ||
104 | tc = 0 | 104 | tc = 0 | ||
n | 105 | for item in self.cart_items: | n | 105 | for item in self.cart_items: |
106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 106 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
n | 107 | item.item_price, (item.item_quantity * item.item_price))) | n | 107 | item.item_price, (item.item_quantity * item.item_price))) |
108 | tc += (item.item_quantity * item.item_price) | 108 | tc += (item.item_quantity * item.item_price) | ||
109 | if len(self.cart_items) == 0: | 109 | if len(self.cart_items) == 0: | ||
n | 110 | print('SHOPPING CART IS EMPTY') | n | 110 | print('SHOPPING CART IS EMPTY') |
111 | print() | 111 | print() | ||
n | 112 | print('Total: ${}'.format(tc)) | n | 112 | print('Total: ${}'.format(tc)) |
113 | def print_menu(customer_Cart): | 113 | def print_menu(customer_Cart): | ||
n | 114 | menu = ('\nMENU\n' | n | 114 | menu = ('\nMENU\n' |
115 | 'a - Add item to cart\n' | 115 | 'a - Add item to cart\n' | ||
n | 116 | 'r - Remove item from cart\n' | n | 116 | 'r - Remove item from cart\n' |
117 | 'c - Change item quantity\n' | 117 | 'c - Change item quantity\n' | ||
118 | 'i - Output items\' descriptions\n' | 118 | 'i - Output items\' descriptions\n' | ||
119 | 'o - Output shopping cart\n' | 119 | 'o - Output shopping cart\n' | ||
120 | 'q - Quit\n') | 120 | 'q - Quit\n') | ||
121 | command = '' | 121 | command = '' | ||
n | 122 | while(command != 'q'): | n | 122 | while(command != 'q'): |
123 | print(menu) | 123 | print(menu) | ||
n | 124 | command = input('Choose an option:') | n | 124 | command = input('Choose an option:') |
125 | print() | 125 | print() | ||
126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | 126 | while(command != 'a' and command != 'o' and command != 'i' and command ! | ||
> | = 'r' | > | = 'r' | ||
n | 127 | and command != 'c' and command != 'q'): | n | 127 | and command != 'c' and command != 'q'): |
128 | command = input('Choose an option:') | 128 | command = input('Choose an option:') | ||
129 | print() | 129 | print() | ||
130 | if(command == 'a'): | 130 | if(command == 'a'): | ||
131 | customer_Cart.add_item() | 131 | customer_Cart.add_item() | ||
132 | if(command == 'o'): | 132 | if(command == 'o'): | ||
133 | customer_Cart.output_cart() | 133 | customer_Cart.output_cart() | ||
134 | if(command == 'i'): | 134 | if(command == 'i'): | ||
135 | customer_Cart.print_descriptions() | 135 | customer_Cart.print_descriptions() | ||
136 | if(command == 'r'): | 136 | if(command == 'r'): | ||
137 | customer_Cart.remove_item() | 137 | customer_Cart.remove_item() | ||
138 | if(command == 'c'): | 138 | if(command == 'c'): | ||
139 | customer_Cart.modify_item() | 139 | customer_Cart.modify_item() | ||
n | 140 | def main(): | n | 140 | def main(): |
141 | customer_name = str(input('Enter customer\'s name:')) | 141 | customer_name = str(input('Enter customer\'s name:')) | ||
142 | print() | 142 | print() | ||
143 | current_date = str(input('Enter today\'s date:')) | 143 | current_date = str(input('Enter today\'s date:')) | ||
144 | print('\n') | 144 | print('\n') | ||
n | 145 | print('Customer name:', customer_name, end='\n') | n | 145 | print('Customer name:', customer_name, end='\n') |
146 | print('Today\'s date:', current_date, end='\n') | 146 | print('Today\'s date:', current_date, end='\n') | ||
147 | newCart = ShoppingCart(customer_name, current_date) | 147 | newCart = ShoppingCart(customer_name, current_date) | ||
148 | print_menu(newCart) | 148 | print_menu(newCart) | ||
149 | if __name__ == '__main__': | 149 | if __name__ == '__main__': | ||
t | 150 | main() from math import sqrt | t | 150 | main()from math import sqrt |
151 | class pt3d: | 151 | class pt3d: | ||
152 | def __init__(self, x, y, z): | 152 | def __init__(self, x=0, y=0, z=0): | ||
153 | self.x = x | 153 | self.x = x | ||
154 | self.y = y | 154 | self.y = y | ||
155 | self.z = z | 155 | self.z = z | ||
156 | def __add__(self, other): | 156 | def __add__(self, other): | ||
157 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 157 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
158 | def __sub__(self, other): | 158 | def __sub__(self, other): | ||
159 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | 159 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | > | her.z)**2) | ||
160 | def __eq__(self, other): | 160 | def __eq__(self, other): | ||
161 | return self.x == other.x and self.y == other.y and self.z == other.z | 161 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
162 | def __str__(self): | 162 | def __str__(self): | ||
163 | return '<{},{},{}>'.format(self.x,self.y,self.z) | 163 | return (f'<{self.x}, {self.y}, {self.z}>') | ||
164 | p1 = pt3d(1, 1, 1) | 164 | p1 = pt3d(1, 1, 1) | ||
165 | p2 = pt3d(2, 2, 2) | 165 | p2 = pt3d(2, 2, 2) | ||
166 | print(p1 + p2) | 166 | print(p1 + p2) | ||
167 | print(p1 - p2) | 167 | print(p1 - p2) | ||
168 | print(p1 == p2) | 168 | print(p1 == p2) | ||
169 | print(p1+p1 == p2) | 169 | print(p1+p1 == p2) | ||
170 | print(p1==p2+pt3d(-1, -1, -1)) | 170 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
3 | def __init__(self,x): | 3 | def __init__(self,x): | ||
n | 4 | self.x = x | n | 4 | self.radius = x |
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area = math.pi*(self.x**2) | n | 6 | area = math.pi * (self.radius)**2 |
7 | return area | 7 | return area | ||
8 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 9 | perimeter= math.pi*(self.x*2) | n | 9 | perimeter = 2 * math.pi * self.radius |
10 | return perimeter | 10 | return perimeter | ||
11 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
12 | x = int(input()) | 12 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 16 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | n | 16 | def __init__(self, name='none', price=0, quantity=0, description='none'): |
> | cription='none'): | ||||
17 | self.item_name = item_name | 17 | self.item_name = name | ||
18 | self.item_price = item_price | 18 | self.item_price = price | ||
19 | self.item_quantity = item_quantity | 19 | self.item_quantity = quantity | ||
20 | self.item_description = item_description | 20 | self.item_description = description | ||
21 | def print_item_cost(self): | 21 | def print_item_cost(self): | ||
n | 22 | totalCost = self.item_quantity*self.item_price | n | 22 | total = self.item_quantity*self.item_price |
23 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | 23 | print('{} {} @ ${} = ${}'. format(self.item_name, self.item_quantity,sel | ||
> | f.item_price, totalCost)) | > | f.item_price, Cost)) | ||
24 | def print_item_description(self): | 24 | def print_item_description(self): | ||
25 | print('{}: {}'.format(self.item_name,self.item_description)) | 25 | print('{}: {}'.format(self.item_name,self.item_description)) | ||
26 | class ShoppingCart: | 26 | class ShoppingCart: | ||
n | 27 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | n | 27 | def __init__(self, customer='none', current='January 1, 2016', items=[]): |
> | t_items=[]): | ||||
28 | self.customer_name = customer_name | 28 | self.customer_name = customer | ||
29 | self.current_date = current_date | 29 | self.current_date = current | ||
30 | self.cart_items = cart_items | 30 | self.cart_items = items | ||
31 | def add_item(self, string): | 31 | def add_item(self, string): | ||
32 | print('\nADD ITEM TO CART', end='\n') | 32 | print('\nADD ITEM TO CART', end='\n') | ||
33 | item_name = str(input('Enter the item name:')) | 33 | item_name = str(input('Enter the item name:')) | ||
34 | item_description = str(input('\nEnter the item description:')) | 34 | item_description = str(input('\nEnter the item description:')) | ||
35 | item_price = int(input('\nEnter the item price:')) | 35 | item_price = int(input('\nEnter the item price:')) | ||
36 | item_quantity = int(input('\nEnter the item quantity:')) | 36 | item_quantity = int(input('\nEnter the item quantity:')) | ||
37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 37 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
38 | def remove_item(self): | 38 | def remove_item(self): | ||
39 | print('\nREMOVE ITEM FROM CART', end='\n') | 39 | print('\nREMOVE ITEM FROM CART', end='\n') | ||
40 | string = str(input('Enter name of item to remove:')) | 40 | string = str(input('Enter name of item to remove:')) | ||
41 | i = 0 | 41 | i = 0 | ||
n | 42 | for Z in self.cart_items: | n | 42 | for item in self.cart_items: |
43 | if (Z.item_name == string): | 43 | if (item.item_name == string): | ||
44 | del self.cart_items[i] | 44 | del self.cart_items[i] | ||
45 | i += 1 | 45 | i += 1 | ||
n | 46 | bruv = True | n | 46 | jawn = True |
47 | break | 47 | break | ||
48 | else: | 48 | else: | ||
n | 49 | bruv = False | n | 49 | jawn = False |
50 | if (bruv == False): | 50 | if (jawn == False): | ||
51 | print('Item not found in cart. Nothing removed') | 51 | print('Item not found in cart. Nothing removed') | ||
52 | def modify_item(self): | 52 | def modify_item(self): | ||
53 | print('\nCHANGE ITEM QUANTITY', end='\n') | 53 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
54 | name = str(input('Enter the item name:')) | 54 | name = str(input('Enter the item name:')) | ||
n | 55 | for Z in self.cart_items: | n | 55 | for item in self.cart_items: |
56 | if (Z.item_name == name): | 56 | if (item.item_name == name): | ||
57 | quantity = int(input('Enter the new quantity:')) | 57 | quantity = int(input('Enter the new quantity:')) | ||
58 | item.item_quantity = quantity | 58 | item.item_quantity = quantity | ||
n | 59 | bruv = True | n | 59 | jawn = True |
60 | break | 60 | break | ||
61 | else: | 61 | else: | ||
n | 62 | bruv = False | n | 62 | jawn = False |
63 | if (bruv == False): | 63 | if (jawn == False): | ||
64 | print('Item not found in cart. Nothing modified') | 64 | print('Item not found in cart. Nothing modified') | ||
65 | def get_num_items_in_cart(self): | 65 | def get_num_items_in_cart(self): | ||
66 | num_items = 0 | 66 | num_items = 0 | ||
n | 67 | for Z in self.cart_items: | n | 67 | for item in self.cart_items: |
68 | num_items = num_items + Z.item_quantity | 68 | num_items = num_items + item.item_quantity | ||
69 | return num_items | 69 | return num_items | ||
70 | def get_cost_of_cart(self): | 70 | def get_cost_of_cart(self): | ||
71 | total_cost = 0 | 71 | total_cost = 0 | ||
72 | cost = 0 | 72 | cost = 0 | ||
n | 73 | for Z in self.cart_items: | n | 73 | for item in self.cart_items: |
74 | cost = (Z.item_quantity * Z.item_price) | 74 | cost = (item.item_quantity * item.item_price) | ||
75 | total_cost += cost | 75 | total_cost += cost | ||
76 | return total_cost | 76 | return total_cost | ||
77 | def print_total(self): | 77 | def print_total(self): | ||
78 | total_cost = self.get_cost_of_cart() | 78 | total_cost = self.get_cost_of_cart() | ||
79 | if (total_cost == 0): | 79 | if (total_cost == 0): | ||
80 | print('SHOPPING CART IS EMPTY') | 80 | print('SHOPPING CART IS EMPTY') | ||
81 | else: | 81 | else: | ||
82 | self.output_cart() | 82 | self.output_cart() | ||
83 | def print_descriptions(self): | 83 | def print_descriptions(self): | ||
84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 84 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 85 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
86 | print('\nItem Descriptions', end='\n') | 86 | print('\nItem Descriptions', end='\n') | ||
87 | for item in self.cart_items: | 87 | for item in self.cart_items: | ||
88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 88 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
89 | def output_cart(self): | 89 | def output_cart(self): | ||
90 | new = ShoppingCart() | 90 | new = ShoppingCart() | ||
91 | print('\nOUTPUT SHOPPING CART', end='\n') | 91 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 92 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 93 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
94 | tc = 0 | 94 | tc = 0 | ||
95 | for item in self.cart_items: | 95 | for item in self.cart_items: | ||
96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 96 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
97 | item.item_price, (item.item_quantit | 97 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
98 | tc += (item.item_quantity * item.item_price) | 98 | tc += (item.item_quantity * item.item_price) | ||
99 | print('\nTotal: ${}'.format(tc), end='\n') | 99 | print('\nTotal: ${}'.format(tc), end='\n') | ||
100 | def print_menu(ShoppingCart): | 100 | def print_menu(ShoppingCart): | ||
101 | customer_Cart = newCart | 101 | customer_Cart = newCart | ||
102 | string = '' | 102 | string = '' | ||
103 | menu = ('\nMENU\n' | 103 | menu = ('\nMENU\n' | ||
104 | 'a - Add item to cart\n' | 104 | 'a - Add item to cart\n' | ||
105 | 'r - Remove item from cart\n' | 105 | 'r - Remove item from cart\n' | ||
106 | 'c - Change item quantity\n' | 106 | 'c - Change item quantity\n' | ||
107 | 'i - Output items\' descriptions\n' | 107 | 'i - Output items\' descriptions\n' | ||
108 | 'o - Output shopping cart\n' | 108 | 'o - Output shopping cart\n' | ||
109 | 'q - Quit\n') | 109 | 'q - Quit\n') | ||
110 | command = '' | 110 | command = '' | ||
111 | while (command != 'q'): | 111 | while (command != 'q'): | ||
112 | string = '' | 112 | string = '' | ||
113 | print(menu, end='\n') | 113 | print(menu, end='\n') | ||
114 | command = input('Choose an option:') | 114 | command = input('Choose an option:') | ||
115 | while (command != 'a' and command != 'o' and command != 'i' and comm | 115 | while (command != 'a' and command != 'o' and command != 'i' and comm | ||
> | and != 'r' | > | and != 'r' | ||
116 | and command != 'c' and command != 'q'): | 116 | and command != 'c' and command != 'q'): | ||
117 | command = input('Choose an option:') | 117 | command = input('Choose an option:') | ||
118 | if (command == 'a'): | 118 | if (command == 'a'): | ||
119 | customer_Cart.add_item(string) | 119 | customer_Cart.add_item(string) | ||
120 | if (command == 'o'): | 120 | if (command == 'o'): | ||
121 | customer_Cart.output_cart() | 121 | customer_Cart.output_cart() | ||
122 | if (command == 'i'): | 122 | if (command == 'i'): | ||
123 | customer_Cart.print_descriptions() | 123 | customer_Cart.print_descriptions() | ||
124 | if (command == 'r'): | 124 | if (command == 'r'): | ||
125 | customer_Cart.remove_item() | 125 | customer_Cart.remove_item() | ||
126 | if (command == 'c'): | 126 | if (command == 'c'): | ||
127 | customer_Cart.modify_item() | 127 | customer_Cart.modify_item() | ||
128 | customer_name = str(input('Enter customer\'s name:')) | 128 | customer_name = str(input('Enter customer\'s name:')) | ||
129 | current_date = str(input('\nEnter today\'s date:')) | 129 | current_date = str(input('\nEnter today\'s date:')) | ||
130 | print() | 130 | print() | ||
131 | print('\nCustomer name:', customer_name,) | 131 | print('\nCustomer name:', customer_name,) | ||
132 | print('Today\'s date:', current_date,) | 132 | print('Today\'s date:', current_date,) | ||
133 | newCart = ShoppingCart(customer_name, current_date) | 133 | newCart = ShoppingCart(customer_name, current_date) | ||
134 | print_menu(newCart)import math | 134 | print_menu(newCart)import math | ||
135 | class pt3d: | 135 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
n | 137 | self.x=x | n | 137 | self.x = x |
138 | self.y=y | 138 | self.y = y | ||
139 | self.z=z | 139 | self.z = z | ||
140 | def __add__(self,L): | ||||
141 | x1 = self.x+L.x | ||||
142 | y1 = self.y+L.y | ||||
143 | z1 = self.z+L.z | ||||
144 | return pt3d(x1, y1, z1) | ||||
145 | def __sub__(self,L): | ||||
146 | x1 = (self.x-L.x)**2 | ||||
147 | y1 = (self.y-L.y)**2 | ||||
148 | z1 = (self.z-L.z)**2 | ||||
149 | return math.sqrt(x1+y1+z1) | ||||
150 | def __str__(self): | ||||
151 | return f"<{self.x},{self.y},{self.z}>" | ||||
152 | def __eq__(self,L): | 140 | def __eq__(self, other): | ||
153 | if (self.x == L.x) and (self.y == L.y) and (self.x == L.x): | 141 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||
154 | return True | 142 | return True | ||
155 | else: | 143 | else: | ||
156 | return False | 144 | return False | ||
t | 157 | a = pt3d(1,1,1) | t | 145 | def __add__(self,other): |
158 | b = pt3d(2,2,2) | 146 | addition_x = self.x + other.x | ||
159 | print(a-b) | 147 | addition_y = self.y + other.y | ||
148 | addition_z = self.z + other.z | ||||
149 | return pt3d(addition_x,addition_y,addition_z) | ||||
150 | def __eq__(self, other): | ||||
151 | if self.x == other.x and self.y == other.y and self.z == other.z: | ||||
152 | return True | ||||
153 | else: | ||||
154 | return False | ||||
155 | def __add__(self,other): | ||||
156 | addition_x = self.x + other.x | ||||
157 | addition_y = self.y + other.y | ||||
158 | addition_z = self.z + other.z | ||||
159 | return pt3d(addition_x,addition_y,addition_z) | ||||
160 | def __sub__(self, other): | ||||
161 | subtraction_x = self.x - other.x | ||||
162 | subtraction_y = self.y - other.y | ||||
163 | subtraction_z = self.z - other.z | ||||
164 | distance = math.sqrt(subtraction_x**2 + subtraction_y**2 + subtraction_z | ||||
> | **2) | ||||
165 | return distance | ||||
166 | def __str__(self): | ||||
167 | return (f'<{self.x},{self.y},{self.z}>') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import math |
2 | class Circle(): | 2 | class Circle(): | ||
n | 3 | def __init__(self, r): | n | 3 | def __init__(self, radius): |
4 | self.radius = r | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return self.radius**2*3.14159265359 | n | 6 | return math.pi * self.radius**2 |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return 2*self.radius*3.14159265359 | n | 8 | return 2*math.pi*self.radius |
9 | if __name__ == '__main__': | ||||
10 | x = int(input()) | ||||
9 | NewCircle = Circle(8) | 11 | NewCircle = Circle(x) | ||
10 | print(NewCircle.area()) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
11 | print(NewCircle.perimeter()) | 13 | print('{:.5f}'.format(NewCircle.perimeter())) | ||
12 | class ItemToPurchase: | 14 | class ItemToPurchase: | ||
13 | def __init__(self, name='none', price=0, quantity=0, description='none'): | 15 | def __init__(self, name='none', price=0, quantity=0, description='none'): | ||
14 | self.item_name=name | 16 | self.item_name=name | ||
15 | self.item_description=description | 17 | self.item_description=description | ||
16 | self.item_price=price | 18 | self.item_price=price | ||
17 | self.item_quantity=quantity | 19 | self.item_quantity=quantity | ||
n | 18 | def print_item_cost(self): | n | ||
19 | total = self.item_price * self.item_quantity | ||||
20 | print('%s %d @ $%d = $%d' % (self.item_name, self.item_quantity, self.ite | ||||
> | m_price, total)) | ||||
21 | def print_item_description(self): | 20 | def print_item_description(self): | ||
n | 22 | print('%s: %s' % (self.item_name, self.item_description)) | n | 21 | print('%s: %s' % (self.item_name, self.item_description)) |
23 | class ShoppingCart: | 22 | class ShoppingCart: | ||
n | 24 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016'): | n | 23 | def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', |
> | cart_items = []): | ||||
25 | self.customer_name = customer_name | 24 | self.customer_name = customer_name | ||
26 | self.current_date = current_date | 25 | self.current_date = current_date | ||
n | 27 | self.cart_items = [] | n | 26 | self.cart_items = cart_items |
28 | def add_item(self, itemToPurchase): | 27 | def add_item(self, itemToPurchase): | ||
n | 29 | self.cart_items.append(itemToPurchase) | n | 28 | self.cart_items.append(itemToPurchase) |
30 | def remove_item(self, itemName): | 29 | def remove_item(self, itemName): | ||
31 | tremove_item = False | 30 | tremove_item = False | ||
32 | for item in self.cart_items: | 31 | for item in self.cart_items: | ||
33 | if item.item_name == itemName: | 32 | if item.item_name == itemName: | ||
34 | self.cart_items.remove(item) | 33 | self.cart_items.remove(item) | ||
35 | tremove_item = True | 34 | tremove_item = True | ||
36 | break | 35 | break | ||
37 | if not tremove_item: | 36 | if not tremove_item: | ||
38 | print('Item not found in the cart. Nothing removed') | 37 | print('Item not found in the cart. Nothing removed') | ||
n | 39 | def modify_item(self, itemToPurchase): | n | 38 | def modify_item(self, itemToPurchase): |
40 | tmodify_item = False | 39 | tmodify_item = False | ||
41 | for i in range(len(self.cart_items)): | 40 | for i in range(len(self.cart_items)): | ||
42 | if self.cart_items[i].item_name == itemToPurchase.item_name: | 41 | if self.cart_items[i].item_name == itemToPurchase.item_name: | ||
43 | tmodify_item = True | 42 | tmodify_item = True | ||
n | 44 | if(itemToPurchase.item_price == 0 and itemToPurchase.item_quantit | n | ||
> | y == 0 and itemToPurchase.item_description == 'none'): | ||||
45 | break | ||||
46 | else: | ||||
47 | if(itemToPurchase.item_price != 0): | ||||
48 | self.cart_items[i].item_price = itemToPurchase.item_price | ||||
49 | if(itemToPurchase.item_quantity != 0): | ||||
50 | self.cart_items[i].item_quantity = itemToPurchase.item_qu | 43 | self.cart_items[i].item_quantity = itemToPurchase.item_quantity | ||
> | antity | ||||
51 | if(itemToPurchase.item_description != 'none'): | ||||
52 | self.cart_items[i].item_description = itemToPurchase.item | ||||
> | _description | ||||
53 | break | 44 | break | ||
54 | if not tmodify_item: | 45 | if not tmodify_item: | ||
55 | print('Item not found in the cart. Nothing modified') | 46 | print('Item not found in the cart. Nothing modified') | ||
56 | def get_num_items_in_cart(self): | 47 | def get_num_items_in_cart(self): | ||
57 | num_items = 0 | 48 | num_items = 0 | ||
58 | for item in self.cart_items: | 49 | for item in self.cart_items: | ||
59 | num_items = num_items + item.item_quantity | 50 | num_items = num_items + item.item_quantity | ||
60 | return num_items | 51 | return num_items | ||
61 | def get_cost_of_cart(self): | 52 | def get_cost_of_cart(self): | ||
62 | total_cost = 0 | 53 | total_cost = 0 | ||
63 | cost = 0 | 54 | cost = 0 | ||
64 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
65 | cost = (item.item_quantity * item.item_price) | 56 | cost = (item.item_quantity * item.item_price) | ||
66 | total_cost += cost | 57 | total_cost += cost | ||
67 | return total_cost | 58 | return total_cost | ||
68 | def print_total(self): | 59 | def print_total(self): | ||
69 | total_cost = self.get_cost_of_cart() | 60 | total_cost = self.get_cost_of_cart() | ||
70 | if (total_cost == 0): | 61 | if (total_cost == 0): | ||
71 | print('SHOPPING CART IS EMPTY') | 62 | print('SHOPPING CART IS EMPTY') | ||
72 | else: | 63 | else: | ||
73 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 64 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
74 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | 65 | print('Number of Items: %d\n' %self.get_num_items_in_cart()) | ||
75 | for item in self.cart_items: | 66 | for item in self.cart_items: | ||
n | 76 | item.print_item_cost() | n | 67 | total = item.item_price * item.item_quantity |
68 | print('%s %d @ $%d = $%d' % (item.item_name, item.item_quantity, | ||||
> | item.item_price, total)) | ||||
77 | print('\nTotal: $%d' %(total_cost)) | 69 | print('\nTotal: $%d' %(total_cost)) | ||
78 | def print_descriptions(self): | 70 | def print_descriptions(self): | ||
79 | if len(self.cart_items) == 0: | 71 | if len(self.cart_items) == 0: | ||
80 | print('SHOPPING CART IS EMPTY') | 72 | print('SHOPPING CART IS EMPTY') | ||
81 | else: | 73 | else: | ||
82 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | 74 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.curr | ||
> | ent_date)) | > | ent_date)) | ||
83 | print('\nItem Descriptions') | 75 | print('\nItem Descriptions') | ||
84 | for item in self.cart_items: | 76 | for item in self.cart_items: | ||
85 | item.print_item_description() | 77 | item.print_item_description() | ||
86 | def print_menu(newCart): | 78 | def print_menu(newCart): | ||
87 | customer_Cart = newCart | 79 | customer_Cart = newCart | ||
88 | menu = ('\nMENU\n' | 80 | menu = ('\nMENU\n' | ||
89 | 'a - Add item to cart\n' | 81 | 'a - Add item to cart\n' | ||
90 | 'r - Remove item from the cart\n' | 82 | 'r - Remove item from the cart\n' | ||
91 | 'c - Change item quantity\n' | 83 | 'c - Change item quantity\n' | ||
92 | "i - Output item's descriptions\n" | 84 | "i - Output item's descriptions\n" | ||
93 | 'o - Output shopping cart\n' | 85 | 'o - Output shopping cart\n' | ||
94 | 'q - Quit\n') | 86 | 'q - Quit\n') | ||
95 | command = '' | 87 | command = '' | ||
96 | while(command != 'q'): | 88 | while(command != 'q'): | ||
97 | print(menu) | 89 | print(menu) | ||
98 | command = input('Choose an option:') | 90 | command = input('Choose an option:') | ||
99 | while(command != 'a' and command != 'o' and command != 'i' and command != | 91 | while(command != 'a' and command != 'o' and command != 'i' and command != | ||
> | 'q' and command != 'r' and command != 'c'): | > | 'q' and command != 'r' and command != 'c'): | ||
100 | command = input('Choose an option:\n') | 92 | command = input('Choose an option:\n') | ||
101 | if(command == 'a'): | 93 | if(command == 'a'): | ||
102 | print("\nADD ITEM TO CART") | 94 | print("\nADD ITEM TO CART") | ||
103 | item_name = input('Enter the item name:\n') | 95 | item_name = input('Enter the item name:\n') | ||
104 | item_description = input('Enter the item description:\n') | 96 | item_description = input('Enter the item description:\n') | ||
105 | item_price = int(input('Enter the item price:\n')) | 97 | item_price = int(input('Enter the item price:\n')) | ||
106 | item_quantity = int(input('Enter the item quantity:\n')) | 98 | item_quantity = int(input('Enter the item quantity:\n')) | ||
107 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | 99 | itemtoPurchase = ItemToPurchase(item_name, item_price, item_quantity, | ||
> | item_description) | > | item_description) | ||
108 | customer_Cart.add_item(itemtoPurchase) | 100 | customer_Cart.add_item(itemtoPurchase) | ||
109 | elif(command == 'o'): | 101 | elif(command == 'o'): | ||
110 | print('\nOUTPUT SHOPPING CART') | 102 | print('\nOUTPUT SHOPPING CART') | ||
111 | customer_Cart.print_total() | 103 | customer_Cart.print_total() | ||
112 | elif(command == 'i'): | 104 | elif(command == 'i'): | ||
113 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 105 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
114 | customer_Cart.print_descriptions() | 106 | customer_Cart.print_descriptions() | ||
115 | elif(command == 'r'): | 107 | elif(command == 'r'): | ||
116 | print('REMOVE ITEM FROM CART') | 108 | print('REMOVE ITEM FROM CART') | ||
117 | itemName = input('Enter the name of the item to remove :\n') | 109 | itemName = input('Enter the name of the item to remove :\n') | ||
118 | customer_Cart.remove_item(itemName) | 110 | customer_Cart.remove_item(itemName) | ||
119 | elif(command == 'c'): | 111 | elif(command == 'c'): | ||
120 | print('\nCHANGE ITEM QUANTITY') | 112 | print('\nCHANGE ITEM QUANTITY') | ||
121 | itemName = input('Enter the name of the item :\n') | 113 | itemName = input('Enter the name of the item :\n') | ||
122 | qty = int(input('Enter the new quantity :\n')) | 114 | qty = int(input('Enter the new quantity :\n')) | ||
123 | itemToPurchase = ItemToPurchase(itemName,0,qty) | 115 | itemToPurchase = ItemToPurchase(itemName,0,qty) | ||
n | 124 | customer_Cart.modify_item(itemToPurchase)import math | n | 116 | customer_Cart.modify_item(itemToPurchase) |
117 | if __name__ == "__main__": | ||||
118 | customer_name = input("Enter customer's name:\n") | ||||
119 | current_date = input("Enter today's date:\n") | ||||
120 | print("\nCustomer name: %s" %customer_name) | ||||
121 | print("Today's date: %s" %current_date) | ||||
122 | newCart = ShoppingCart(customer_name, current_date) | ||||
123 | print_menu(newCart)from math import sqrt | ||||
125 | class pt3d: | 124 | class pt3d: | ||
n | 126 | def __init__(self,x=0,y=0,z=0): | n | 125 | def __init__(self, x, y, z): |
127 | self.x = x | 126 | self.x = x | ||
128 | self.y = y | 127 | self.y = y | ||
129 | self.z = z | 128 | self.z = z | ||
130 | def __add__(self, other): | 129 | def __add__(self, other): | ||
131 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | 130 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
132 | def __sub__(self, other): | 131 | def __sub__(self, other): | ||
n | 133 | return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z | n | 132 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot |
> | - other.z)**2) | > | her.z)**2) | ||
134 | def __eq__(self, other): | 133 | def __eq__(self, other): | ||
n | 135 | if self.x == other.x and self.y == other.y and self.z == other.z: | n | 134 | return self.x == other.x & self.y == other.y & self.z == other.z |
136 | return True | ||||
137 | return False | ||||
138 | def __str__(self): | 135 | def __str__(self): | ||
t | 139 | return '<{},{},{}>'.format(self.x, self.y, self.z) | t | 136 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
137 | p1 = pt3d(1, 1, 1) | ||||
138 | p2 = pt3d(2, 2, 2) | ||||
139 | print(p1 + p2) | ||||
140 | print(p1 - p2) | ||||
141 | print(p1 == p2) | ||||
142 | print(p1+p1 == p2) | ||||
143 | print(p1==p2+pt3d(-1, -1, -1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | class Circle: | n | 2 | class Circle(): |
3 | def __init__(self,rad): | 3 | def __init__(self, radius): | ||
4 | self.rad=rad | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area=math.pi*self.rad*self.rad | n | 6 | return math.pi * self.radius**2 |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | per=math.pi*2*self.rad | n | 8 | return 2 * math.pi * self.radius |
10 | return per | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | 14 | def __init__(self, item_name='none', item_price=0, item_quantity=0, item_des | ||
> | cription='none'): | > | cription='none'): | ||
17 | self.item_name = item_name | 15 | self.item_name = item_name | ||
18 | self.item_price = item_price | 16 | self.item_price = item_price | ||
19 | self.item_quantity = item_quantity | 17 | self.item_quantity = item_quantity | ||
20 | self.item_description = item_description | 18 | self.item_description = item_description | ||
21 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
22 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | 20 | string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, | ||
> | self.item_price, | > | self.item_price, | ||
23 | (self.item_quantity * self.item_pric | 21 | (self.item_quantity * self.item_pric | ||
> | e)) | > | e)) | ||
24 | cost = self.item_quantity * self.item_price | 22 | cost = self.item_quantity * self.item_price | ||
25 | return string, cost | 23 | return string, cost | ||
26 | def print_item_description(self): | 24 | def print_item_description(self): | ||
27 | string = '{}: {}'.format(self.item_name, self.item_description) | 25 | string = '{}: {}'.format(self.item_name, self.item_description) | ||
28 | print(string, end='\n') | 26 | print(string, end='\n') | ||
29 | return string | 27 | return string | ||
30 | class ShoppingCart: | 28 | class ShoppingCart: | ||
31 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | 29 | def __init__(self, customer_name='none', current_date='January 1, 2016', car | ||
> | t_items=[]): | > | t_items=[]): | ||
32 | self.customer_name = customer_name | 30 | self.customer_name = customer_name | ||
33 | self.current_date = current_date | 31 | self.current_date = current_date | ||
34 | self.cart_items = cart_items | 32 | self.cart_items = cart_items | ||
35 | def add_item(self, string): | 33 | def add_item(self, string): | ||
36 | print('\nADD ITEM TO CART', end='\n') | 34 | print('\nADD ITEM TO CART', end='\n') | ||
n | 37 | item_name = str(input('Enter the item name:')) | n | 35 | item_name = str(input('Enter the item name: ')) |
38 | item_description = str(input('\nEnter the item description:')) | 36 | item_description = str(input('\nEnter the item description: ')) | ||
39 | item_price = int(input('\nEnter the item price:')) | 37 | item_price = int(input('\nEnter the item price: ')) | ||
40 | item_quantity = int(input('\nEnter the item quantity:\n')) | 38 | item_quantity = int(input('\nEnter the item quantity: ')) | ||
41 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | 39 | self.cart_items.append(ItemToPurchase(item_name, item_price, item_quanti | ||
> | ty, item_description)) | > | ty, item_description)) | ||
42 | def remove_item(self): | 40 | def remove_item(self): | ||
n | 43 | print('REMOVE ITEM FROM CART', end='\n') | n | 41 | print('\nREMOVE ITEM FROM CART', end='\n') |
44 | string = str(input('Enter name of item to remove:\n')) | 42 | string = str(input('Enter name of item to remove: ')) | ||
45 | i = 0 | 43 | i = 0 | ||
46 | for item in self.cart_items: | 44 | for item in self.cart_items: | ||
47 | if (item.item_name == string): | 45 | if (item.item_name == string): | ||
48 | del self.cart_items[i] | 46 | del self.cart_items[i] | ||
49 | i += 1 | 47 | i += 1 | ||
50 | flag = True | 48 | flag = True | ||
51 | break | 49 | break | ||
52 | else: | 50 | else: | ||
53 | flag = False | 51 | flag = False | ||
54 | if (flag == False): | 52 | if (flag == False): | ||
n | 55 | print('Item not found in cart. Nothing removed.') | n | 53 | print('Item not found in cart. Nothing removed') |
56 | def modify_item(self): | 54 | def modify_item(self): | ||
57 | print('\nCHANGE ITEM QUANTITY', end='\n') | 55 | print('\nCHANGE ITEM QUANTITY', end='\n') | ||
n | 58 | name = str(input('Enter the item name:')) | n | 56 | name = str(input('Enter the item name: ')) |
59 | for item in self.cart_items: | 57 | for item in self.cart_items: | ||
60 | if (item.item_name == name): | 58 | if (item.item_name == name): | ||
n | 61 | quantity = int(input('Enter the new quantity:')) | n | 59 | quantity = int(input('Enter the new quantity: ')) |
62 | item.item_quantity = quantity | 60 | item.item_quantity = quantity | ||
63 | flag = True | 61 | flag = True | ||
64 | break | 62 | break | ||
65 | else: | 63 | else: | ||
66 | flag = False | 64 | flag = False | ||
67 | if (flag == False): | 65 | if (flag == False): | ||
n | 68 | print('Item not found in cart. Nothing modified.') | n | 66 | print('Item not found in cart. Nothing modified') |
69 | def get_num_items_in_cart(self): | 67 | def get_num_items_in_cart(self): | ||
70 | num_items = 0 | 68 | num_items = 0 | ||
71 | for item in self.cart_items: | 69 | for item in self.cart_items: | ||
72 | num_items = num_items + item.item_quantity | 70 | num_items = num_items + item.item_quantity | ||
73 | return num_items | 71 | return num_items | ||
74 | def get_cost_of_cart(self): | 72 | def get_cost_of_cart(self): | ||
75 | total_cost = 0 | 73 | total_cost = 0 | ||
76 | cost = 0 | 74 | cost = 0 | ||
77 | for item in self.cart_items: | 75 | for item in self.cart_items: | ||
78 | cost = (item.item_quantity * item.item_price) | 76 | cost = (item.item_quantity * item.item_price) | ||
79 | total_cost += cost | 77 | total_cost += cost | ||
80 | return total_cost | 78 | return total_cost | ||
81 | def print_total(self): | 79 | def print_total(self): | ||
82 | total_cost = self.get_cost_of_cart() | 80 | total_cost = self.get_cost_of_cart() | ||
83 | if (total_cost == 0): | 81 | if (total_cost == 0): | ||
84 | print('SHOPPING CART IS EMPTY') | 82 | print('SHOPPING CART IS EMPTY') | ||
85 | else: | 83 | else: | ||
86 | self.output_cart() | 84 | self.output_cart() | ||
87 | def print_descriptions(self): | 85 | def print_descriptions(self): | ||
88 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | 86 | print('\nOUTPUT ITEMS\' DESCRIPTIONS') | ||
89 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 87 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
90 | print('\nItem Descriptions', end='\n') | 88 | print('\nItem Descriptions', end='\n') | ||
91 | for item in self.cart_items: | 89 | for item in self.cart_items: | ||
92 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | 90 | print('{}: {}'.format(item.item_name, item.item_description), end='\ | ||
> | n') | > | n') | ||
93 | def output_cart(self): | 91 | def output_cart(self): | ||
94 | new = ShoppingCart() | 92 | new = ShoppingCart() | ||
95 | print('\nOUTPUT SHOPPING CART', end='\n') | 93 | print('\nOUTPUT SHOPPING CART', end='\n') | ||
96 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | 94 | print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current | ||
> | _date), end='\n') | > | _date), end='\n') | ||
97 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | 95 | print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') | ||
98 | tc = 0 | 96 | tc = 0 | ||
99 | for item in self.cart_items: | 97 | for item in self.cart_items: | ||
100 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | 98 | print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, | ||
101 | item.item_price, (item.item_quantit | 99 | item.item_price, (item.item_quantit | ||
> | y * item.item_price)), end='\n') | > | y * item.item_price)), end='\n') | ||
102 | tc += (item.item_quantity * item.item_price) | 100 | tc += (item.item_quantity * item.item_price) | ||
103 | print('\nTotal: ${}'.format(tc), end='\n') | 101 | print('\nTotal: ${}'.format(tc), end='\n') | ||
104 | def print_menu(ShoppingCart): | 102 | def print_menu(ShoppingCart): | ||
105 | customer_Cart = newCart | 103 | customer_Cart = newCart | ||
106 | string = '' | 104 | string = '' | ||
107 | menu = ('\nMENU\n' | 105 | menu = ('\nMENU\n' | ||
108 | 'a - Add item to cart\n' | 106 | 'a - Add item to cart\n' | ||
109 | 'r - Remove item from cart\n' | 107 | 'r - Remove item from cart\n' | ||
110 | 'c - Change item quantity\n' | 108 | 'c - Change item quantity\n' | ||
111 | 'i - Output items\' descriptions\n' | 109 | 'i - Output items\' descriptions\n' | ||
112 | 'o - Output shopping cart\n' | 110 | 'o - Output shopping cart\n' | ||
113 | 'q - Quit\n') | 111 | 'q - Quit\n') | ||
114 | command = '' | 112 | command = '' | ||
115 | while (command != 'q'): | 113 | while (command != 'q'): | ||
116 | string = '' | 114 | string = '' | ||
117 | print(menu, end='\n') | 115 | print(menu, end='\n') | ||
n | 118 | command = input('Choose an option:\n') | n | 116 | command = input('Choose an option: ') |
119 | while (command != 'a' and command != 'o' and command != 'i' and command | 117 | while (command != 'a' and command != 'o' and command != 'i' and command | ||
> | != 'r' | > | != 'r' | ||
120 | and command != 'c' and command != 'q'): | 118 | and command != 'c' and command != 'q'): | ||
n | 121 | command = input('Choose an option:\n') | n | 119 | command = input('Choose an option: ') |
122 | if (command == 'a'): | 120 | if (command == 'a'): | ||
123 | customer_Cart.add_item(string) | 121 | customer_Cart.add_item(string) | ||
124 | if (command == 'o'): | 122 | if (command == 'o'): | ||
125 | customer_Cart.output_cart() | 123 | customer_Cart.output_cart() | ||
126 | if (command == 'i'): | 124 | if (command == 'i'): | ||
127 | customer_Cart.print_descriptions() | 125 | customer_Cart.print_descriptions() | ||
128 | if (command == 'r'): | 126 | if (command == 'r'): | ||
129 | customer_Cart.remove_item() | 127 | customer_Cart.remove_item() | ||
130 | if (command == 'c'): | 128 | if (command == 'c'): | ||
131 | customer_Cart.modify_item() | 129 | customer_Cart.modify_item() | ||
n | 132 | if __name__ == '__main__': | n | ||
133 | customer_name = str(input('Enter customer\'s name:')) | 130 | customer_name = str(input('Enter customer\'s name: ')) | ||
134 | current_date = str(input('\nEnter today\'s date:\n\n')) | 131 | current_date = str(input('\nEnter today\'s date: ')) | ||
135 | print('Customer name:', customer_name) | 132 | print('Customer name:', customer_name, end='\n') | ||
136 | print('Today\'s date:', current_date) | 133 | print('Today\'s date:', current_date, end='\n') | ||
137 | newCart = ShoppingCart(customer_name, current_date) | 134 | newCart = ShoppingCart(customer_name, current_date) | ||
138 | print_menu(newCart) | 135 | print_menu(newCart)from math import sqrt | ||
139 | import math | ||||
140 | class pt3d: | 136 | class pt3d: | ||
n | 141 | def __init__(self,x=0,y=0,z=0): | n | 137 | def __init__(self, x=0, y=0, z=0): |
142 | self.x = x | 138 | self.x = x | ||
143 | self.y = y | 139 | self.y = y | ||
144 | self.z = z | 140 | self.z = z | ||
n | 145 | def __add__(self,pt): | n | 141 | def __add__(self, other): |
146 | if isinstance(pt,pt3d): | ||||
147 | return pt3d(self.x + pt.x , self.y + pt.y , self.z + pt.z) | 142 | return pt3d(self.x + other.x, self.y + other.y, self.z + other.z) | ||
148 | def __sub__(self,pt): | 143 | def __sub__(self, other): | ||
149 | if isinstance(pt,pt3d): | 144 | return sqrt((self.x - other.x)**2 + (self.y - other.y)**2 + (self.z - ot | ||
> | her.z)**2) | ||||
150 | X = pow((self.x - pt.x),2) | ||||
151 | Y = pow((self.y - pt.y),2) | ||||
152 | Z = pow((self.z - pt.z),2) | ||||
153 | return math.sqrt(X + Y + Z) | ||||
154 | def __eq__(self,pt): | 145 | def __eq__(self, other): | ||
155 | if self.x == pt.x and self.y == pt.y and self.z == pt.z: | 146 | return self.x == other.x & self.y == other.y & self.z == other.z | ||
156 | return True | ||||
157 | else: | ||||
158 | return False | ||||
159 | def __str__(self): | 147 | def __str__(self): | ||
t | 160 | return "<{},{},{}>".format(self.x,self.y,self.z) | t | 148 | return '<{}, {}, {}>'.format(self.x, self.y, self.z) |
161 | def main(): | 149 | if __name__ == '__main__': | ||
162 | p1 = pt3d(1,1,1) | 150 | p1 = pt3d(1, 1, 1) | ||
163 | p2 = pt3d(2,2,2) | 151 | p2 = pt3d(2, 2, 2) | ||
164 | print(p1 + p2) | 152 | print(p1+p2) | ||
165 | print(p1 - p2) | 153 | print(p1-p2) | ||
166 | print(p1 == p2) | 154 | print(p1==p2) | ||
167 | print(p1+p1 == p2) | 155 | print(p1+p1==p2) | ||
168 | if __name__ == "__main__": | 156 | print(p1==p2+pt3d(-1,-1,-1)) | ||
169 | main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius=0,): | n | 3 | def __init__(self,x=0): |
4 | self.radius=radius | 4 | self.x=x | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | area=self.radius*self.radius*math.pi | n | 6 | return (self.x**2)*math.pi |
7 | return area | ||||
8 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 9 | perimeter=2*self.radius*math.pi | n | 8 | return self.x*2*math.pi |
10 | return perimeter | ||||
11 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
12 | x = int(input()) | 10 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 16 | def init(self,item_name='none',item_price=0,item_quantity=0,item_description | n | 14 | def __init__(self,item_name='none',item_price=0,item_quantity=0,item_descrip |
> | ='none'): | > | tion='none'): | ||
17 | self.item_name=item_name | 15 | self.item_name=item_name | ||
18 | self.item_price=item_price | 16 | self.item_price=item_price | ||
19 | self.item_quantity=item_quantity | 17 | self.item_quantity=item_quantity | ||
20 | self.item_description=item_description | 18 | self.item_description=item_description | ||
21 | def print_item_description(self): | 19 | def print_item_description(self): | ||
22 | return print(self.item_description) | 20 | return print(self.item_description) | ||
23 | def print_item_cost(self): | 21 | def print_item_cost(self): | ||
24 | totalCost=self.item_quantity*self.item_price | 22 | totalCost=self.item_quantity*self.item_price | ||
25 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 23 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price,totalCost)) | > | item_price,totalCost)) | ||
26 | class ShoppingCart: | 24 | class ShoppingCart: | ||
27 | def __init__(self,customer_name='none',current_date='January 1, 2016',cart_i | 25 | def __init__(self,customer_name='none',current_date='January 1, 2016',cart_i | ||
> | tems=[]): | > | tems=[]): | ||
28 | self.customer_name=customer_name | 26 | self.customer_name=customer_name | ||
29 | self.current_date=current_date | 27 | self.current_date=current_date | ||
30 | self.cart_items=cart_items | 28 | self.cart_items=cart_items | ||
n | 31 | def __add_item__(self,ItemToPurchase): | n | 29 | def add_item(self,ItemToPurchase): |
32 | print('ADD ITEM TO CART',end='\n') | 30 | print('ADD ITEM TO CART',end='\n') | ||
33 | item_name=str(input('Enter the item name:\n')) | 31 | item_name=str(input('Enter the item name:\n')) | ||
34 | item_description=str(input('Enter the item description:\n')) | 32 | item_description=str(input('Enter the item description:\n')) | ||
35 | item_price=int(input('Enter the item price:\n')) | 33 | item_price=int(input('Enter the item price:\n')) | ||
36 | item_quantity=int(input('Enter the item quantity:\n')) | 34 | item_quantity=int(input('Enter the item quantity:\n')) | ||
37 | self.cart_items.append(ItemToPurchase(item_name,item_price,item_quantity | 35 | self.cart_items.append(ItemToPurchase(item_name,item_price,item_quantity | ||
> | ,item_description)) | > | ,item_description)) | ||
38 | def remove_item(self): | 36 | def remove_item(self): | ||
39 | print('REMOVE ITEM FROM CART\n') | 37 | print('REMOVE ITEM FROM CART\n') | ||
40 | item=str(input('Enter name of item to remove:\n')) | 38 | item=str(input('Enter name of item to remove:\n')) | ||
41 | i=0 | 39 | i=0 | ||
42 | for item in self.cart_items: | 40 | for item in self.cart_items: | ||
43 | if self.item_name == item: | 41 | if self.item_name == item: | ||
44 | self.cart_items.remove(item) | 42 | self.cart_items.remove(item) | ||
45 | i+=1 | 43 | i+=1 | ||
46 | delete=True | 44 | delete=True | ||
47 | break | 45 | break | ||
48 | else: | 46 | else: | ||
49 | delete=False | 47 | delete=False | ||
50 | if delete==False: | 48 | if delete==False: | ||
51 | print('Item not found in cart. Nothing removed.') | 49 | print('Item not found in cart. Nothing removed.') | ||
52 | def modify_item(self): | 50 | def modify_item(self): | ||
53 | print('CHANGE ITEM QUANTITY\n') | 51 | print('CHANGE ITEM QUANTITY\n') | ||
54 | name=str(input('Enter the item name:\n')) | 52 | name=str(input('Enter the item name:\n')) | ||
55 | for item in self.cart_items: | 53 | for item in self.cart_items: | ||
56 | if self.item_name == name: | 54 | if self.item_name == name: | ||
57 | quantity=int(input('Enter the new quantity:\n')) | 55 | quantity=int(input('Enter the new quantity:\n')) | ||
58 | item.item_quantity=quantity | 56 | item.item_quantity=quantity | ||
59 | mod = True | 57 | mod = True | ||
60 | break | 58 | break | ||
61 | else: | 59 | else: | ||
62 | mod=False | 60 | mod=False | ||
63 | if mod==False: | 61 | if mod==False: | ||
64 | print('Item not found in cart. Nothing modified.') | 62 | print('Item not found in cart. Nothing modified.') | ||
65 | def get_num_items_in_cart(self): | 63 | def get_num_items_in_cart(self): | ||
66 | total=0 | 64 | total=0 | ||
67 | for item in self.cart_items: | 65 | for item in self.cart_items: | ||
68 | total+=item.item_quantity | 66 | total+=item.item_quantity | ||
69 | return total | 67 | return total | ||
70 | def get_cost_of_cart(self): | 68 | def get_cost_of_cart(self): | ||
71 | totalc=0 | 69 | totalc=0 | ||
72 | for item in self.cart_items: | 70 | for item in self.cart_items: | ||
n | 73 | totalc+=(self.item_quantityself.item_price) | n | 71 | totalc+=(self.item_quantity*self.item_price) |
74 | return totalc | 72 | return totalc | ||
75 | def print_total(self,get_cost_of_cart,output_cart): | 73 | def print_total(self,get_cost_of_cart,output_cart): | ||
76 | totalcost=get_cost_of_cart() | 74 | totalcost=get_cost_of_cart() | ||
77 | if totalcost==0: | 75 | if totalcost==0: | ||
78 | print('SHOPPING CART IS EMPTY') | 76 | print('SHOPPING CART IS EMPTY') | ||
79 | else: | 77 | else: | ||
80 | output_cart() | 78 | output_cart() | ||
81 | def print_descriptions(self): | 79 | def print_descriptions(self): | ||
82 | print("OUTPUT ITEMS' DESCRIPTIONS\n") | 80 | print("OUTPUT ITEMS' DESCRIPTIONS\n") | ||
83 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | 81 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | ||
> | _date)) | > | _date)) | ||
84 | print('Item Descriptions\n') | 82 | print('Item Descriptions\n') | ||
85 | for item in self.cart_items: | 83 | for item in self.cart_items: | ||
86 | print('{}: {}\n'.format(item.item_name,item.item_description)) | 84 | print('{}: {}\n'.format(item.item_name,item.item_description)) | ||
87 | def output_cart(self): | 85 | def output_cart(self): | ||
88 | new=ShoppingCart() | 86 | new=ShoppingCart() | ||
89 | print('OUTPUT SHOPPING CART\n') | 87 | print('OUTPUT SHOPPING CART\n') | ||
90 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | 88 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | ||
> | _date)) | > | _date)) | ||
91 | print('Number of Items: {}\n'.format(new.get_num_items_in_cart)) | 89 | print('Number of Items: {}\n'.format(new.get_num_items_in_cart)) | ||
92 | costtotal=0 | 90 | costtotal=0 | ||
93 | for item in self.cart_items: | 91 | for item in self.cart_items: | ||
94 | print('{} {} @ ${} = ${}\n'.format(item.item_name,item.item_quantity | 92 | print('{} {} @ ${} = ${}\n'.format(item.item_name,item.item_quantity | ||
> | ,item.item_price, | > | ,item.item_price, | ||
n | 95 | (item.item_quantityitem.item_price))) | n | 93 | (item.item_quantity*item.item_price))) |
96 | costtotal+=(item.item_quantity*item.item_price) | 94 | costtotal+=(item.item_quantity*item.item_price) | ||
97 | print('Total: ${}\n'.format(costtotal)) | 95 | print('Total: ${}\n'.format(costtotal)) | ||
98 | def print_menu(ShoppingCart): | 96 | def print_menu(ShoppingCart): | ||
99 | customer_Cart=newCart | 97 | customer_Cart=newCart | ||
100 | blank='' | 98 | blank='' | ||
101 | menu=('MENU\n' | 99 | menu=('MENU\n' | ||
102 | 'a - Add item to cart\n' | 100 | 'a - Add item to cart\n' | ||
103 | 'r - Remove item from cart\n' | 101 | 'r - Remove item from cart\n' | ||
104 | 'c - Change item quantity\n' | 102 | 'c - Change item quantity\n' | ||
105 | "i - Output items' descriptions\n" | 103 | "i - Output items' descriptions\n" | ||
106 | 'o - Output shopping cart\n' | 104 | 'o - Output shopping cart\n' | ||
107 | 'q - Quit\n') | 105 | 'q - Quit\n') | ||
108 | command='' | 106 | command='' | ||
109 | while command!='q': | 107 | while command!='q': | ||
n | 110 | string='' | n | 108 | blank='' |
111 | print(menu,end='\n') | 109 | print(menu,end='\n') | ||
112 | command = input('Choose an option:\n') | 110 | command = input('Choose an option:\n') | ||
n | 113 | while (command != 'a' and command != 'r' and command != 'c' and command | n | 111 | while (command != 'a' and command != 'r' and command != 'c' and comm |
> | != 'i' and command != 'o' and command != 'q'): | > | and != 'i' and command != 'o' | ||
112 | and command != 'q'): | ||||
114 | command = input('Choose an option:\n') | 113 | command = input('Choose an option:\n') | ||
115 | if command == 'a': | 114 | if command == 'a': | ||
116 | customer_Cart.add_item(string) | 115 | customer_Cart.add_item(blank) | ||
117 | if command == 'r': | 116 | if command == 'r': | ||
118 | customer_Cart.remove_item() | 117 | customer_Cart.remove_item() | ||
119 | if command == 'c': | 118 | if command == 'c': | ||
120 | customer_Cart.modify_item() | 119 | customer_Cart.modify_item() | ||
121 | if command == 'i': | 120 | if command == 'i': | ||
122 | customer_Cart.print_descriptions() | 121 | customer_Cart.print_descriptions() | ||
123 | if command == 'o': | 122 | if command == 'o': | ||
124 | customer_Cart.output_cart() | 123 | customer_Cart.output_cart() | ||
125 | if __name__=='__main__': | 124 | if __name__=='__main__': | ||
126 | itemdesc=ItemToPurchase() | 125 | itemdesc=ItemToPurchase() | ||
n | 127 | customer_name=str(input("Enter customer's name:\n")) | n | 126 | shopcart=ShoppingCart() |
128 | current_date=str(input("Enter today's date:\n")) | 127 | customer_name=str(input()) | ||
128 | current_date=str(input()) | ||||
129 | print("Enter customer's name:") | ||||
130 | print("Enter today's date:") | ||||
129 | print('\nCustomer name: {}'.format(customer_name)) | 131 | print('\nCustomer name: {}'.format(customer_name)) | ||
130 | print("Today's date: {}\n".format(current_date)) | 132 | print("Today's date: {}\n".format(current_date)) | ||
131 | newCart=ShoppingCart(customer_name,current_date) | 133 | newCart=ShoppingCart(customer_name,current_date) | ||
n | 132 | newCart.print_menu | n | 134 | shopcart.print_menu()import math |
133 | ShoppinCart.output_cartfrom math import dist | ||||
134 | import math | ||||
135 | class pt3d: | 135 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
n | 137 | self.x=x | n | 137 | self.x = x |
138 | self.y=y | 138 | self.y = y | ||
139 | self.z=z | 139 | self.z = z | ||
140 | self.add_x=0 | ||||
141 | self.add_y=0 | ||||
142 | self.add_z=0 | ||||
143 | def __add__(self,jeff): | 140 | def __add__ (self,number): | ||
144 | add_x=self.x+jeff.x | 141 | x = self.x + number.x | ||
145 | add_y=self.y+jeff.y | 142 | y = self.y + number.y | ||
146 | add_z=self.z+jeff.z | 143 | z = self.z + number.z | ||
147 | return pt3d(add_x,add_y,add_z) | 144 | return pt3d(x,y,z) | ||
148 | def __sub__(self,jeff): | 145 | def __sub__ (self,number): | ||
149 | return math.sqrt(math.pow(self.x-jeff.x,2)+math.pow(self.y-jeff.y,2)+mat | 146 | return math.sqrt(math.pow(number.x - self.x, 2) + math.pow(number.y - se | ||
> | h.pow(self.z-jeff.z,2)) | > | lf.y, 2) + math.pow(number.z - self.z, 2)) | ||
150 | def __eq__(self,jeff): | 147 | def __eq__ (self,number): | ||
151 | if self.x==jeff.x and self.y==jeff.y and self.z==jeff.z: | 148 | if number.x == self.x: | ||
152 | return True | 149 | return True | ||
n | n | 150 | elif number.y == self.y: | ||
151 | return True | ||||
152 | elif number.z == self.z: | ||||
153 | return True | ||||
154 | else: | ||||
155 | return False | ||||
153 | def __str__(self): | 156 | def __str__ (self): | ||
154 | return f'<{self.x},{self.y},{self.z}>' | 157 | return f'<{self.x},{self.y},{self.z}>' | ||
155 | if __name__=='__main__': | 158 | if __name__=='__main__': | ||
156 | p1=pt3d(1,1,1) | 159 | p1=pt3d(1,1,1) | ||
157 | p2=pt3d(2,2,2) | 160 | p2=pt3d(2,2,2) | ||
t | 158 | print(p1+p2) | t | 161 | print(p1 + p2) |
159 | print(p1-p2) | 162 | print(p1 - p2) | ||
160 | print(p1==p2) | 163 | print(p1 == p2) | ||
161 | print(p1+p2==p2) | 164 | print((p1 + p1) == p2) | ||
162 | print(p1==p2+pt3d(-1,-1,-1)) | 165 | print(p1 == (p2 + pt3d(-1,-1,-1))) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,x=0): | n | 3 | def __init__(self,radius=0,): |
4 | self.x=x | 4 | self.radius=radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return (self.x**2)*math.pi | n | 6 | area=self.radius*self.radius*math.pi |
7 | return area | ||||
7 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 8 | return self.x*2*math.pi | n | 9 | perimeter=2*self.radius*math.pi |
10 | return perimeter | ||||
9 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
10 | x = int(input()) | 12 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 14 | def __init__(self,item_name='none',item_price=0,item_quantity=0,item_descrip | n | 16 | def init(self,item_name='none',item_price=0,item_quantity=0,item_description |
> | tion='none'): | > | ='none'): | ||
15 | self.item_name=item_name | 17 | self.item_name=item_name | ||
16 | self.item_price=item_price | 18 | self.item_price=item_price | ||
17 | self.item_quantity=item_quantity | 19 | self.item_quantity=item_quantity | ||
18 | self.item_description=item_description | 20 | self.item_description=item_description | ||
19 | def print_item_description(self): | 21 | def print_item_description(self): | ||
20 | return print(self.item_description) | 22 | return print(self.item_description) | ||
21 | def print_item_cost(self): | 23 | def print_item_cost(self): | ||
22 | totalCost=self.item_quantity*self.item_price | 24 | totalCost=self.item_quantity*self.item_price | ||
23 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | 25 | print('{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self. | ||
> | item_price,totalCost)) | > | item_price,totalCost)) | ||
24 | class ShoppingCart: | 26 | class ShoppingCart: | ||
25 | def __init__(self,customer_name='none',current_date='January 1, 2016',cart_i | 27 | def __init__(self,customer_name='none',current_date='January 1, 2016',cart_i | ||
> | tems=[]): | > | tems=[]): | ||
26 | self.customer_name=customer_name | 28 | self.customer_name=customer_name | ||
27 | self.current_date=current_date | 29 | self.current_date=current_date | ||
28 | self.cart_items=cart_items | 30 | self.cart_items=cart_items | ||
n | 29 | def add_item(self,ItemToPurchase): | n | 31 | def __add_item__(self,ItemToPurchase): |
30 | print('ADD ITEM TO CART',end='\n') | 32 | print('ADD ITEM TO CART',end='\n') | ||
31 | item_name=str(input('Enter the item name:\n')) | 33 | item_name=str(input('Enter the item name:\n')) | ||
32 | item_description=str(input('Enter the item description:\n')) | 34 | item_description=str(input('Enter the item description:\n')) | ||
33 | item_price=int(input('Enter the item price:\n')) | 35 | item_price=int(input('Enter the item price:\n')) | ||
34 | item_quantity=int(input('Enter the item quantity:\n')) | 36 | item_quantity=int(input('Enter the item quantity:\n')) | ||
35 | self.cart_items.append(ItemToPurchase(item_name,item_price,item_quantity | 37 | self.cart_items.append(ItemToPurchase(item_name,item_price,item_quantity | ||
> | ,item_description)) | > | ,item_description)) | ||
36 | def remove_item(self): | 38 | def remove_item(self): | ||
37 | print('REMOVE ITEM FROM CART\n') | 39 | print('REMOVE ITEM FROM CART\n') | ||
38 | item=str(input('Enter name of item to remove:\n')) | 40 | item=str(input('Enter name of item to remove:\n')) | ||
39 | i=0 | 41 | i=0 | ||
40 | for item in self.cart_items: | 42 | for item in self.cart_items: | ||
41 | if self.item_name == item: | 43 | if self.item_name == item: | ||
42 | self.cart_items.remove(item) | 44 | self.cart_items.remove(item) | ||
43 | i+=1 | 45 | i+=1 | ||
44 | delete=True | 46 | delete=True | ||
45 | break | 47 | break | ||
46 | else: | 48 | else: | ||
47 | delete=False | 49 | delete=False | ||
48 | if delete==False: | 50 | if delete==False: | ||
49 | print('Item not found in cart. Nothing removed.') | 51 | print('Item not found in cart. Nothing removed.') | ||
50 | def modify_item(self): | 52 | def modify_item(self): | ||
51 | print('CHANGE ITEM QUANTITY\n') | 53 | print('CHANGE ITEM QUANTITY\n') | ||
52 | name=str(input('Enter the item name:\n')) | 54 | name=str(input('Enter the item name:\n')) | ||
53 | for item in self.cart_items: | 55 | for item in self.cart_items: | ||
54 | if self.item_name == name: | 56 | if self.item_name == name: | ||
55 | quantity=int(input('Enter the new quantity:\n')) | 57 | quantity=int(input('Enter the new quantity:\n')) | ||
56 | item.item_quantity=quantity | 58 | item.item_quantity=quantity | ||
57 | mod = True | 59 | mod = True | ||
58 | break | 60 | break | ||
59 | else: | 61 | else: | ||
60 | mod=False | 62 | mod=False | ||
61 | if mod==False: | 63 | if mod==False: | ||
62 | print('Item not found in cart. Nothing modified.') | 64 | print('Item not found in cart. Nothing modified.') | ||
63 | def get_num_items_in_cart(self): | 65 | def get_num_items_in_cart(self): | ||
64 | total=0 | 66 | total=0 | ||
65 | for item in self.cart_items: | 67 | for item in self.cart_items: | ||
66 | total+=item.item_quantity | 68 | total+=item.item_quantity | ||
67 | return total | 69 | return total | ||
68 | def get_cost_of_cart(self): | 70 | def get_cost_of_cart(self): | ||
69 | totalc=0 | 71 | totalc=0 | ||
70 | for item in self.cart_items: | 72 | for item in self.cart_items: | ||
n | 71 | totalc+=(self.item_quantity*self.item_price) | n | 73 | totalc+=(self.item_quantityself.item_price) |
72 | return totalc | 74 | return totalc | ||
73 | def print_total(self,get_cost_of_cart,output_cart): | 75 | def print_total(self,get_cost_of_cart,output_cart): | ||
74 | totalcost=get_cost_of_cart() | 76 | totalcost=get_cost_of_cart() | ||
75 | if totalcost==0: | 77 | if totalcost==0: | ||
76 | print('SHOPPING CART IS EMPTY') | 78 | print('SHOPPING CART IS EMPTY') | ||
77 | else: | 79 | else: | ||
78 | output_cart() | 80 | output_cart() | ||
79 | def print_descriptions(self): | 81 | def print_descriptions(self): | ||
80 | print("OUTPUT ITEMS' DESCRIPTIONS\n") | 82 | print("OUTPUT ITEMS' DESCRIPTIONS\n") | ||
81 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | 83 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | ||
> | _date)) | > | _date)) | ||
82 | print('Item Descriptions\n') | 84 | print('Item Descriptions\n') | ||
83 | for item in self.cart_items: | 85 | for item in self.cart_items: | ||
84 | print('{}: {}\n'.format(item.item_name,item.item_description)) | 86 | print('{}: {}\n'.format(item.item_name,item.item_description)) | ||
85 | def output_cart(self): | 87 | def output_cart(self): | ||
86 | new=ShoppingCart() | 88 | new=ShoppingCart() | ||
87 | print('OUTPUT SHOPPING CART\n') | 89 | print('OUTPUT SHOPPING CART\n') | ||
88 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | 90 | print("{}'s Shopping Cart - {}\n".format(self.customer_name,self.current | ||
> | _date)) | > | _date)) | ||
89 | print('Number of Items: {}\n'.format(new.get_num_items_in_cart)) | 91 | print('Number of Items: {}\n'.format(new.get_num_items_in_cart)) | ||
90 | costtotal=0 | 92 | costtotal=0 | ||
91 | for item in self.cart_items: | 93 | for item in self.cart_items: | ||
92 | print('{} {} @ ${} = ${}\n'.format(item.item_name,item.item_quantity | 94 | print('{} {} @ ${} = ${}\n'.format(item.item_name,item.item_quantity | ||
> | ,item.item_price, | > | ,item.item_price, | ||
n | 93 | (item.item_quantity*item.item_price))) | n | 95 | (item.item_quantityitem.item_price))) |
94 | costtotal+=(item.item_quantity*item.item_price) | 96 | costtotal+=(item.item_quantity*item.item_price) | ||
95 | print('Total: ${}\n'.format(costtotal)) | 97 | print('Total: ${}\n'.format(costtotal)) | ||
96 | def print_menu(ShoppingCart): | 98 | def print_menu(ShoppingCart): | ||
97 | customer_Cart=newCart | 99 | customer_Cart=newCart | ||
98 | blank='' | 100 | blank='' | ||
99 | menu=('MENU\n' | 101 | menu=('MENU\n' | ||
100 | 'a - Add item to cart\n' | 102 | 'a - Add item to cart\n' | ||
101 | 'r - Remove item from cart\n' | 103 | 'r - Remove item from cart\n' | ||
102 | 'c - Change item quantity\n' | 104 | 'c - Change item quantity\n' | ||
103 | "i - Output items' descriptions\n" | 105 | "i - Output items' descriptions\n" | ||
104 | 'o - Output shopping cart\n' | 106 | 'o - Output shopping cart\n' | ||
105 | 'q - Quit\n') | 107 | 'q - Quit\n') | ||
106 | command='' | 108 | command='' | ||
107 | while command!='q': | 109 | while command!='q': | ||
n | 108 | blank='' | n | 110 | string='' |
109 | print(menu,end='\n') | 111 | print(menu,end='\n') | ||
110 | command = input('Choose an option:\n') | 112 | command = input('Choose an option:\n') | ||
n | 111 | while (command != 'a' and command != 'r' and command != 'c' and comm | n | 113 | while (command != 'a' and command != 'r' and command != 'c' and command |
> | and != 'i' and command != 'o' | > | != 'i' and command != 'o' and command != 'q'): | ||
112 | and command != 'q'): | ||||
113 | command = input('Choose an option:\n') | 114 | command = input('Choose an option:\n') | ||
114 | if command == 'a': | 115 | if command == 'a': | ||
115 | customer_Cart.add_item(blank) | 116 | customer_Cart.add_item(string) | ||
116 | if command == 'r': | 117 | if command == 'r': | ||
117 | customer_Cart.remove_item() | 118 | customer_Cart.remove_item() | ||
118 | if command == 'c': | 119 | if command == 'c': | ||
119 | customer_Cart.modify_item() | 120 | customer_Cart.modify_item() | ||
120 | if command == 'i': | 121 | if command == 'i': | ||
121 | customer_Cart.print_descriptions() | 122 | customer_Cart.print_descriptions() | ||
122 | if command == 'o': | 123 | if command == 'o': | ||
123 | customer_Cart.output_cart() | 124 | customer_Cart.output_cart() | ||
124 | if __name__=='__main__': | 125 | if __name__=='__main__': | ||
125 | itemdesc=ItemToPurchase() | 126 | itemdesc=ItemToPurchase() | ||
n | 126 | shopcart=ShoppingCart() | n | 127 | customer_name=str(input("Enter customer's name:\n")) |
127 | customer_name=str(input()) | 128 | current_date=str(input("Enter today's date:\n")) | ||
128 | current_date=str(input()) | ||||
129 | print("Enter customer's name:") | ||||
130 | print("Enter today's date:") | ||||
131 | print('\nCustomer name: {}'.format(customer_name)) | 129 | print('\nCustomer name: {}'.format(customer_name)) | ||
132 | print("Today's date: {}\n".format(current_date)) | 130 | print("Today's date: {}\n".format(current_date)) | ||
133 | newCart=ShoppingCart(customer_name,current_date) | 131 | newCart=ShoppingCart(customer_name,current_date) | ||
n | 134 | shopcart.print_menu()import math | n | 132 | newCart.print_menu |
133 | ShoppinCart.output_cartfrom math import dist | ||||
134 | import math | ||||
135 | class pt3d: | 135 | class pt3d: | ||
136 | def __init__(self,x=0,y=0,z=0): | 136 | def __init__(self,x=0,y=0,z=0): | ||
n | 137 | self.x = x | n | 137 | self.x=x |
138 | self.y = y | 138 | self.y=y | ||
139 | self.z = z | 139 | self.z=z | ||
140 | self.add_x=0 | ||||
141 | self.add_y=0 | ||||
142 | self.add_z=0 | ||||
140 | def __add__ (self,number): | 143 | def __add__(self,jeff): | ||
141 | x = self.x + number.x | 144 | add_x=self.x+jeff.x | ||
142 | y = self.y + number.y | 145 | add_y=self.y+jeff.y | ||
143 | z = self.z + number.z | 146 | add_z=self.z+jeff.z | ||
144 | return pt3d(x,y,z) | 147 | return pt3d(add_x,add_y,add_z) | ||
145 | def __sub__ (self,number): | 148 | def __sub__(self,jeff): | ||
146 | return math.sqrt(math.pow(number.x - self.x, 2) + math.pow(number.y - se | 149 | return math.sqrt(math.pow(self.x-jeff.x,2)+math.pow(self.y-jeff.y,2)+mat | ||
> | lf.y, 2) + math.pow(number.z - self.z, 2)) | > | h.pow(self.z-jeff.z,2)) | ||
147 | def __eq__ (self,number): | 150 | def __eq__(self,jeff): | ||
148 | if number.x == self.x: | 151 | if self.x==jeff.x and self.y==jeff.y and self.z==jeff.z: | ||
149 | return True | 152 | return True | ||
n | 150 | elif number.y == self.y: | n | ||
151 | return True | ||||
152 | elif number.z == self.z: | ||||
153 | return True | ||||
154 | else: | ||||
155 | return False | ||||
156 | def __str__ (self): | 153 | def __str__(self): | ||
157 | return f'<{self.x},{self.y},{self.z}>' | 154 | return f'<{self.x},{self.y},{self.z}>' | ||
158 | if __name__=='__main__': | 155 | if __name__=='__main__': | ||
159 | p1=pt3d(1,1,1) | 156 | p1=pt3d(1,1,1) | ||
160 | p2=pt3d(2,2,2) | 157 | p2=pt3d(2,2,2) | ||
t | 161 | print(p1 + p2) | t | 158 | print(p1+p2) |
162 | print(p1 - p2) | 159 | print(p1-p2) | ||
163 | print(p1 == p2) | 160 | print(p1==p2) | ||
164 | print((p1 + p1) == p2) | 161 | print(p1+p2==p2) | ||
165 | print(p1 == (p2 + pt3d(-1,-1,-1))) | 162 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,x): | n | 3 | def __init__(self,radius): |
4 | self.x=x | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | return math.pi*self.x*self.x | n | 6 | return math.pi*(self.radius**2) |
7 | def perimeter(self): | 7 | def perimeter(self): | ||
n | 8 | return math.pi*self.x*2 | n | 8 | return 2*math.pi*self.radius |
9 | if __name__=='__main__': | 9 | if __name__=='__main__': | ||
10 | x = int(input()) | 10 | x = int(input()) | ||
11 | NewCircle = Circle(x) | 11 | NewCircle = Circle(x) | ||
12 | print('{:.5f}'.format(NewCircle.area())) | 12 | print('{:.5f}'.format(NewCircle.area())) | ||
13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 13 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
n | 14 | def __init__(self) -> None: | n | 14 | def __init__(self): |
15 | self.item_description='none' | ||||
16 | self.item_name='none' | 15 | self.item_name='none' | ||
17 | self.item_price=0 | 16 | self.item_price=0 | ||
18 | self.item_quantity=0 | 17 | self.item_quantity=0 | ||
19 | def print_item_description(self, ItemToPurchase): | ||||
20 | return self.item_description | 18 | self.item_description = 'none' | ||
21 | def print_item_cost(self): | 19 | def print_item_cost(self): | ||
22 | totalPrice = self.item_price*self.item_quantity | 20 | totalCost = self.item_price * self.item_quantity | ||
23 | outStr = '{} {} @ ${} = ${}'.format(self.item_name,self.item_quantity,self | 21 | print('{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, sel | ||
> | .item_price,totalPrice) | > | f.item_price, totalCost)) | ||
24 | return outStr | 22 | def print_item_description(self): | ||
23 | print(f'{self.item_name}: {self.item_description}') | ||||
25 | class ShoppingCart: | 24 | class ShoppingCart: | ||
n | 26 | def __init__(self, customer_name='none', current_date='January 1, 2016'): | n | 25 | def __init__(self,customer_name='none', current_date="January 1, 2016", cart |
> | _items=[]): | ||||
27 | self.customer_name=customer_name | 26 | self.customer_name = customer_name | ||
28 | self.current_date=current_date | 27 | self.current_date = current_date | ||
29 | self.cart_items=[] | 28 | self.cart_items = cart_items | ||
30 | def add_item(self, Item): | 29 | def add_item(self,item_name=ItemToPurchase()): | ||
30 | self.item_name = item_name | ||||
31 | self.cart_items.append(Item) | 31 | self.cart_items.append(self.item_name) | ||
32 | def remove_item(self, item_name): | 32 | def remove_item(self,item_name=ItemToPurchase()): | ||
33 | self.item_name = item_name | ||||
33 | if item_name in self.cart_items: | 34 | if self.item_name in self.cart_items: | ||
34 | self.cart_items.remove(item_name) | 35 | self.cart_items.remove(self.item_name) | ||
35 | else: | 36 | else: | ||
37 | return "Item not found in cart. Nothing removed." | ||||
38 | def modify_item(self,item_quantity = ItemToPurchase(),item_name = ItemToPurc | ||||
> | hase(), new_item_quantity = ItemToPurchase()): | ||||
39 | self.item_quantity = item_quantity | ||||
40 | self.item_name = item_name | ||||
41 | self.new_item_quantity = new_item_quantity | ||||
42 | if self.item_name in self.cart_items: | ||||
43 | self.item_quantity = self.new_item_quantity | ||||
44 | else: | ||||
36 | print('Item not found in cart. Nothing removed.') | 45 | print('Item not found in cart. Nothing modified.') | ||
37 | def get_num_items_in_cart(self): | 46 | def get_num_items_in_cart(self): | ||
38 | c=0 | 47 | num_items = 0 | ||
39 | for k in self.cart_items: | 48 | for x in self.cart_items: | ||
40 | c=c+k.item_quantity | 49 | num_items = num_items+x.item_quantity | ||
41 | return c | 50 | return num_items | ||
42 | def get_cost_of_cart(self): | 51 | def get_cost_of_cart(self): | ||
43 | cost=0 | 52 | total = 0 | ||
53 | item_cost = 0 | ||||
44 | for k in self.cart_items: | 54 | for i in self.cart_items: | ||
45 | cost = cost + k.item_price*k.item_quantity | 55 | item_cost = (i.item_quantity * i.item_price) | ||
56 | total+=item_cost | ||||
46 | return cost | 57 | return total | ||
58 | def print_total(self): | ||||
59 | print(f'{self.customer_name}\'s Shopping Cart - {self.current_date}') | ||||
60 | print(f'Number of Items: {self.get_num_items_in_cart}') | ||||
61 | print() | ||||
62 | for x in self.cart_items: | ||||
63 | print(f'{x.item_name} {x.item_quantity} @ ${x.item_price} = ${x.item | ||||
> | _price*x.item_quantity}') | ||||
64 | print() | ||||
65 | print(f'Total: ${self.get_cost_of_cart}') | ||||
66 | def print_descriptions(self): | ||||
67 | print(f'{self.customer_name}\'s Shopping Cart - {self.current_date}') | ||||
68 | print() | ||||
69 | print('Item Descriptions') | ||||
70 | for item in self.cart_items: | ||||
71 | print(f'{item.print_item_descripton()}') | ||||
47 | def print_menu(): | 72 | def print_menu(): | ||
n | 48 | print('MENU') | n | 73 | print('MENU') |
49 | print('a - Add item to cart') | 74 | print('a - Add item to cart') | ||
50 | print('r - Remove item from cart') | 75 | print('r - Remove item from cart') | ||
51 | print('c - Change item quantity') | 76 | print('c - Change item quantity') | ||
52 | print('i - Output items\' descriptions') | 77 | print('i - Output items\' descriptions') | ||
53 | print('o - Output shopping cart') | 78 | print('o - Output shopping cart') | ||
54 | print('q - Quit') | 79 | def execute_menu(self,character,shopping_cart): | ||
55 | def execute_menu(userOption, my_cart): | 80 | self.character = character | ||
56 | if userOption == 'o': | 81 | self.shopping_cart = shopping_cart | ||
82 | if character == 'q': | ||||
83 | quit | ||||
84 | elif character == 'a': | ||||
85 | shopping_cart.add_item() | ||||
86 | elif character == 'r': | ||||
87 | shopping_cart.remove_item() | ||||
88 | elif character == 'c': | ||||
89 | shopping_cart.modify_item() | ||||
90 | elif character == 'i': | ||||
91 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||||
92 | shopping_cart.print_descriptions() | ||||
93 | elif character == 'o': | ||||
57 | print('OUTPUT SHOPPING CART') | 94 | print('OUTPUT SHOPPING CART') | ||
58 | print('John Doe\'s Shopping Cart - February 1, 2016') | 95 | shopping_cart.print_total() | ||
96 | if __name__=="__main__": | ||||
97 | customer = ShoppingCart() | ||||
98 | print('Enter customer\'s name:') | ||||
99 | customer.customer_name = 'John Doe' | ||||
100 | print('Enter today\'s date:') | ||||
101 | customer.current_date = 'February 1, 2016' | ||||
59 | print() | 102 | print() | ||
n | 60 | tt = 0 | n | ||
61 | for k in my_cart: | ||||
62 | print('{} {} @ ${}) = $378'.format((k.item_name),(k.item_quantity),(k.it | ||||
> | em_price),(k.item_price*k.item_quantity))) | ||||
63 | tt = tt + (k.item_price*k.item_quantity) | ||||
64 | print('Total: ${})'.format(tt)) | ||||
65 | if userOption == 'i': | ||||
66 | print('OUTPUT ITEMS\' DESCRIPTIONS') | ||||
67 | print('{}\'s Shopping Cart - {}'.format((customer_name),(current_date))) | ||||
68 | print() | ||||
69 | print('Item Descriptions') | ||||
70 | for k in my_cart: | ||||
71 | print(ItemToPurchase.print_item_description()) | ||||
72 | if __name__=="__main__": | ||||
73 | print('Enter customer\'s name:') | ||||
74 | cmm = input() | ||||
75 | customer1 = ShoppingCart() | ||||
76 | customer1.customer_name=cmm | ||||
77 | print('Enter today\'s date:') | ||||
78 | dd = input() | ||||
79 | customer1.current_date=dd | ||||
80 | print() | ||||
81 | print('Customer name:',customer1.customer_name) | 103 | print(f'Customer name: {customer.customer_name}') | ||
82 | print('Today\'s date:',customer1.current_date) | 104 | print(f'Today\'s date: {customer.current_date}') | ||
83 | print() | ||||
84 | print_menu() | 105 | print_menu() | ||
85 | print() | 106 | print() | ||
86 | print('Choose an option:') | ||||
87 | opt1=input() | ||||
88 | print('Choose an option:') | ||||
89 | opt2=input() | ||||
90 | print('Choose an option:') | ||||
91 | opt3=input() | ||||
92 | import numpy as np | 107 | import numpy as np | ||
93 | class pt3d: | 108 | class pt3d: | ||
t | 94 | def __init__(self,x=0,y=0,z=0): | t | 109 | def __init__(self,x=0,y=0,z=0): |
95 | self.x=x | 110 | self.x = x | ||
96 | self.y=y | 111 | self.y = y | ||
97 | self.z=z | 112 | self.z = z | ||
98 | def __str__(self): | 113 | def __str__(self): | ||
99 | return '<{},{},{}>'.format((self.x),(self.y),(self.z)) | 114 | return (f'<{self.x},{self.y},{self.z}>') | ||
100 | def __add__(self, other): | 115 | def __add__(self,other): | ||
101 | p3 = pt3d((self.x + other.x),( self.y + other.y),(self.z + other.z)) | 116 | sum_x = self.x + other.x | ||
102 | return p3 | 117 | sum_y = self.y + other.y | ||
118 | sum_z = self.z + other.z | ||||
119 | return (f'<{sum_x},{sum_y},{sum_z}>') | ||||
120 | def __sub__(self,other): | ||||
121 | a = np.array((self.x, self.y, self.z)) | ||||
122 | b = np.array((other.x, other.y, other.z)) | ||||
123 | return (np.sqrt(np.sum(np.square(a-b)))) | ||||
103 | def __e__(self, other): | 124 | def __eq__(self,other): | ||
104 | if (self.x==other.x and self.y==other.y and self.z==other.z): | 125 | if (self.x == other.x) and (self.y == other.y) and (self.z == other.z): | ||
105 | return True | 126 | return True | ||
127 | else: | ||||
106 | return False | 128 | return False | ||
107 | def __sub__(self, other): | 129 | if __name__ == "__main__": | ||
108 | po_1 = np.array((self.x, self.y, self.z)) | 130 | p1=pt3d(1,1,1) | ||
109 | po_2 = np.array((other.x, other.y, other.z)) | 131 | print(p1) | ||
110 | instance = np.sqrt(np.sum(np.square(po_1 - po_2))) | 132 | p2=pt3d(2,2,2) | ||
111 | return instance | 133 | print(p1+p2) | ||
134 | print(p1-p2) | ||||
135 | print(p1==p2) | ||||
136 | print(p1+p1==p2) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | class Circle: | 2 | class Circle: | ||
n | 3 | def __init__(self,radius): | n | 3 | def __init__(self, radius): |
4 | self.radius=radius | 4 | self.radius = radius | ||
5 | def area(self): | 5 | def area(self): | ||
n | 6 | self.area= math.pi * (self.radius**2) | n | 6 | area = ((self.radius)**2)*(math.pi) |
7 | return self.area | 7 | return area | ||
8 | def perimeter(self): | 8 | def perimeter(self): | ||
n | 9 | self.perimeter= 2 *math.pi * self.radius | n | 9 | perimeter = 2*(math.pi)*(self.radius) |
10 | return self.perimeter | 10 | return perimeter | ||
11 | if __name__=='__main__': | 11 | if __name__=='__main__': | ||
12 | x = int(input()) | 12 | x = int(input()) | ||
13 | NewCircle = Circle(x) | 13 | NewCircle = Circle(x) | ||
14 | print('{:.5f}'.format(NewCircle.area())) | 14 | print('{:.5f}'.format(NewCircle.area())) | ||
15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | 15 | print('{:.5f}'.format(NewCircle.perimeter()))class ItemToPurchase: | ||
16 | def __init__(self): | 16 | def __init__(self): | ||
17 | self.item_name='none' | 17 | self.item_name='none' | ||
18 | self.item_price=0 | 18 | self.item_price=0 | ||
19 | self.item_quantity=0 | 19 | self.item_quantity=0 | ||
n | 20 | self.item_description="none" | n | 20 | self.item_description ='none' |
21 | def get_total(self): | ||||
22 | self_total=self.item_price*self.item_quantity | ||||
23 | return self_total | ||||
24 | def get_discription(self): | 21 | def print_item_description(self): | ||
25 | return self.item_description | 22 | print('{}: {}.'.format(self.item_name, self.item_description)) | ||
23 | def print_item_cost(self): | ||||
24 | totalCost = self.item_quantity*self.item_price | ||||
25 | print('{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, sel | ||||
> | f.item_price, totalCost)) | ||||
26 | class ShoppingCart: | 26 | class ShoppingCart: | ||
n | 27 | def __init__(self, name="none",date="January 1, 2016"): | n | 27 | def __init__(self, customer_name='none', current_date="January 1, 2016", car |
> | t_items = []): | ||||
28 | self.name=name | 28 | self.customer_name = customer_name | ||
29 | self.date=date | 29 | self.current_date = current_date | ||
30 | self.cart_items=[] | 30 | self.cart_items = cart_items | ||
31 | def add_items(self): | 31 | def add_item(self, ItemToPurchase): | ||
32 | self.cart_items.appened(self) | 32 | self.cart_items.append(ItemToPurchase) | ||
33 | def remove_items(string): | 33 | def remove_item(self, Item_name): | ||
34 | Item_name = str(input()) | ||||
34 | if string in self.cart_items: | 35 | for item in self.cart_items: | ||
35 | self.cart_items.remove(string) | 36 | if Item_name != item.item_name: | ||
36 | else: | ||||
37 | print("Item not found in cart. Nothing removed.") | 37 | print('Item not found in cart. Nothing removed.') | ||
38 | else: | ||||
39 | self.cart_items.remove(item) | ||||
38 | def modify_items(self,x): | 40 | def modify_item(self, Item_name): | ||
41 | Item_name = str(input()) | ||||
39 | if self in self.cart_items: | 42 | for item in self.cart_items: | ||
40 | self.item_quanity= x | 43 | if item.item_name != Item_name: | ||
44 | print('Item not found in cart. Nothing modified.') | ||||
45 | else: | ||||
46 | new_quantity = int(input()) | ||||
47 | item.item_quantity = new_quantity | ||||
48 | def get_num_items_in_cart(self): | ||||
49 | num_items = 0 | ||||
50 | for item in self.cart_items: | ||||
51 | num_items += item.item_quantity | ||||
52 | return num_items | ||||
53 | def get_cost_of_cart(self): | ||||
54 | total_cost = 0 | ||||
55 | for item in self.cart_items: | ||||
56 | total_cost += (item.item_quantity * item.item_price) | ||||
57 | return total_cost | ||||
58 | def print_menu(): | ||||
59 | print("MENU") | ||||
60 | print("a - Add item to cart") | ||||
61 | print("r - Remove item from cart") | ||||
62 | print("c - Change item quantity") | ||||
63 | print("i - Output items' descriptions") | ||||
64 | print("o - Output shopping cart") | ||||
65 | print("q - Quit\n") | ||||
66 | if __name__ == "__main__": | ||||
67 | customer_name = str(input("Enter customer's name:\n")) | ||||
68 | current_date = input("Enter today's date:\n") | ||||
69 | print() | ||||
70 | print("Customer name: {}".format(customer_name)) | ||||
71 | print("Today's date: {}".format(current_date)) | ||||
72 | print_menu()import math | ||||
73 | class pt3d: | ||||
74 | def __init__(self, x=0, y=0, z=0): | ||||
75 | self.x = x | ||||
76 | self.y = y | ||||
77 | self.z = z | ||||
78 | def __add__(self, other): | ||||
79 | ab_x = self.x + other.x | ||||
80 | ab_y = self.y + other.y | ||||
81 | ab_z = self.z + other.z | ||||
82 | return pt3d(ab_x, ab_y, ab_z) | ||||
83 | def __sub__(self, other): | ||||
84 | equation = (self.x-other.x)**2 + (self.y-other.y)**2 + (self.z - other.z | ||||
> | )**2 | ||||
85 | euclidean_distance = math.sqrt(equation) | ||||
86 | return euclidean_distance | ||||
87 | def __eq__(self, other): | ||||
88 | if (self.x != other.x) or (self.y != other.y) or (self.z != other.z): | ||||
89 | return False | ||||
41 | else: | 90 | else: | ||
n | 42 | print('Item not found in cart. Nothing modified.') | n | ||
43 | def get_num_items_in_cart(): | ||||
44 | return len(self.cart_items) | ||||
45 | def get_cost_of_cart(): | ||||
46 | t=0 | ||||
47 | for n in self.cart_items: | ||||
48 | t+=get_total(n) | ||||
49 | return t | 91 | return True | ||
50 | def print_total(self): | ||||
51 | print(get_num_items_in_cart(self)) | ||||
52 | def print_descriptions(): | ||||
53 | for n in self.cart_items: | ||||
54 | print(get_description(n)) | ||||
55 | def print_menu(): | ||||
56 | print('MENU') | ||||
57 | print("a - Add item to cart") | ||||
58 | print('r - Remove item from cart') | ||||
59 | print('c - Change item quantity') | ||||
60 | print('i - Output items\' descriptions') | ||||
61 | print('o - Output shopping cart') | ||||
62 | print('q - Quit') | ||||
63 | return print() | ||||
64 | def execute_menu(x,y): | ||||
65 | if x=="q": | ||||
66 | print() | ||||
67 | while x!="q": | ||||
68 | if x=="a": | ||||
69 | add_items(y) | ||||
70 | elif x=='r': | ||||
71 | remove_items(y) | ||||
72 | elif x=='c': | ||||
73 | modify_items(y) | ||||
74 | elif x=='i': | ||||
75 | print-discriptions(y) | ||||
76 | elif x=='o': | ||||
77 | print(y.cart_list) | ||||
78 | else: | ||||
79 | print("Choose an option:") | ||||
80 | break | ||||
81 | if __name__ == "__main__": | ||||
82 | print('Enter customer\'s name:') | ||||
83 | name=input() | ||||
84 | print("Enter today\'s date:") | ||||
85 | date=input() | ||||
86 | print() | ||||
87 | print("Customer name:", name) | ||||
88 | print("Today's date:" , date) | ||||
89 | print() | ||||
90 | print(print_menu()) | ||||
91 | print() | ||||
92 | print("Choose an option:") | ||||
93 | choice=input('hello') | ||||
94 | execute_menu(choice,name)class pt3d: | ||||
95 | def __init__(self, x=0, y=0, z=0): | ||||
96 | self.x= x | ||||
97 | self.y= y | ||||
98 | self.z= z | ||||
99 | def __add__(self,other_number): | ||||
100 | return pt3d(self.x+other_number.x,self.y+other_number.y,self.z+other_num | ||||
> | ber.z) | ||||
101 | def __sub__(self,other_number): | ||||
102 | return ((((other_number.x - self.x)**2) + ((other_number.y-self.y)** 2)+ | ||||
> | ((other_number.z-self.z)**2))**0.5) | ||||
103 | def __eq__(self, other_number): | ||||
104 | return(self.x == other_number.x) | ||||
105 | return(self.y == other_number.y) | ||||
106 | return(self.z == other_number.z) | ||||
107 | def __str__(self): | 92 | def __str__(self): | ||
t | 108 | return (f'<{self.x},{self.y},{self.z}>') | t | 93 | return('<{},{},{}>'.format(self.x, self.y, self.z)) |
109 | if __name__ == '__main__': | ||||
110 | p1 = pt3d(1, 1, 1) | ||||
111 | p2 = pt3d(2, 2, 2) | ||||
112 | print(p1+p2) | ||||
113 | print(p1-p2) | ||||
114 | print(p1==p2) | ||||
115 | print(p1+p1==p2) | ||||
116 | print(p1==p2+pt3d(-1,-1,-1)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|