मैं Python का उपयोग करके एक प्रोग्राम लिख रहा हूं। मेरा कार्यक्रम निम्नलिखित है:

# -*- coding: utf-8 -*-
"""
Created on Wed Sep  2 15:22:31 2020
"""

from collections import namedtuple

Customer = namedtuple('Customer', 'name fidelity')

class LineItem:
    
    def __init__(self, product, quantity, price):
        self.product = product
        self.quantity = quantity
        self.price = price
        
    def total(self):
        return self.price * self.quantity
    
    
class Order:
    def __init__(self, customer, cart, promotion=None):
        self.customer = customer
        self.cart = list(cart)
        self.promotion = promotion
        
    def total(self):
        if not hasattr(self, '__total'):
            self.__total = sum(item.total() for item in self.cart)
            
        return self.__total
    
    def due(self):
        if self.promotion is None:
            discount = 0
        else:
            discount = self.promotion(self)
        return self.total() - discount

    def __repr__(self):
        fmt = '<Order total:{:.2f} due: {:.2f}>'
        return str(fmt.format(self.total(), self.due()))
    

def fidelity_promo(order):
    if order.customer.fidelity >= 1000:
        return order.total() * .05
    

joe = Customer('John Doe', 0)
ann = Customer('Ann Smith', 1100)
cart = [LineItem('banana', 4, .5),
        LineItem('apple', 10,1.5)]

print(Order(joe, cart, fidelity_promo))

हालांकि, जब मैं इसे चलाता हूं, तो यह हमेशा एक त्रुटि दिखाता है:


  File "C:/Users/hanlulu/Desktop/Python/chapter06-285.py", line 59, in <module>
    print(Order(joe, cart, fidelity_promo))

  File "C:/Users/hanlulu/Desktop/Python/chapter06-285.py", line 46, in __repr__
    return str(fmt.format(self.total(), self.due()))

  File "C:/Users/hanlulu/Desktop/Python/chapter06-285.py", line 42, in due
    return self.total() - discount

TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

इसे कैसे हल करें?

-2
Land 2 सितंबर 2020, 10:48

1 उत्तर

सबसे बढ़िया उत्तर

लाइन 38 कहता है:

discount = self.promotion(self)

जहां self.promotion को फंक्शन fidelity_promo के साथ इनिशियलाइज़ किया जाता है।

फिर भी उस फ़ंक्शन में, अगर क्लॉज नहीं मिलता है और हम None वापस कर देते हैं:

def fidelity_promo(order):
    # We don't get in :/
    if order.customer.fidelity >= 1000:
        return order.total() * .05
    # <-- We reach here, return None

हमें दोनों मामलों को संभालना चाहिए:

def fidelity_promo(order):
    if order.customer.fidelity >= 1000:
        return order.total() * .05
    return 0

कोड फिक्स:

# -*- coding: utf-8 -*-
"""
Created on Wed Sep  2 15:22:31 2020
"""

from collections import namedtuple

Customer = namedtuple('Customer', 'name fidelity')


class LineItem:

    def __init__(self, product, quantity, price):
        self.product = product
        self.quantity = quantity
        self.price = price

    def total(self):
        return self.price * self.quantity


class Order:
    def __init__(self, customer, cart, promotion=None):
        self.customer = customer
        self.cart = list(cart)
        self.promotion = promotion

    def total(self):
        if not hasattr(self, '__total'):
            self.__total = sum(item.total() for item in self.cart)

        return self.__total

    def due(self):
        if self.promotion is None:
            discount = 0
        else:
            discount = self.promotion(self)
        return self.total() - discount

    def __repr__(self):
        fmt = '<Order total:{:.2f} due: {:.2f}>'
        return str(fmt.format(self.total(), self.due()))


def fidelity_promo(order):
    if order.customer.fidelity >= 1000:
        return order.total() * .05
    return 0


joe = Customer('John Doe', 0)
ann = Customer('Ann Smith', 1100)
cart = [LineItem('banana', 4, .5),
        LineItem('apple', 10, 1.5)]

# <Order total:17.00 due: 0.00>
print(Order(joe, cart, fidelity_promo))
0
Aviv Yaniv 2 सितंबर 2020, 07:57