| | |
Struggling with class functions
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 43
Reputation:
Solved Threads: 0
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?
Because when i type:
i get the error message:
Thanks in advance
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.
1
#2 Oct 13th, 2009
It should be
There is no
python Syntax (Toggle Plain Text)
def withdraw(self, amount): self.balance -= amount
self.amount , only the paramater amount . 0
#4 Oct 13th, 2009
•
•
•
•
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.
Last edited by Gribouillis; Oct 13th, 2009 at 9:03 am.
![]() |
Similar Threads
- need help writing pseudocodes that involves class and functions (C)
- Why the member functions of one class can access to private data member of other obj? (C++)
- new user: member functions can't read arrays from inside a class (C++)
- Using functions without calling them from any class (C++)
- invoking class functions from user input (C++)
- Basic Math class HELP! (Java)
- Class Functions and Conversions (C)
- binary tree class (C++)
- Create Function that prints letters, words and sentences (C++)
Other Threads in the Python Forum
- Previous Thread: twill unicode related
- Next Thread: Form Value Won't Post
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse rpg scrolledtext search server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython





