用python写一个ATM的过程
时间: 2023-06-12 19:04:34 浏览: 52
以下是一个简单的 Python ATM 程序的示例,包括账户余额、存款、取款、更改密码等基本功能:
```python
class ATM:
def __init__(self, balance, pin):
self.balance = balance
self.pin = pin
def check_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient balance"
else:
self.balance -= amount
return self.balance
def change_pin(self, new_pin):
self.pin = new_pin
return "PIN successfully changed"
atm = ATM(1000, 1234)
print("Welcome to Python ATM")
pin = int(input("Please enter your PIN: "))
if pin == atm.pin:
print("1. Check Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Change PIN")
option = int(input("Please select an option: "))
if option == 1:
print("Your balance is:", atm.check_balance())
elif option == 2:
amount = int(input("Please enter the amount to deposit: "))
print("Your new balance is:", atm.deposit(amount))
elif option == 3:
amount = int(input("Please enter the amount to withdraw: "))
print("Your new balance is:", atm.withdraw(amount))
elif option == 4:
new_pin = int(input("Please enter your new PIN: "))
print(atm.change_pin(new_pin))
else:
print("Invalid option selected")
else:
print("Incorrect PIN")
```
此程序创建了一个名为 `ATM` 的类,该类具有账户余额、密码等属性和方法。在程序的主函数中,用户输入他们的 PIN 码并验证它是否正确。然后,用户可以选择要执行的操作,包括检查余额、存款、取款和更改密码。程序将相应地调用相应的方法,并显示余额或其他选项。如果PIN码不正确,则程序将提示用户输入正确的PIN码。