这一个 atm 的 python 程序
时间: 2024-05-04 18:18:55 浏览: 81
以下是一个简单的 Python ATM 程序,它可以模拟 ATM 的基本功能,包括检查账户余额、取款、存款和余额查询等操作:
```python
class ATM:
def __init__(self, balance=0):
self.balance = balance
def check_balance(self):
print("Your current balance is: ", self.balance)
def withdraw(self, amount):
if self.balance - amount < 0:
print("Insufficient balance. Withdrawal failed.")
else:
print("Withdrawal successful. Please collect your cash.")
self.balance -= amount
def deposit(self, amount):
self.balance += amount
print("Deposit successful. Your current balance is: ", self.balance)
atm = ATM(5000)
while True:
print("Welcome to the Python ATM!")
print("1. Check balance")
print("2. Withdraw cash")
print("3. Deposit cash")
print("4. Quit")
option = input("Please select an option: ")
if option == "1":
atm.check_balance()
elif option == "2":
amount = int(input("Please enter the amount to withdraw: "))
atm.withdraw(amount)
elif option == "3":
amount = int(input("Please enter the amount to deposit: "))
atm.deposit(amount)
elif option == "4":
print("Thank you for using the Python ATM!")
break
else:
print("Invalid option. Please try again.")
```
这是一个基本的 ATM 类,它包含了检查余额、取款、存款和退出等功能。在程序中,我们首先创建了一个 ATM 对象,并将其初始余额设置为 5000。然后,我们在一个 while 循环中显示了 ATM 的菜单,用户可以选择一个选项来执行不同的操作。当用户选择退出时,程序退出。
阅读全文