开发者社区 问答 正文

在Python3中进行比较后,有没有办法让我从列表访问帐户类对象

我正在使用Python3,我打算创建一个应用程序,该应用程序可以接收来自用户和银行的大量帐户。我正在从用户那里获取帐户名称和号码,并将其存储在一个集合中(列表)。我应该如何比较该对象的属性或通过从用户帐户中删除该对象来删除该对象?这是我的代码:

class Account:

    def __init__(self):
        print("Enter the account name")
        self.account_name = input()
        print("Enter the account no")
        self.account_num = int(input())
        # print("Enter the balance")
        self.balance = 0

    def __str__(self):
        return f"{self.account_name,self.account_num,self.balance}"


class Bank:

    def __init__(self):
        print("Enter Bank Name")
        self.bname = input()
        print("Enter Bank Branch")
        self.branch = input()
        self.account_list = []
        # self.transfer_balance(13)

    def get_AccountList(self):
        print("Enter Account Details")
        self.account_list.append(Account())

    def delete_acccount(self, num):
        del self.account_list[num]

    def print_bank_details(self):
        print(f"Name of the Bank : {self.bname}")
        print(f"The name of the branch is : {self.branch}")

        for i in range(1, len(self.account_list)):
            print(f"Id : {i}---> {self.account_list[i]}")


def function():

    account_obj = []
    ans=True
    bank1 = Bank()
    while ans:
        print ("""
        1. Accept User Account INfo
        2. Print Account INfo
        3. Delete the account by Number
        4. Print Bank Info
        """)
        print("What would you like to do? ")

        ans=input()
        if ans == "1":
            print("\n Enter The Account Details")
            bank1.get_AccountList()
        elif ans == "2":
            print("\n Print Acccount info in the bank")
            bank1.print_bank_details()
        elif ans == "3":
            print("\n Enter the ID of the account to be deleted")
            num= int(input())
            bank1.delete_acccount(num)
        elif ans == "4":
            print("\n Goodbye")
        elif ans != "":
            print("\n Not Valid Choice Try again")

function()

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 10:02:05 380 分享 版权
1 条回答
写回答
取消 提交回答
  • 比较每个对象的帐号是唯一的选择。通过delete_account函数,我已经编码了解决方案。另外,print_bank_details函数中存在错误。这不是打印第一个帐户详细信息,我也已修复了这个问题。

    class Account:
    
        def __init__(self):
            print("Enter the account name")
            self.account_name = input()
            print("Enter the account no")
            self.account_num = int(input())
            # print("Enter the balance")
            self.balance = 0
    
        def __str__(self):
            return f"{self.account_name,self.account_num,self.balance}"
    
    
    class Bank:
    
        def __init__(self):
            print("Enter Bank Name")
            self.bname = input()
            print("Enter Bank Branch")
            self.branch = input()
            self.account_list = []
            # self.transfer_balance(13)
    
        def get_AccountList(self):
            print("Enter Account Details")
            self.account_list.append(Account())
    
        def delete_acccount(self, num):
            for account in self.account_list:
              if account.account_num == num:
                self.account_list.remove(account)
                return True
            print("Account number was not found")
    
        def print_bank_details(self):
            print(f"Name of the Bank : {self.bname}")
            print(f"The name of the branch is : {self.branch}")
    
            for i in range(0, len(self.account_list)):
                print(f"Id : {i+1}---> {self.account_list[i]}")
    
    
    def function():
    
        account_obj = []
        ans=True
        bank1 = Bank()
        while ans:
            print ("""
            1. Accept User Account INfo
            2. Print Account INfo
            3. Delete the account by Number
            4. Print Bank Info
            """)
            print("What would you like to do? ")
    
            ans=input()
            if ans == "1":
                print("\n Enter The Account Details")
                bank1.get_AccountList()
            elif ans == "2":
                print("\n Print Acccount info in the bank")
                bank1.print_bank_details()
            elif ans == "3":
                print("\n Enter the ID of the account to be deleted")
                num= int(input())
                bank1.delete_acccount(num)
            elif ans == "4":
                print("\n Goodbye")
            elif ans != "":
                print("\n Not Valid Choice Try again")
    
    function()
    

    回答来源:stackoverflow

    2020-03-24 10:02:14
    赞同 展开评论