網上購物需要支付商品錢數加運費。假設某網上書店與快遞公司簽訂了快遞費用,每件5元。對於買家,商家設定在購買金額超過一定限額時(100)免運費

君子令儀發表於2020-10-18
class Book:
    prices={"A":45,"B":56,"C":67,"D":78,"E":90.1}
    shipping=5
    def __init__(self,book_name,num,free_ship):#書名,數量,免運費金額 
        self.book_name=book_name
        self.num=num
        self.free_ship=free_ship
    def totals(self):
        price=Book.prices.get(self.book_name)
        if price :
            t=price*self.num
            return(t+Book.shipping) if t<self.free_ship else t
        return "no this book"
book_a=Book("A",2,100)
a_total=book_a.totals()
print(a_total)

輸出:95

相關文章