【问题标题】:Python "Undefined variable 'flavours" pylint [closed]Python“未定义变量'口味”pylint [关闭]
【发布时间】:2020-08-02 21:26:53
【问题描述】:

这段代码有问题。 Pylint 说“风味是一个未定义的变量”,但我已经在继承自 Restaurant 类的 init 中声明了它?有人可以帮忙吗?我错过了什么?

class Restaurant:
""" A class representing a restaurant """
def __init__(self, restaurant_name, cuisine_type):
    """ Initialize the restaurant """
    self.restaurant_name = restaurant_name
    self.cuisine_type = cuisine_type

def describe_restaurant(self):
    """ A method to print the basic information about the restaurant """
    print (f"{self.restaurant_name}")
    print (f"{self.cuisine_type}")

def open_restaurant(self):
    """ Prints a message saying the restaurant is open """
    print (f"{self.restaurant_name} is open!")

class IceCreamStand(Restaurant):
    """ A class representing an ice cream stand. Inherits attributes from the Restaurant class """

    def __init__(self, restaurant_name, cuisine_type="Ice Cream"):
        """ Initialize an ice cream stand ""
        super().__init__(restaurant_name, cuisine_type)
        self.flavours = []

    def display_flavours(self):
        """ Display flavours available """
        print (f"\nThe current flavour offerings are: ")
        for flavour in self.flavours:
            print("- " + flavours.title())

 icecreamstand1 = IceCreamStand("Joel's Ice Cream")
 icecreamstand1.flavours = ["Mint", "Chocolate", "Vanilla"]

 icecreamstand1.describe_restaurant()
 icecreamstand1.display_flavours()

【问题讨论】:

  • 请修复代码中的 cmets,并向我们显示它给您错误的行。
  • 是在抱怨print("- " + flavours.title())吗?那应该是flavour.title()
  • 如果这是您的实际代码,它根本不会运行。您没有正确关闭 IceCreamStand.__init__ 的文档字符串。
  • 没有 pylint 报告问题所在的行吗?来自 pylint 的实际错误消息帮助我们找出问题所在。由于 pylint 往往很健谈,您可以将其修剪到相关的一点,但是,您仍然让我们寻找已经在错误消息中的内容。

标签: python list class methods


【解决方案1】:

你的 for 循环中有错字:

for flavour in self.flavours:
            print("- " + flavour.title())

上面写着flavours.title()

【讨论】:

  • 啊,我真笨!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
相关资源
最近更新 更多