Struggling with class functions

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Struggling with class functions

 
0
  #1
Oct 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 966
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
1
  #2
Oct 13th, 2009
It should be
  1. def withdraw(self, amount):
  2. self.balance -= amount
There is no self.amount , only the paramater amount .
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #3
Oct 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 966
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #4
Oct 13th, 2009
Originally Posted by thehivetyrant View Post
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC