Modified card game and new example class

This commit is contained in:
Donald Calloway 2025-09-22 11:46:21 -07:00
parent fb0db229c2
commit 8c5914826b
2 changed files with 37 additions and 1 deletions

View File

@ -225,7 +225,7 @@ def reset_round(players):
class Deck:
def __init__(self):
self.cards: List[Card] = [Card(rank, suit) for suit in SUITS for rank in RANKS]
random.seed(time.time())
random.seed(time.time()) # Seed random number generator with current time
random.shuffle(self.cards)
def deal_card(self) -> Card:

36
MyProductClass.py Normal file
View File

@ -0,0 +1,36 @@
class Product:
pay_rate = 0.8 # Class variable for a standard discount of 20%
def __init__(self, name, price, quantity):
assert price >= 0, f"Product price {price} is not greater than or equal to zero!"
assert quantity >= 0, f"Product quantity {quantity} is not greater than or equal to zero!"
self.name = name
self.price = price
self.quantity = quantity
def calculate_total_price(self):
return self.price * self.quantity
def apply_discount(self):
return self.calculate_total_price() * (self.pay_rate)
# Example usage:
if __name__ == "__main__":
item1 = Product("Laptop", 1000, 2) # Item with standard discount
item2 = Product("Smartphone", 100, 3) # Item with special discount
print(f'Price of {item1.name}: ${item1.price:.2f}')
print(f'Price of {item1.name} after 20% discount: ${item1.price * Product.pay_rate:.2f}')
print(f"Total price for {item1.name}(s) after 20% discount: ${item1.apply_discount():.2f}") # Applying standard discount
print()
item2.pay_rate = 0.7 # Instance variable for a special discount of 30%
print(f'Price of {item2.name}: ${item2.price:.2f}')
print(f'Price of {item2.name}(s) after 30% discount: ${item2.price * item2.pay_rate:.2f}')
print(f"Total price for {item2.name}(s) after 30% discount: ${item2.apply_discount():.2f}") # Applying item-specific discount