Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jami Schwarzwalder
17,961 PointsWhat am I doing wrong?
I can't quite figure out what I did wrong in my code? It doesn't seem to handle fees properly but my first test works...
class BankAccount:
""" Class definition modeling the behavior of a simple bank account """
def __init__(self, initial_balance):
"""Creates an account with the given balance."""
self.balance = initial_balance
self.fee = 0
def deposit(self, amount):
"""Deposits the amount into the account."""
self.balance += amount
def withdraw(self, amount):
"""
Withdraws the amount from the account. Each withdrawal resulting in a
negative balance also deducts a penalty fee of 5 dollars from the balance.
"""
self.balance -= amount
print self.balance
if (self.balance < 0):
self.balance -= 5
#print self.balance
self.fee +=1
#print self.fee
else:
self.balance -= amount
def get_balance(self):
"""Returns the current balance in the account."""
return self.balance
def get_fees(self):
"""Returns the total fees ever deducted from the account."""
return self.fee * 5
my_account = BankAccount(10)
my_account.withdraw(15)
my_account.deposit(20)
my_account.withdraw(15)
my_account.deposit(20)
my_account.withdraw(15)
my_account.deposit(20)
my_account.withdraw(15)
my_account.deposit(20)
print my_account.get_balance(), my_account.get_fees()
2 Answers
William Li
Courses Plus Student 26,868 PointsHi, Jami Schwarzwalder At first glance, I can see there's problem with the withdraw method
def withdraw(self, amount):
"""
Withdraws the amount from the account. Each withdrawal resulting in a
negative balance also deducts a penalty fee of 5 dollars from the balance.
"""
self.balance -= amount
print self.balance
if (self.balance < 0):
self.balance -= 5
#print self.balance
self.fee +=1
#print self.fee
else:
self.balance -= amount
First there's this line self.balance -= amount that subtracts the amount from self.balance.
then enter the if...else conditional, if the leftover balance is not negative, it will call in this statment self.balance -= amount at the else clause to subtract the amount from balance AGAIN.
So this is a semantic problem here because the method can double withdraw the amount so long as the balance is above 0.
Jami Schwarzwalder
17,961 PointsThe code does work there,
but when i tried to do other amounts it doesn't calculate correctly
I don't know why it only works for those amounts.
Trevor Currie
9,289 PointsTrevor Currie
9,289 PointsWhat is the functionality you desire? Right now it looks like you have functioning code.... Do you have errors?
(Please elaborate)