Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中比较之后,有没有一种方法可以从列表中访问Account类对象_Python_Python 3.x_Oop - Fatal编程技术网

在Python3中比较之后,有没有一种方法可以从列表中访问Account类对象

在Python3中比较之后,有没有一种方法可以从列表中访问Account类对象,python,python-3.x,oop,Python,Python 3.x,Oop,我正在使用Python3,我计划创建一个应用程序,从用户和银行获取帐户数。我从用户处获取帐户名和号码,并将其存储在集合(列表)中。我应该如何比较该对象的属性,或者通过从用户处获取帐号来删除该对象? 这是我的代码: class Account: def __init__(self): print("Enter the account name") self.account_name = input() print("Enter the ac

我正在使用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()

比较每个对象的帐号是唯一的选择。通过delete_account函数,我已经对解决方案进行了编码。此外,print_bank_details函数中存在一个bug。它没有打印第一个帐户的详细信息,我也修复了这个

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()

非常感谢。这就解决了问题。我试图使用account_list[I]访问对象列表。我试着反复讨论它。塔克斯!