Jupyter 无法识别类声明

时间:2021-06-29 03:35:15

标签: python jupyter-notebook

在 Jupyter 单元中,我有这个类:

class Shirt:

    def __init__(self, shirt_color, shirt_size, shirt_style, shirt_price):
        self.color = shirt_color
        self.size = shirt_size
        self.style = shirt_style
        self.price = shirt_price
    
    def change_price(self, new_price):
    
        self.price = new_price
        
    def discount(self, discount):

        return self.price * (1 - discount)

当我这样做时:

import Shirt as shirt

shirt.color = 'red'
shirt.size = 'S'
shirt.style = 'long-sleeve'
shirt.price = 25

我收到此错误:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-d95b669ecd57> in <module>()
----> 5 import Shirt as shirt
      6 
      7 shirt.color = 'red'

ModuleNotFoundError: No module named 'Shirt'

我正在运行声明 Shirt 类的单元格,所以我不知道为什么它无法识别它。

1 个答案:

答案 0 :(得分:0)

这不是您创建对象的方式。你必须这样做

shirt = Shirt('red', 'S', 'long-sleeve', 25)