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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章