python的小程序

class Account:
def __init__(self, number, name):
self.number = number
self.name = name
self.balance = 0
def deposit(self, amount):
if amount <= 0:
raise ValueError('must be positive(存入金額有誤)')
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise RuntimeError('balance not enough(餘額不足)')
account=input('請輸入您的賬號(賬號現僅能選擇Justin、Momor):')
f=int(input('請選擇操作類型,1爲存款,0爲取款,請選擇:',))
if f==1:
c=int(input('請輸入存款金額(必須爲整數):',))
elif f==0:
d=int(input('請輸入取款金額(必須爲整數):',))
else:
raise RuntimeError('您輸入有誤,謝謝使用,再見')
if f==1:
if c>=0:
a=c
else:
raise ValueError('您輸入的金額有誤,謝謝使用')
elif f==0:
if d>=0:
b=d
else:
raise ValueError('您輸入的金額有誤,謝謝使用')
acct1 = Account('123-456-789', 'Justin')
deposit1 = acct1.deposit
withdraw1 = acct1.withdraw
acct2 = Account('987-654-321', 'Momor')
deposit2 = acct2.deposit
withdraw2 = acct2.withdraw
if account ==acct1.name:
if f==1:
deposit1(a)
elif f==0:
withdraw1(b)
print('賬號:',acct1.name,'當前餘額爲:',acct1.balance)
elif account ==acct2.name:
if f==1:
deposit2(a)
elif f==0:
withdraw2(b)
print('賬號:',acct2.name,'當前餘額爲:',acct2.balance)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章