[Tutor] Problems with pytz module

Daniel Bosah dbosah at buffalo.edu
Wed Jul 19 15:58:49 EDT 2017


I'm learning about OOP programming in Python.
This is my code from my online course.

import datetime
import pytz
class Account:
    """""""Simple account class with balance"""""""
    def __init__(self,name,balance):
        self.name = name
        self.balance = balance
        self.transaction_list = []
        print "Account created for " + self.name

    def deposit(self,amount):
        if amount > 0:
            self.balance += amount
            self.show_balance()

self.transaction_list.append((pytz.utc.localize(datetime.datetime.utcnow()),amount))
# appends traction details to list

    def withdrawl(self,amount):
         if 0 < amount <= self.balance:
                self.balance -= amount
         else:
            print "The account must be greater then zero and no more then
your account balance"
         self.show_balance()

    def show_balance(self):
        print "Balance is {}".format(self.balance)

    def show_transactions(self):
        for date, amount in self.transaction_list:
            if amount > 0:
                tran_type = "deposited"
            else:
                tran_type = "withdrawn"
                amount *= -1 # to show negative number
                print "{:6} {} on {} (local time was {})".format(amount,
tran_type, date, date.astimezone())

if __name__ == '__main__':
    tim = Account("Tim", 0)
    tim.show_balance()


    tim.deposit(1000)
    tim.show_balance()
    tim.withdrawl(500)
    tim.show_transactions()

    tim.show_balance()



Im using Ubuntu Linux. My problem is that I cannot get show_transactions to
print out on the console. I suspect that I cannot use pytz ( as I'm using
Python 2.7) . Im trying to get the date and time of the transaction (as
shown on this line -

print "{:6} {} on {} (local time was {})".format(amount, tran_type, date,
date.astimezone())

But it will. Is there a workaround for pytz, or another problem that I am
missing?


More information about the Tutor mailing list