Python程序创建具有存款、取款函数的 Bankaccount 类
先决条件: Python中的面向对象编程
让我们使用 OOP 概念编写一个简单的Python程序来执行一些简单的银行操作,例如存款和取款。
首先,定义类Bankaccount。这一步之后是使用 __init__ 定义一个函数。一旦实例化类的对象,它就会运行。这个 __init__ 方法对于做任何你想对对象做的初始化很有用,然后我们有默认的参数 self。
Python3
# BankAccount class
class Bankaccount:
def __init__(self):
Python3
# Function to deposite amount
def deposit(self):
amount = float(input("Enter amount to be deposited: "))
self.balance += amount
print("\n Amount Deposited:", amount)
Python3
# Function to withdraw the amount
def withdraw(self):
amount = float(input("Enter amount to be withdrawn: "))
if self.balance >= amount:
self.balance -= amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
Python3
# Function to display the amount
def display(self):
print("\n Net Available Balance =", self.balance)
Python3
# Python program to create Bankaccount class
# with both a deposit() and a withdraw() function
class Bank_Account:
def __init__(self):
self.balance=0
print("Hello!!! Welcome to the Deposit & Withdrawal Machine")
def deposit(self):
amount=float(input("Enter amount to be Deposited: "))
self.balance += amount
print("\n Amount Deposited:",amount)
def withdraw(self):
amount = float(input("Enter amount to be Withdrawn: "))
if self.balance>=amount:
self.balance-=amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
def display(self):
print("\n Net Available Balance=",self.balance)
# Driver code
# creating an object of class
s = Bank_Account()
# Calling functions with that class object
s.deposit()
s.withdraw()
s.display()
这一步之后是使用 self 参数声明余额为 0,然后我们简单地打印一条欢迎机器的声明。在函数存款和取款中,金额作为输入(以浮动形式),然后添加/减去余额。因此,结果余额打印在下一行。
Python3
# Function to deposite amount
def deposit(self):
amount = float(input("Enter amount to be deposited: "))
self.balance += amount
print("\n Amount Deposited:", amount)
使用 if 条件检查是否存在充分
账户中可用于处理提款的金额。
Python3
# Function to withdraw the amount
def withdraw(self):
amount = float(input("Enter amount to be withdrawn: "))
if self.balance >= amount:
self.balance -= amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
接下来,我们使用显示函数来显示账户中的余额。然后我们创建一个对象并调用它来执行程序。
Python3
# Function to display the amount
def display(self):
print("\n Net Available Balance =", self.balance)
下面是实现:
Python3
# Python program to create Bankaccount class
# with both a deposit() and a withdraw() function
class Bank_Account:
def __init__(self):
self.balance=0
print("Hello!!! Welcome to the Deposit & Withdrawal Machine")
def deposit(self):
amount=float(input("Enter amount to be Deposited: "))
self.balance += amount
print("\n Amount Deposited:",amount)
def withdraw(self):
amount = float(input("Enter amount to be Withdrawn: "))
if self.balance>=amount:
self.balance-=amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
def display(self):
print("\n Net Available Balance=",self.balance)
# Driver code
# creating an object of class
s = Bank_Account()
# Calling functions with that class object
s.deposit()
s.withdraw()
s.display()
输出:
Hello !!! Welcome to Deposit&Withdrawal Machine
Enter amount to be deposited:
Amount Deposited: 1000.0
Enter amount to be withdrawn:
You Withdrew: 500.0
Net Available Balance = 500.0