2

This is my first question on here, so I hope I am asking the 'right' way. The code below is supposed to generate a class object, which should be named via the function createAccount() at the end. I was expecting the account to be a combination of surname and dob, however it creates the class object called accountName and not the product of the variable itself. I can see why it is doing this, but i cannot see how to correct it.

class Account(object):
    def __init__(self, balance):
        self.balance = balance

    def deposit(self,amount):
        self.balance += amount
        print(self.balance)
        return self.balance

    def withdraw(self,amount):
        if amount <= self.balance:
            self.balance -= amount
            print(self.balance)
            return self.balance
        else:
            print("You do not have sufficient funds for this transaction, please contact your local branch manager")

    def printBalance(self):
        print(self.balance)

def createAccount():
    name = input("Client Surname")
    dob = input("client Date of Birth")
    accountName = name+dob
    print(accountName) # for debug
    accountName = Account(0) # opening account balance is £0
    return accountName
Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
Neildor
  • 19
  • 6
  • 4
    You do realize that the name of the variable is lost when you `return` the object? What you're trying to do is 1) bad design and 2) impossible. If your Account needs a name, give your `Account` class a `name` attribute. – Aran-Fey Apr 12 '18 at 22:02
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – wwii Apr 12 '18 at 22:04
  • To be honest, no. i did not realise that the variable was lost after return. Thanks for the response, i have added a name attribute to my class. – Neildor Apr 12 '18 at 22:26

2 Answers2

2

I think what you want is something like a "name" field in your Account object. For example:

class Account(object):
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance

. . .

def createAccount():
    name = input("Client Surname")
    dob = input("client Date of Birth")
    accountName = name + dob
    print(accountName) # for debug
    account = Account(accountName, 0.0) # opening account balance is £0
    return account

The object returned by createAccount contains both the account balance and the name on the account.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • say if i wanted to avoid typing; neil = Account() bob = Account() jeff = Account().... to create my class object, is there a way to automate this process for a long list of people who want accounts creating for them? or is it better to call ALL objects 'account' and access them individually by self.name? – Neildor Apr 13 '18 at 20:09
  • You can loop through a list of account names and create one account for each name. You could store all those Account objects in a list named `accounts`, or in a dictionary where the keys are account names and the values are Account objects. I don't know what you mean by "calling all objects `account`" because you can have only one object with that name. If that object is some sort of container, however, you can add to it as many Account objects as you wish. – Paul Cornelius Apr 13 '18 at 22:12
0

You are creating a variable accountName and assigning to it a text string object whose contents is the name and date of birth.

Next you're clobbering that and reassigning accountName with a new Account object you're creating. The old value for accountName gets lost when you assign something new to it.

What you need to do is pass name+dob to Account's init function, and in that function, assign the passed account name to an object variable such as self.accountName:

    def __init__(self, name, balance):
        self.accountName = name
        self.balance = balance
...
def createAccount():
    name = input("Client Surname")
    dob = input("client Date of Birth")
    account = Account(name+dob, 0) # opening account balance is £0
    return account

Note that it's a bad idea to use name+dob as the account name, because later it'll be hard to figure out where the name ends and the date of birth begins. But at least, this will get you started.

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • Thanks for this. It has solved the problem i was having (which as you can probably tell was certainly not a real world one!) the trouble is with being new to programming is that often, you don't really know what it is you're trying to do until somebody re-directs you. I am very confused with classes. I am assuming that i can run this function as many times as i like, and it will create x amount of account classes, but each will have self.accountName as a differentiator? if so, that is what i wanted :) – Neildor Apr 12 '18 at 22:30
  • 1
    I'll throw my two cents in and say that I think Neildor would significantly benefit from understanding how object-oriented programming works, and more specifically how to construct and instantiate class objects in Python. https://docs.python.org/3/tutorial/classes.html & best of luck! – Ephexx Apr 12 '18 at 22:32
  • I agree Ephexx, i thought i had the general idea of it, thought i'd try something, and thus highlighted my lack of understanding. Back to the docs. Thanks all! – Neildor Apr 12 '18 at 22:35