我正在使用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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
比较每个对象的帐号是唯一的选择。通过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