Python代码求解

小码君想使用这个商品类去管理店里的铅笔数量,并将数量的变化情况写入一个 txt 文件中。

代码如下,如果有帮助,请点击一下采纳谢谢:

class Sku():
    def __init__(self, name ,count):
        self.name = name
        self.count = count
    def add(self):
        self.count += 1
        with open('铅笔.txt', 'a') as f:
            f.write(str(self.count) + '\n')
    def sell(self):
        self.count -= 1
        with open('铅笔.txt', 'a') as f:
            f.write(str(self.count) + '\n')


sku = Sku('铅笔', 100)

class Sku():
    def __init__(self,count):
        self.count = count
        with open('铅笔.txt', 'w') as f:
            f.write(str(self.count) + '\n')
    def add(self,count):
        self.count =self.count+count
        with open('铅笔.txt', 'w') as f:
            f.write(str(self.count) + '\n')
    def sell(self,count):
        self.count = self.count-count
        with open('铅笔.txt', 'w') as f:
            f.write(str(self.count) + '\n')
    def read(self):
        with open('铅笔.txt', 'r') as f:
            count = f.read()
            print(f"铅笔的数量是{count}")
 
 
sku = Sku(100)  #初始化
sku.read() #读取
sku.add(2)
sku.read() #读取
sku.sell(100)
sku.read() #读取