我找不到任何有此问题的源代码,所以我很困惑我的代码没有参考点。基本上,我想做的是从get_interest_rate()获得回报利息,并将其放入get_interest()的括号中。我不太确定是否有可能?我一直在互联网上寻找可以找到如何执行此操作的资源,但找不到任何东西。谢谢!
class BankAccount:
def __init__(self, a, b, c):
self.account = "%d - %d - %d" %(a, b, c)
self.balance = 0
def get_account(self):
print("Your account number is", self.account)
return self.account
def get_balance(self):
print("You have %.2f" %self.balance)
return self.balance
def deposit(self):
dep = float(input("Enter deposit amount: "))
self.balance = self.balance + dep
def withdraw(self):
while True:
draw = float(input("Enter withdrawal amount: "))
if draw > self.balance:
print("You have insufficient amount to withdraw")
continue
else:
break
self.balance = self.balance - draw
print("You are left with %.2f" %self.balance)
return self.balance
def get_interest_rate(self):
interest = 0
if self.account[0] == '1':
interest = 0.01
print("Your interest rate is", interest)
elif self.account[0] == '0':
interest = 0.05
print("Your interest rate is", interest)
return interest
def get_interest(self, interest):
earned = self.balance * interest
print("Your interest earned is %.2f" % earned)
return earned
account1 = BankAccount(00, 12345, 11)
account1.get_account()
account1.deposit()
account1.get_balance()
account1.withdraw()
account1.get_interest_rate()
account1.get_interest()
这是我一直收到的输出/错误:
Your interest rate is 0.05
Traceback (most recent call last):
File "/Users/kovitan/PycharmProjects/Object-Oriented Programming/venv/Practical 2/Qn 4.py", line 51, in <module>
account1.get_interest()
TypeError: get_interest() missing 1 required positional argument: 'interest'
答案 0 :(得分:2)
要返回account1.get_interest(..)
来呼叫account1.get_interest_rate()
,请输入:
account1.get_interest(account1.get_interest_rate())
答案 1 :(得分:0)
您可以尝试类似的方法吗?
class Account:
def __init__(self, balance):
self.account = ['0']
self.balance = balance
def get_interest_rate(self):
interest = 0
if self.account[0] == '1':
interest = 0.01
print("Your interest rate is", interest)
elif self.account[0] == '0':
interest = 0.05
print("Your interest rate is", interest)
return interest
def get_interest(self, interest):
earned = self.balance * interest
return "Your interest earned is %.2f" % earned
account = Account(1000)
print(account.get_interest(account.get_interest_rate()))