需要以下代码的帮助我需要它基本上打印出帐户持有人的姓名及其在订单输入程序时的余额。此外,如何实现从程序中添加和删除帐户的能力,任何建议都会很好,非常感谢任何帮助。class BankAccount:
# constructor or initializer
def __init__(self, name, money, pin):
self.__name = name
self.__balance = money # __balance is private now, so it is only accessible inside the class
self.__pin = pin
def pincheck(self):
pin = input("Enter Pin")
if pin == self.__pin
return True
else:
print("Error try again")
def deposit(self, money):
self.__balance += money
def withdraw(self, money):
if self.__balance > money :
self.__balance -= money
return money
else:
return "Insufficient funds"
def checkbalance(self):
return self.__balance
b1 = BankAccount('Obi Ezeakachi', 5000, 1111)
b2 = BankAccount('Tasha St.Patrick', 80000, 2222)
b3 = BankAccount('Tommy Egan', 7000, 3333)
d1 = 0
d2 = 0
d3 = 0
print("Obi Ezeakachi: £",b1.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
if y1 == 1:
w1= int(input("How much do you want to withdraw"))
print("Withdrawal: £",b1.withdraw(w1))
else:
d1= int(input("How much do you want to deposit"))
b1.deposit(d1)
print("Current Balance:",b1.checkbalance())
print("Tasha St.Patrick:",b2.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
if y1 == 1:
w2= int(input("How much do you want to withdraw"))
print(b2.withdraw(w2))
else:
d2= int(input("How much do you want to deposit"))
b2.deposit(d2)
print("Tommy Egan:",b3.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
if y1 == 1:
w3= int(input("How much do you want to withdraw"))
print("Withdrawal:",b3.withdraw(w3))
else:
d3= int(input("How much do you want to deposit"))
b3.deposit(d3)
print("Current Balance:",b3.checkbalance())
import package1.module1
答案 0 :(得分:0)
考虑使用主键作为名称和pin& amp;保存字典中的所有详细信息。平衡作为辅助键。然后,您可以将其保存到json文件。
import json
accdict ={}
accdict['Obi'] = {'Name':'Obi Ezeakachi','Pin':1111,'Balance': 5000}
继续所有帐户。
with open('accounts.json','w') as f:
f.write(json.dumps(accdict))
您现在可以根据需要操作此词典并重复。
此外,您应该在进行交易之前调用引脚检查功能。如果你认真对待并希望它像ATM一样,在访问细节并与用户交互时,使用线程在后台检查引脚。