从Python中的现有列表中添加并删除字符串

发布于 2025-02-06 23:11:06 字数 3051 浏览 0 评论 0原文

我正在做一个功课,而我正在添加或删除汽车和买家的清单。我已经解决了汽车的一部分,但是我在添加买家方面遇到了麻烦。我希望将买家添加到列表中,并选择删除。我已经部分解决了添加问题,但是,每当我尝试删除买家时,它似乎行不通。

这是我的代码:

import json
import os.path

# Class
class Inventory:
    cars  = {}
    buyers = {}

    def __init__ (self):
        self.load()

    def add(self,key,qty):
        q = 0
        if key in self.cars:
            v = self.cars[key]
            q = v + qty
        else:
            q = qty
        self.cars[key] = q
        print(f'added {qty} {key}: total = {self.cars[key]}')
    
    def add2(self,buy):
        if buy in self.buyers:
            b = self.buyers[buy]
        else:
            b = buy
        self.buyers[buy] = b
        print(f'added {buy}')

    def remove2(self,buy):
        if buy in self.buyers:
            b = str(self.buyers[buy])
            
        else:
            b = buy
        self.buyers[buy] = b
        print(f'removed {buy}')

    def remove(self,key,qty):
        q = 0
        if key in self.cars:
            v = self.cars[key]
            q = v - qty
        if q < 0:
            q = 0
        self.cars[key] = q
        print(f'removed {qty} {key}: total = {self.cars[key]}')
    
    def display(self):
        for key, value in self.cars.items():
            print(f'{key} = {value}')
        for buy in self.buyers.items():
            print(f'{buy}')

    def save(self):
        print('saving information...')
        with open('inventory4.txt', 'w') as f:
            json.dump(self.cars,f)
            json.dump(self.buyers,f)
        print('information saved!')

    def load(self):
        print('loading information...')
        if not os.path.exists('inventory4.txt'):
            print('nothing to load!')
            return
        with open('inventory4.txt', 'r') as f:
            self.cars = json.load(f)
            self.buyers = json.load(f)
        print('Information Loaded!')

def main():
    inv = Inventory()

    while True:
        print('Write your choice...\n')
        action = input('Choice:\nadd car, remove car, add buyer, remove buyer, list, save, exit: ')
        if action == 'avsluta':
            break
        if action == 'add car' or action == 'remove car':
                key = input('Add/remove specific car: ')
                qty = int(input('Quantity: '))
                if action == 'add car':
                    inv.add(key,qty)
                if action == 'remove car':
                    inv.remove(key,qty)
        if action == 'add buyer' or action == 'remove buyer':
                buyers = input('Add/remove buyer: ')
                if action == 'add buyer':
                    inv.add2(buyers)
                if action == 'remove buyer':
                    inv.remove2(buyers)
            
        if action == 'list':
            print('____________________________________\n')
            inv.display()
            print('____________________________________\n')
        
        if action == 'save':
            inv.save
    inv.save()


if __name__ == "__main__":
    main()

I am doing a schoolwork whereas I am doing a list where you add or remove cars and buyers. I have solved the car part, however I have trouble with adding the buyers part. I want the buyers to be added to the list, and removed at choice. I have partially solved the adding issue, however, whenever I try to remove a buyer it doesn't seem to work.

This is my code:

import json
import os.path

# Class
class Inventory:
    cars  = {}
    buyers = {}

    def __init__ (self):
        self.load()

    def add(self,key,qty):
        q = 0
        if key in self.cars:
            v = self.cars[key]
            q = v + qty
        else:
            q = qty
        self.cars[key] = q
        print(f'added {qty} {key}: total = {self.cars[key]}')
    
    def add2(self,buy):
        if buy in self.buyers:
            b = self.buyers[buy]
        else:
            b = buy
        self.buyers[buy] = b
        print(f'added {buy}')

    def remove2(self,buy):
        if buy in self.buyers:
            b = str(self.buyers[buy])
            
        else:
            b = buy
        self.buyers[buy] = b
        print(f'removed {buy}')

    def remove(self,key,qty):
        q = 0
        if key in self.cars:
            v = self.cars[key]
            q = v - qty
        if q < 0:
            q = 0
        self.cars[key] = q
        print(f'removed {qty} {key}: total = {self.cars[key]}')
    
    def display(self):
        for key, value in self.cars.items():
            print(f'{key} = {value}')
        for buy in self.buyers.items():
            print(f'{buy}')

    def save(self):
        print('saving information...')
        with open('inventory4.txt', 'w') as f:
            json.dump(self.cars,f)
            json.dump(self.buyers,f)
        print('information saved!')

    def load(self):
        print('loading information...')
        if not os.path.exists('inventory4.txt'):
            print('nothing to load!')
            return
        with open('inventory4.txt', 'r') as f:
            self.cars = json.load(f)
            self.buyers = json.load(f)
        print('Information Loaded!')

def main():
    inv = Inventory()

    while True:
        print('Write your choice...\n')
        action = input('Choice:\nadd car, remove car, add buyer, remove buyer, list, save, exit: ')
        if action == 'avsluta':
            break
        if action == 'add car' or action == 'remove car':
                key = input('Add/remove specific car: ')
                qty = int(input('Quantity: '))
                if action == 'add car':
                    inv.add(key,qty)
                if action == 'remove car':
                    inv.remove(key,qty)
        if action == 'add buyer' or action == 'remove buyer':
                buyers = input('Add/remove buyer: ')
                if action == 'add buyer':
                    inv.add2(buyers)
                if action == 'remove buyer':
                    inv.remove2(buyers)
            
        if action == 'list':
            print('____________________________________\n')
            inv.display()
            print('____________________________________\n')
        
        if action == 'save':
            inv.save
    inv.save()


if __name__ == "__main__":
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文