[Tutor] Help on Python Assignment
Bob Gailer
bgailer at alum.rpi.edu
Sun Jun 6 18:24:08 EDT 2004
Hey Chris - how goes it. Looks like you are working on the class project
again. Is that true? What's your relationship with the school's program at
this time?
Please, when submitting code with a syntax error, also provide the
exception. It tells us which line the problem is on.
In this case, when I paste your code into my IDE and check it, it has no
errors!
Here is my copy of your program with some code to exercise it:
import time
class Account:
def __init__(self, initial):
self.balance = initial
self.payactions={}
def deposit(self,amt):
self.balance=self.balance+amt
t=time.strftime('%a, %d %b %Y %H:%M:%S',
time.localtime(time.time() + 0))
self.payactions[amt]=t
def withdraw(self,amt):
if self.balance-amt<0:
print 'Not enough Mojo in your account!'
else:
self.balance=self.balance-amt
t=time.strftime('%a, %d %b %Y %H:%M:%S',
time.localtime(time.time() + 0))
self.payactions[-amt]=t
def printbalance(self):
print 'Date Activities'
for k, v in self.payactions.iteritems():
print v,' ',k
a=Account(100)
a.deposit(10)
a.withdraw(20)
a.printbalance()
And here is the output:
Date Activities
Sun, 06 Jun 2004 16:16:22 10
Sun, 06 Jun 2004 16:16:22 -20
There is a problem with storing transactions using the amount as the key.
If you make 2 or more transactions with the same amount, each will replace
the previous in the dictionary, and you will be left with only the last.
Why did you choose a dictionary? A list is the preferred way to save
something like this, especially if you want to keep it in its timestamp
order, and retain all transactions.
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
More information about the Tutor
mailing list