您好,我是一名刚开始学习python的新手程序员。 我之前发过同样的问题并且我已经解决了,但我的答案并不是问题所在。
我需要帮助解决为什么我需要实现一种新方法,即使我可以采取其他方式。
感谢
问题:
__str__
类的Bank
方法返回包含该字符串的字符串 帐户按随机顺序排列。设计并实施导致的变更 按名称顺序放置在字符串中的帐户。 [这是我不理解的部分] (提示:您还必须在SavingsAccount类中定义一个新方法。)
class Bank(object):
def __init__(self):
self._accounts = {}
def __str__(self):
"""Return the string rep of the entire bank."""
pTemp =[]
for i in xrange(len(SavingsAccount.temp)-1):
if self._accounts.get(SavingsAccount.temp[i]).getName() >= self._accounts.get(SavingsAccount.temp[i+1]).getName():
temp = SavingsAccount.temp[i]
SavingsAccount.temp[i] = SavingsAccount.temp[i+1]
SavingsAccount.temp[i+1] = temp
for i in SavingsAccount.temp:
pTemp.append(self._accounts[i])
return '\n'.join(map(str, pTemp))
def add(self, account):
"""Inserts an account using its PIN as a key."""
self._accounts[account.getPin()] = account
def remove(self, pin):
return self._accounts.pop(pin, None)
def get(self, pin):
return self._accounts.get(pin, None)
def computeInterest(self):
"""Computes interest for each account and
returns the total."""
total = 0.0
for account in self._accounts.values():
total += account.computeInterest()
return total
class SavingsAccount(object):
"""This class represents a Savings account
with the owner's name, PIN, and balance."""
RATE = 0.02
temp = []
def __init__(self, name, pin, balance = 0.0):
self._name = name
self._pin = pin
self._balance = balance
SavingsAccount.temp.append(self)
def __str__(self):
result = 'Name: ' + self._name + '\n'
result += 'PIN: ' + self._pin + '\n'
result += 'Balance: ' + str(self._balance)
return result
def getBalance(self):
return self._balance
def getName(self):
return self._name
def getPin(self):
return self._pin
def deposit(self, amount):
"""Deposits the given amount and returns the
new balance."""
self._balance += amount
return self._balance
def withdraw(self, amount):
"""Withdraws the given amount.
Returns None if successful, or an
error message if unsuccessful."""
if amount < 0:
return 'Amount must be >= 0'
elif self._balance < amount:
return 'Insufficient funds'
else:
self._balance -= amount
return None
def computeInterest(self):
"""Computes, deposits, and returns the interest."""
interest = self._balance * SavingsAccount.RATE
self.deposit(interest)
def main():
bank = Bank()
bank.add(SavingsAccount("Zelda","1003",5000.00))
bank.add(SavingsAccount("Wilma","1001",4000.00))
bank.add(SavingsAccount("Fred","1002",1000.00))
print bank
main()
答案 0 :(得分:1)
我认为这个问题希望你在SavingsAccount类中定义排序,也就是说,能够确定SavingAccounts
的实例是否在{{1}的另一个实例之后或之前}}。我不想在这里写任何剧透,但告诉我,如果我的提示不够;)。
<强>更新强>
另外,Python中常见的错误源包括字符串排序:SavingAccount
出现在a
之前z
之前A
...
<强> UPDATE2 强>
更多提示;)
您真正想要的是根据给定条件对SavingAccount的实例列表进行排序。有两种方法可以做这种事情。你可以:
第二个选项通常更好,因为“要排序的类”应该比任何其他人更清楚如何对自己进行排序(这是关于封装:不让人们控制你的类如何工作)。即使问题不是很清楚,而且这个例子不是很好(在我看来),这是他们希望你选择的选项。
这个想法是银行应该做这样的事情:
Z
class Bank(object):
def __str__(self):
"""Return the string rep of the entire bank."""
#get a sorted copy of the list
#using default SavingAccount comparison
pTemp =sorted(self._accounts)
return '\n'.join(map(str, pTemp))
包含有关如何排序的信息。
您可能需要查看this article from the PythonInfo Wiki。 另外:http://docs.python.org/reference/datamodel.html#object.__lt__