944,120 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 546
  • Python RSS
Oct 13th, 2009
0

Struggling with class functions

Expand Post »
Hi there,
I'm trying to create a class with whats below.
I made it and it works but i need to be able to withdraw an amount from the balance.

I'm just having trouble with the layout and whether i got the right idea?

Is the underlined area on the right track?

class BankAccount:
    def __init__(self, balance, interestrate, accountnumber):
        self.balance = balance
        self.interestrate = interestrate
        self.accountnumber = accountnumber
    def withdraw(self, amount):
        self.amount = self.balance-self.amount

p1 = BankAccount(5500, 0.2, 001)


Because when i type:

p1.withdraw(500) (i'm wanting to withdraw 500 from the balance of 5500.)

i get the error message:
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    p1.withdraw(500)
  File "H:\Year 2\217CR James S\Lab 1\BankAccount.py", line 7, in withdraw
    self.amount = self.balance-self.amount
AttributeError: BankAccount instance has no attribute 'amount'

Thanks in advance
Last edited by thehivetyrant; Oct 13th, 2009 at 8:12 am.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 13th, 2009
1
Re: Struggling with class functions
It should be
python Syntax (Toggle Plain Text)
  1. def withdraw(self, amount):
  2. self.balance -= amount
There is no self.amount , only the paramater amount .
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 13th, 2009
0
Re: Struggling with class functions
Thanks!
Thats sorted it. Is there a rule that you dont enter any self.xxx code if it's not in the init or something. Just trying to understand here.

I obviously put in an if statment to stop them withdrawing more than allowed. Works great.

Thanks a bunch.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 13th, 2009
0
Re: Struggling with class functions
Is there a rule that you dont enter any self.xxx code if it's not in the init or something. Just trying to understand here.
There is no such rule. The BankAccount object stores a certain amount of data in it's members self.balance, self.interestrate and self.accountnumber. These values can be used in all the methods of the class.In your method "withdraw", the parameter "amount" is just a temporary value which is not stored in the BankAccount object.
Last edited by Gribouillis; Oct 13th, 2009 at 9:03 am.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: twill unicode related
Next Thread in Python Forum Timeline: Form Value Won't Post





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC