So i am fairly new to OOP in Python, and as an excercise, I wrote a simple "Bank" program, and it works. The obstacle is when I create an instance to the Account class, i want to save it in some text file, and be able to read existing instances from it. For example: *I create 10 accounts, and when I kill the program, I want for them to save the instance data to a file, and when I open it again, I want to be able to import it and use it as previously * Here is the code I wrote:
class Account:
def __init__(self,name,password,amount):
self.name = name
self.password = password
self.amount = amount
def display(self):
print(self.name, self.amount)
def withdraw(self, new_amount):
if new_amount < self.amount and new_amount > 0:
self.amount -=new_amount
else:
raise ValueError('Amount must be greater than 0 and less than current amount')
def deposit(self,new_amount):
if new_amount>0:
self.amount += new_amount
else:
raise ValueError('Amount must be greater than 0')
Hope you all can help me!
source https://stackoverflow.com/questions/75585126/saving-class-instances-to-a-file-and-reading-from-it-oop
Comments
Post a Comment