Python Forum

Full Version: Need help with pseuo -bank account using OOP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Can someone please help me i'm creating a code for an imitated bank account, what i'm trying to get it to is to basically display the name of the account holder and their current balance when a pre-determined pin number is entered into the account. But when run the code it seems to skip over the step to print the account holder's details, and print the error message when the right pin is entered in. Here's the code below. Any help would be appreciated, thanks

class BankAccount:
 
     # constructor or initializer
    def __init__(self, name, money):
         self.__name = name
         self.__balance = money   # __balance is private now, so it is only accessible inside the class
 
    def deposit(self, money):
         self.__balance += money
 
    def withdraw(self, money):
         if self.__balance > money :
             self.__balance -= money
             return money
         else:
             return "Insufficient funds"
 
    def checkbalance(self):
         return self.__balance
 
b1 = BankAccount('Obi Ezeakachi', 5000)
b2 = BankAccount('Tasha St.Patrick', 80000)
b3 = BankAccount('Tommy Egan', 7000)
d1 = 0
d2 = 0
d3 = 0
pin1 = 1111
pin2 = 2222
pin3 = 3333
pin = input("Enter Pin")
if pin1 & pin:   
   print("Obi Ezeakachi: £",b1.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
else:
   print("ERROR! TRY AGAIN")  
if y1 == 1:
   w1= int(input("How much do you want to withdraw"))
else:
   d1= int(input("How much do you want to deposit"))   
   print("Withdrawal: £",b1.withdraw(w1))
   b1.deposit(d1)   
   print("Current Balance:",b1.checkbalance())                                        
                                            
pin = input("Enter pin")
if pin2 == pin:  
   print("Tasha St.Patrick:",b2.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
elif pin2 != pin:
    print("ERROR! TRY AGAIN")
if y1 == 1:
   w2= int(input("How much do you want to withdraw"))
   print(b2.withdraw(w2))
else:
   d2= int(input("How much do you want to deposit"))
   b2.deposit(d2)

if pin3 == pin:
   print("Tommy Egan:",b3.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))   
else:   
   print("ERROR! TRY AGAIN")
if y1 == 1:
   w3= int(input("How much do you want to withdraw"))
   print("Withdrawal:",b3.withdraw(w3))
else:
   d3= int(input("How much do you want to deposit"))
   b3.deposit(d3) 

print("Current Balance:",b3.checkbalance())
Please use python tags, I added them for you. See the BBCode tutorial link in my signature for more info.

First of all, the code after the class definition should be loops. Have an outer loop where you ask for the pin. Once you match the pin to an account, have an inner loop that allows for withdrawls, deposits, and balance inquiries, until they are done.

Second, I would store the objects in a dictionary, with the pins being the keys. Then if the pin is in the dictionary, you can have the inner loop on that object. If it isn't, you can give the login error.
(Jul-08-2017, 05:18 PM)ichabod801 Wrote: [ -> ]Please use python tags, I added them for you. See the BBCode tutorial link in my signature for more info. First of all, the code after the class definition should be loops. Have an outer loop where you ask for the pin. Once you match the pin to an account, have an inner loop that allows for withdrawls, deposits, and balance inquiries, until they are done. Second, I would store the objects in a dictionary, with the pins being the keys. Then if the pin is in the dictionary, you can have the inner loop on that object. If it isn't, you can give the login error.
Thanks but can you you show me how to do that in my code please
(Jul-08-2017, 05:54 PM)obieze998 Wrote: [ -> ]can you you show me how to do that in my code please

That's not how we do things around here. The way it works is that you write some code, you tell us exactly how it isn't working, and we help you fix it. I'm more generous than most here, in that I will (and did) give you an outline of what you need to do. Now it's your turn.
(Jul-08-2017, 06:59 PM)ichabod801 Wrote: [ -> ]
(Jul-08-2017, 05:54 PM)obieze998 Wrote: [ -> ]can you you show me how to do that in my code please

That's not how we do things around here. The way it works is that you write some code, you tell us exactly how it isn't working, and we help you fix it. I'm more generous than most here, in that I will (and did) give you an outline of what you need to do. Now it's your turn.

Ok let's try this again can you be a bit more specific by what you mean by using outer and inner loops. I'm finding it hard to visualise how to link the input of the pin to the accounts
Sorry, I got my posts mixed up. I thought this was one without any code.

data = {'1': 'abcde', '2': '2357', '3': '11235'}
while True:
    pin = input('pin? ')
    if pin in data:
        text = data[pin]
        while True:
            index = int(input('index? '))
            if index < 0:
                break
            else:
                print(text[index])
    elif pin[0] not in 'qQ':
        print('Invalid pin.')
    else:
        break
Untested, but that's the general idea.
(Jul-08-2017, 07:19 PM)ichabod801 Wrote: [ -> ]Sorry, I got my posts mixed up. I thought this was one without any code.

data = {'1': 'abcde', '2': '2357', '3': '11235'}
while True:
    pin = input('pin? ')
    if pin in data:
        text = data[pin]
        while True:
            index = int(input('index? '))
            if index < 0:
                break
            else:
                print(text[index])
    elif pin[0] not in 'qQ':
        print('Invalid pin.')
    else:
        break
Untested, but that's the general idea.
What if I were to do:
for Val in account:
if pin1 == pin:
Print...............
else:
print(error)
Sorry but I'm a beginner at this so I don't understand your code ?%
Please use python tags, as I requested in my first post.

The problem with checking pin1 == pin is that you are only checking one account. You would have to use a separate if clause for each account. And every time someone added an account you would have to rewrite your code. You need to have some way to search the entered pin to see if matches any account. The dictionary I showed you is one way to do that.

What part of my code don't you understand? The only things I'm using that you aren't are dictionaries and loops. Those are very basic features of the language that you should understand before messing around with classes.
(Jul-08-2017, 08:01 PM)ichabod801 Wrote: [ -> ]Please use python tags, as I requested in my first post.

The problem with checking pin1 == pin is that you are only checking one account. You would have to use a separate if clause for each account. And every time someone added an account you would have to rewrite your code. You need to have some way to search the entered pin to see if matches any account. The dictionary I showed you is one way to do that.

What part of my code don't you understand? The only things I'm using that you aren't are dictionaries and loops. Those are very basic features of the language that you should understand before messing around with classes.

I don't understand your use of dictionaries in the code at all, your code is a bit confusing. Though i think i have found a different way to create a pin checker for this code below:
[python][ def pincheck(self):
pin = input("Enter Pin")
if pin == self.__pin
return True
else:
print("Error try again")
/python]
Sorry i that didn't use the python tag, still getting to grips with this website
You are still just checking one at a time. That is not going to solve the problem. You need to get the pin from the user, and then find the account that the pin matches. You are getting the account, getting the pin from the user, and seeing if it matches that account. Then you're saying it's an error when it doesn't match that particular account, even if they entered a valid pin for another account.

In my data dictionary, the keys (1, 2, 3) are the pins. By using pin in data I check that the pin they entered is any valid pin, and only give them an error if their pin doesn't match any account. Then I give the "account" (in this case a string) to the inner loop and let the user do things with it.
Pages: 1 2 3