首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError:未定义名称"“

NameError:未定义名称"“
EN

Stack Overflow用户
提问于 2021-06-06 16:11:21
回答 2查看 1.7K关注 0票数 1

下面的代码应该将customer对象的描述方法返回的值打印到没有的terminal...but。问题似乎是NameError:没有定义名称‘价格’。

代码语言:javascript
复制
class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self, age):
        
        ticket_price = 0
        price = ticket_price
        
        if self.age < 12:
            price = ticket_price + 0
        elif self.age < 18:
            price = ticket_price + 15
        elif self.age < 60:
            price = ticket_price + 20
        elif self.age >= 60:
            price = ticket_price + 10
        return price

class Customer(TicketMixin):
    """ Create instance of Customer """
    def __init__(self, name, age,):
        self.name = name
        self.age = age
    
    def describe(self):
        return f"{self.name} age {self.age} ticket price is {price}"
        
customer = Customer("Ryan Phillips", 22)
print(customer.describe())

谁能告诉我错过了什么吗?

EN

回答 2

Stack Overflow用户

发布于 2021-06-06 16:17:15

你没给calculate_ticket_price打电话。

代码语言:javascript
复制
def describe(self):
    return f"{self.name} age {self.age} ticket price is {self.calculate_ticket_price(self.age)}"

请注意,calculate_ticket_price可以采用age参数,在这种情况下,它不需要假设存在self.age

代码语言:javascript
复制
class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self, age):
        
        ticket_price = 0
        price = ticket_price
        
        if age < 12:
            price = ticket_price + 0
        elif age < 18:
            price = ticket_price + 15
        elif age < 60:
            price = ticket_price + 20
        elif age >= 60:
            price = ticket_price + 10
        return price

或者,您可以做这个假设,完全去掉age参数:

代码语言:javascript
复制
class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self):
        
        ticket_price = 0
        price = ticket_price
        
        if self.age < 12:
            price = ticket_price + 0
        elif self.age < 18:
            price = ticket_price + 15
        elif self.age < 60:
            price = ticket_price + 20
        elif self.age >= 60:
            price = ticket_price + 10
        return price

然后,describe的主体将简单地省略任何显式的参数:

代码语言:javascript
复制
def describe(self):
    return f"{self.name} age {self.age} ticket price is {self.calculate_ticket_price()}"

在前者中,请注意定义中根本不使用self;这意味着它应该是一个静态方法,或者仅仅是一个Customer可以使用的常规函数,而不需要任何混合类。

票数 2
EN

Stack Overflow用户

发布于 2021-06-06 16:23:38

切普纳是对的。您必须在您的calculate_ticket_price init函数中调用。正确的语法是使用Super关键字。

示例:

代码语言:javascript
复制
class Customer(TicketMixin):
    """ Create instance of Customer """
    def __init__(self, name, age,):
        self.name = name
        self.age = age
        self.price = super().calculate_ticket_price(self.age)
    
    def describe(self):
        return f"{self.name} age {self.age} ticket price is {self.price}"

您可以通过单击这里来了解有关超级关键字的更多信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67861189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档