python程序银行开户
时间: 2024-01-03 07:14:56 浏览: 26
以下是一个简单的Python程序,用于银行开户:
```python
class BankAccount:
def __init__(self, name, initial_balance):
self.name = name
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
print("Deposit successful. New balance is:", self.balance)
def withdraw(self, amount):
if amount > self.balance:
print("Withdrawal amount exceeds available balance.")
else:
self.balance -= amount
print("Withdrawal successful. New balance is:", self.balance)
# Example usage
name = input("Enter your name: ")
initial_balance = float(input("Enter initial balance: "))
account = BankAccount(name, initial_balance)
while True:
print("\nSelect an option:")
print("1. Check balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
print("Your balance is:", account.balance)
elif choice == 2:
amount = float(input("Enter deposit amount: "))
account.deposit(amount)
elif choice == 3:
amount = float(input("Enter withdrawal amount: "))
account.withdraw(amount)
elif choice == 4:
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
```
这个程序创建了一个 `BankAccount` 类,该类包含账户名称和余额属性,以及存款和取款方法。程序还提供了一个简单的用户界面,允许用户选择要执行的操作,包括检查余额、存款、取款和退出。用户输入通过 `input()` 函数获取,并将其转换为适当的类型。