如何解决我在python中使用while循环运行命令时遇到金额问题
我导入了一个json文件,并为超级稻的稻米银行帐户创建了一个while循环。我在父代码下创建def,但遇到错误:TypeError:withdraw_money()缺少1个必需的位置参数:'amount'。我还尝试为每个子类在新周中添加新值,该值将为超级大米帐户和预付附加500,为普通大米A附加0。由于错误,我被SuperRiceAccount.withdraw_money(amount)困住了,我仍然需要弄清楚新星期的功能。谁能帮我吗?
下面是我的代码
class BaseAccount:
def __init__(self,account_id,account_type,full_name,birthday,balance,amount=500):
self.account_id = account_id
self.account_type = account_type
self.full_name = full_name
self.birthday = birthday
self.balance = balance
self.amount = amount
def account_info(self):
for p in data ['accounts']:
print('You have successfully withdrawn 500 grams from the account.')
print('Account ID:' + p['account_id'])
print('Account Type:' + p['account_type'])
print('Full Name:' + p['full_name'])
print('Birthday:' + p['birthday'])
print('Balance: ' + self.balance + ' left')
def withdraw_money(self,amount):
self.balance = self.balance - amount(500)
for p in data['accounts']:
print('You have successfully withdrawn 500 grams from the account.')
print('Account ID:' + p['account_id'])
print('Account Type:' + p['account_type'])
print('Full Name:' + p['full_name'])
print('Birthday:' + p['birthday'])
print('Balance: ' + self.balance + ' left')
def new_week(self):
return
class SuperRiceAccount(BaseAccount):
def __init__(self,balance):
super().__init__(account_id,balance)
if self.balance >= 2000:
print ('You still have ',self.balance,' left.')
else:
print('You do not have enough balance.')
def new_week(self):
if initial_balance < 2000:
self.balance = self.balance + 500
print('Your new balance is ',' left')
while True:
print('( 1 ) List All Accounts')
print('( 2 ) Withdraw Rice from an Account')
print('( 3 ) New Week')
print('( 4 ) Exit')
print('')
option = int(input('Enter your choice: '))
if option == 1:
for p in data['accounts']:
print('Account ID:'+ p['account_id'])
print('Account Type:' + p['account_type'])
print('Full Name:' + p['full_name'])
print('Birthday:' + p['birthday'])
print('Balance:'+ str(p["balance"]))
print('')
if option == 2:
print('')
i = input('Enter Account ID:')
print('')
for p in data['accounts']:
if p ['account_id'] == i:
print('Account ID:' + p['account_id'])
print('Account Type:' + p['account_type'])
print('Full Name:' + p['full_name'])
print('Birthday:' + p['birthday'])
print('Balance: ' + str(p["balance"]) + ' left')
print('')
else:
amount = input('Input 500 to withdraw 500 grams from this account:')
SuperRiceAccount.withdraw_money(amount)
SuperRiceAccount.account_info(amount)
for p in data['accounts']:
print('Account ID:' + p['account_id'])
print('Account Type:' + p['account_type'])
print('Full Name:' + p['full_name'])
print('Birthday:' + p['birthday'])
print('Balance:' + str(p["balance"]))
print('')
解决方法
在调用方法SuperRiceAccount
时应使用withdraw_money
对象。
发生的事情是,当您调用该函数时,金额作为第一个参数self
传递,因此第二个参数amount
丢失了。
通过初始化对其进行修复:
bank = SuperRiceAccount([PASS HERE INITIALIZATION PARAMETERS])
并致电:
bank.withdraw_money(amount)
bank.account_info(amount)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。