Search
Implement a class named Rectangle to store the coordinates of a rectangle given by its top-left point P1(x1, y1) and bottom-right point P2(x2, y2).
Rectangle
width()
height()
area
circumference
__str__
First, implement a class Product representing products to store in a fridge:
Product
name
qty
unit
inc(self, by)
by
use(self, qty)
__str__(self)
Then, implement a class Fridge to represent a collection of products (food) in a fridge.
Fridge
add(self, product_name, qty, unit)
inc(self, product_name, by)
use(self, product_name, qty)
if __name__ == "__main__": fridge = Fridge() fridge.add("tomatoes", 10, "pieces") fridge.add("onions", 3, "pieces") fridge.add("eggs", 12, "pieces") fridge.add("butter", 500, "grams") fridge.add("oil", 1, "liter") print(fridge) fridge.inc("onions", 8) print(fridge) fridge.use("butter", 200) print(fridge) fridge.use("butter", 500) print(fridge)
Fridge: ...10 pieces of tomatoes ...3 pieces of onions ...12 pieces of eggs ...500 grams of butter ...1 liter of oil Adding 8 pieces of onions. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...500 grams of butter ...1 liter of oil Using 200 grams of butter. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...300 grams of butter ...1 liter of oil Using 300 grams of butter. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...1 liter of oil
Create a class representing playing cards (standard 52-card deck). A card (object) is described with 2 pieces of information:
Notes:
J
Q
K
A
C
D
H
S
>>> print(Card('A', 'S')) Card: ace of spades >>> print(Card(2, 'D')) Card: 2 of diamonds >>> print(Card('K', 'H')) Card: king of hearts >>> print(Card(9, 'C')) Card: 9 of clubs
__repr__
__eq__
__lt__
.sort()
Required filename: 12_weekly_hw.py.
12_weekly_hw.py
class Card: def __init__(self, rank, suit): def __str__(self): def __repr__(self): return str(self) def __eq__(self, other): def __lt__(self, other): if __name__ == "__main__": cards = [] cards.append(Card('A', 'D')) cards.append(Card(10, 'S')) cards.append(Card('K', 'H')) cards.append(Card('A', 'C')) cards.append(Card(3, 'S')) cards.append(Card(3, 'D')) print(cards) cards.sort() print(cards)
[Card: ace of diamonds, Card: 10 of spades, Card: king of hearts, Card: ace of clubs, Card: 3 of spades, Card: 3 of diamonds] [Card: 3 of diamonds, Card: 3 of spades, Card: 10 of spades, Card: king of hearts, Card: ace of clubs, Card: ace of diamonds]