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
3
  • 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. Commented Apr 12, 2018 at 22:02
  • Possible duplicate of How do I create a variable number of variables? Commented Apr 12, 2018 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. Commented Apr 12, 2018 at 22:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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.

3 Comments

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 :)
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. docs.python.org/3/tutorial/classes.html & best of luck!
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.