python写一个银行管理系统
时间: 2023-10-10 11:08:53 浏览: 78
好的,以下是一个简单的银行管理系统的Python代码。这个系统可以让用户进行账户的创建、存款、取款、查询余额等操作。
```python
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposit successful. Your new balance is {self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrawal successful. Your new balance is {self.balance}")
else:
print("Insufficient balance.")
def check_balance(self):
print(f"Your balance is {self.balance}")
def create_account():
account_number = input("Enter account number: ")
balance = float(input("Enter opening balance: "))
print("Account created successfully.")
return BankAccount(account_number, balance)
def main():
accounts = {}
while True:
print("1. Create Account\n2. Deposit\n3. Withdraw\n4. Check Balance\n5. Quit")
choice = input("Enter choice: ")
if choice == "1":
account = create_account()
accounts[account.account_number] = account
elif choice == "2":
account_number = input("Enter account number: ")
amount = float(input("Enter amount to deposit: "))
accounts[account_number].deposit(amount)
elif choice == "3":
account_number = input("Enter account number: ")
amount = float(input("Enter amount to withdraw: "))
accounts[account_number].withdraw(amount)
elif choice == "4":
account_number = input("Enter account number: ")
accounts[account_number].check_balance()
elif choice == "5":
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
```
这个程序使用一个BankAccount类来表示银行账户。它有一个初始的账户号码和余额。然后,它可以执行存款、取款和查询余额操作。
在主函数中,它使用一个字典来保存已经创建的账户。当用户选择创建账户时,它会要求用户输入账户号码和初始余额,并创建一个新的BankAccount对象。然后,它将这个对象添加到字典中。
如果用户选择存款、取款或查询余额,它将要求用户输入账户号码,并调用相应的BankAccount方法来执行操作。
最后,当用户选择退出时,程序将结束。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)