TypeError:';的操作数类型不受支持;浮动';和';非类型';用Python

TypeError:';的操作数类型不受支持;浮动';和';非类型';用Python,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,我正在用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__(

我正在用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))
#-*-编码:utf-8-*-
"""
创建于2020年9月2日星期三15:22:31
"""
从集合导入namedtuple
Customer=namedtuple('Customer','name-fidelity')
类别行项目:
定义初始(自身、产品、数量、价格):
self.product=产品
self.quantity=数量
self.price=价格
def总计(自身):
返回自制价格*自制数量
课程顺序:
定义初始化(自我、客户、购物车、促销=无):
self.customer=客户
self.cart=列表(购物车)
自我推销
def总计(自身):
如果不是hasattr(self,“\uu total”):
self.\uuu total=sum(self.cart中项目的item.total())
返回自我。\u总计
def到期(自我):
如果自我提升为无:
折扣=0
其他:
折扣=自我促销(自我)
返回self.total()-折扣
定义报告(自我):
fmt=“”
返回str(fmt.format(self.total(),self.due())
def fidelity_促销(订单):
如果order.customer.fidelity>=1000:
退货订单.total()*.05
乔=客户('John Doe',0)
安=客户(“安·史密斯”,1100)
购物车=[LineItem('banana',4,.5),
行项目('apple',10,1.5)]
打印(订单(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'

文件“C:/Users/hanlulu/Desktop/Python/chapter06-285.py”,第59行,在
打印(订单(joe、cart、fidelity_promo))
文件“C:/Users/hanlulu/Desktop/Python/chapter06-285.py”,第46行,在__
返回str(fmt.format(self.total(),self.due())
文件“C:/Users/hanlulu/Desktop/Python/chapter06-285.py”,第42行,到期
返回self.total()-折扣
TypeError:-:“float”和“NoneType”的操作数类型不受支持
如何解决它?

第38行说明:

discount = self.promotion(self)
其中,
self.promotion
用函数
fidelity\u promo
初始化

但是在该函数中,if子句没有进入,我们返回
None

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

# -*- 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))
#-*-编码:utf-8-*-
"""
创建于2020年9月2日星期三15:22:31
"""
从集合导入namedtuple
Customer=namedtuple('Customer','name-fidelity')
类别行项目:
定义初始(自身、产品、数量、价格):
self.product=产品
self.quantity=数量
self.price=价格
def总计(自身):
返回自制价格*自制数量
课程顺序:
定义初始化(自我、客户、购物车、促销=无):
self.customer=客户
self.cart=列表(购物车)
自我推销
def总计(自身):
如果不是hasattr(self,“\uu total”):
self.\uuu total=sum(self.cart中项目的item.total())
返回自我。\u总计
def到期(自我):
如果自我提升为无:
折扣=0
其他:
折扣=自我促销(自我)
返回self.total()-折扣
定义报告(自我):
fmt=“”
返回str(fmt.format(self.total(),self.due())
def fidelity_促销(订单):
如果order.customer.fidelity>=1000:
退货订单.total()*.05
返回0
乔=客户('John Doe',0)
安=客户(“安·史密斯”,1100)
购物车=[LineItem('banana',4,.5),
行项目('apple',10,1.5)]
# 
打印(订单(joe、cart、fidelity_promo))

如果order.customer.fidelity<1000怎么办?然后
fidelity\u promo
返回
None
…@deceze♦ 谢谢我根据你的建议解决它。我是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
    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))