1,105,399 Community Members

To class or not to class...

Member Avatar
clouds_n_things
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

So I'm writing a personal bank tracker for myself, and I'm having a bit of trouble with a line of code. The ID_Pin method below is equal to 0000, however when i try to create an instance (any instance of the Account class), it returns the "Access Denied" bit that I wrote(ID_Pin), even when the instance pin matches the method pin. Any suggestions?

class Account:
    '''Wells Fargo.'''
    global balance 
    def __init__(self, name, pin_number, balance=100):
        self.name = name
        self.pin_number = pin_number
        self.balance = 100

    def alter_pin(self, enter_a_pin):
        '''To be used a later revision, changes a pin number.'''
        ID_List = []
        enter_a_pin.append(ID_List) 

    def ID_Pin(self):
        pin = 0000
        return pin  

    def get_balance(self, pin_number): 
        if pin_number == Account.ID_Pin:
            print("Balance: %d" % self.balance)
        else:
            print("Access denied: Incorrect PIN.")

    def withdraw(self,pin_number, amount):
        if pin_number == Account.pin:
            balance -= amount
            print("Withdrew  %d. New Balance: %d" % amount, balance)
        else:
            print("Access denied: Incorrect PIN.")

    def deposit(self, pin_number, amount):
        if pin_number == Account.ID_Pin:
            balance += amount
            print("Deposited %d. New Balance: %d" % amount, balance) 
            if balance <= 0:
                print("Balance limit reached!")
        else:
            print("Access denied: Incorrect PIN.") 

New_Bank_Account = Account("person",0000,100) 
New_Bank_Account.get_balance(0000)
New_Bank_Account.deposit(0000, 50000) 
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
2
 

Your code has a handful of mistakes and misconceptions. I corrected some of them to get this to work ...

class Account:
    '''Wells Fargo.'''
    #global balance 
    def __init__(self, name, pin_number, balance):
        self.name = name
        self.pin_number = pin_number
        self.balance = balance

    def alter_pin(self, enter_a_pin):
        '''needs work'''
        '''To be used a later revision, changes a pin number.'''
        ID_List = []
        ID_List.append(enter_a_pin) 

    def ID_Pin(self):
        '''needs work'''
        pin = 0000
        return pin  

    def get_balance(self, pin_number): 
        if pin_number == self.pin_number:
            print("%s has balance: %d" % (self.name, self.balance))
        else:
            print("Access denied: Incorrect PIN.")

    def withdraw(self,pin_number, amount):
        if pin_number == self.pin_number:
            self.balance -= amount
            print("Withdrew  %d. New Balance: %d" % (amount, self.balance))
        else:
            print("Access denied: Incorrect PIN.")

    def deposit(self, pin_number, amount):
        if pin_number == self.pin_number:
            self.balance += amount
            print("Deposited %d. New Balance: %d" % (amount, self.balance)) 
            if self.balance <= 0:
                print("Balance limit reached!")
        else:
            print("Access denied: Incorrect PIN.") 

# create unique account instances ...
jimmy = Account("Jim", 0000, 100) 
jimmy.get_balance(0000)
jimmy.deposit(0000, 50000)

print('-'*40)

betsy = Account("Elisabeth", 1234, 777) 
betsy.get_balance(1234)
betsy.deposit(1234, 7000)

''' result ...
Jim has balance: 100
Deposited 50000. New Balance: 50100
----------------------------------------
Elisabeth has balance: 777
Deposited 7000. New Balance: 7777
'''

Note that self is a reference to the particular instance of the class.

Member Avatar
clouds_n_things
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Yeah im sure it would have, im translating it from Ruby lol x_x, but thanks! :)

Member Avatar
clouds_n_things
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

Man now that I look at it, I did make a lot of screwups, but it works just fine in Ruby. :O From now on, im just gonna write in either python or ruby, translating is not for me...yet.

Thanks Vega! :D

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

Translating too closely doesn't allow you to take adavantage of the chosen language's true power/elegance.

Question Answered as of 1 Year Ago by vegaseat
Member Avatar
clouds_n_things
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

So is it wise just to not translate and just write each program in either Ruby or Python?

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

As long as you can make the program function the way you want. You learn a lot more that way.

Member Avatar
clouds_n_things
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hmmm seems logical. Its just some things I find easier to write in ruby, some I find easier to write with python. I wish I could combine them. Rubython! lol.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article