Python Forum
Need help with pseuo -bank account using OOP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with pseuo -bank account using OOP
#11
(Jul-08-2017, 10:42 PM)ichabod801 Wrote: 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.
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
Can you explain your code line by line please still a bit confused on how it works. Also how will I be able to link this piece of code to the rest of my own
Reply
#12
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] in 'qQ':
        break
    else:
        print('Invalid pin.')
This is not something you would link to your code. This is an outline of what your code should look like, with a dummy example using strings instead of account objects.

The data dictionary has the pins for the keys, and the "accounts" (strings) for the values.

The outer loop is to get the pin from the user. Basic input of the pin. The if statement checks if the pin is a valid pin, and I will describe that section of the code in the next paragraph. The elif statement (I rewrote it to make it more clear) checks that the input from the user is in 'q' or 'Q', which would indicate quitting the application. If so, it breaks out of the outer loop. The else statement treats everything else as an invalid pin. The loop keeps asking for a pin until the user quits.

If there is a valid pin, the text variable (this would be your account object) gets the value from the data dictionary for the pin. The inner loop then asks for an index. This is where you would ask if they want to deposit, withdraw, get a balance, or whatever. The if statement checks for a negative index, which is the dummy quit command for the inner loop. You would have a quit option that would trigger here. You would then have a bunch of elif statements to handle the valid commands, and an else statement for invalid commands.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
d1 = 0
d2 = 0
d3 = 0
y1 = 0
w1 = 0
pin = 0
class Bank_Account: 
    def pincheck(self, pin):
       if pin in  data:
           return True
       else:
          return False
     # constructor or initializer
    def __init__(self, name, money, pin1):
         self.__name = name
         self.__balance = money   # __balance is private now, so it is only accessible inside the class
         self.__pin = pin1
         

    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
    def next_step():
        while True:
              y2 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
        if y2 == 1:
           enter_pin()
        else:
            print("ERROR, TRY AGAIN")


Bone = Bank_Account('Obi Ezeakachi', 5000, 1111) 
Btwo = Bank_Account('Tasha St.Patrick', 80000 , 2222)
Bthree = Bank_Account('Tommy Egan', 7000, 3333)

data = {Bone:  '1111'  , Btwo :  '2222' , Bthree :   '3333' }
data = {int(pin):value for key, value in data.items()}

def enter_pin():
     while True:
      pin = int( input("Enter pin "))
      if pin == data:
         pincheck()
      else:
       print("INCORRECT PIN TRY AGAIN")

enter_pin()

if Bone.pincheck(pin):
   
   print("Obi Ezeakachi: £",Bone.checkbalance())
   y1 = int(input("Enter 1 if you for a withdrawal, enter 2 if you don't"))
if y1 == 1:
   w1 = input("How much do you want withdraw")
   int(w1)
   print("Withdrawal: £",Bone.withdraw(w1))
   print("Current Balance:",Bone.checkbalance())
   next_step()
else:
   d1 = int(input("How much do you want to deposit"))   
   Bone.deposit(d1)   
   print("Current Balance:",Bone.checkbalance())
   next_step()
if Btwo.pincheck(pin):
   print("Tasha St.Patrick:",Btwo.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
if y1 == 1:
   w2= int(input("How much do you want to withdraw"))
   print(Btwo.withdraw(w2))
else:
   d2= int(input("How much do you want to deposit"))
   Btwo.deposit(d2)
   print("Current Balance:",Btwo.checkbalance())
   next_step()
if Bthree.pincheck(pin):
    
   print("Tommy Egan:",Bthree.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))   
if y1 == 1:
   w3= int(input("How much do you want to withdraw"))
   print("Withdrawal:",Bthree.withdraw(w3))
   next_step()
else:
   d3= int(input("How much do you want to deposit"))
   Bthree.deposit(d3) 
   print("Current Balance:",Bthree.checkbalance())
   next_step()
Reply
#14
Again, this is bad format. You are checking each account's pin, requiring you to repeat code and requiring you to add code every time you add an account.

Your data global is backwards. The pins should be the keys, the accounts should be the values. Then you can see if the pin is in the data (pin in data not pin == data), and get the account out of the data with data[pin]. Then you take that account and run it through ONE set of code that checks for withdrawl or deposit.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
(Jul-22-2017, 09:06 PM)ichabod801 Wrote: Again, this is bad format. You are checking each account's pin, requiring you to repeat code and requiring you to add code every time you add an account.

Your data global is backwards. The pins should be the keys, the accounts should be the values. Then you can see if the pin is in the data (pin in data not pin == data), and get the account out of the data with data[pin]. Then you take that account and run it through ONE set of code that checks for withdrawl or deposit.

d1 = 0
d2 = 0
d3 = 0
y1 = 0
w1 = 0
pin = 0
class Bank_Account: 
    def pincheck(self, pin):
       if pin in  data:
           data[pin]
           return True
       else:
          return False
     # constructor or initializer
    def __init__(self, name, money, pin1):
         self.__name = name
         self.__balance = money   # __balance is private now, so it is only accessible inside the class
         self.__pin = pin1
         

    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
    def next_step():
        while True:
              y2 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
        if y2 == 1:
           enter_pin()
        else:
            print("ERROR, TRY AGAIN")


Bone = Bank_Account('Obi Ezeakachi', 5000, 1111) 
Btwo = Bank_Account('Tasha St.Patrick', 80000 , 2222)
Bthree = Bank_Account('Tommy Egan', 7000, 3333)

data = {'1111':  Bone  , '2222' :  Btwo , '3333' :  Bthree }
data = {int(pin):value for key, value in data.items()}

def enter_pin():
     while True:
      pin = int( input("Enter pin "))
      if pin in data:
         data[pin]
         return True
      else:
       print("INCORRECT PIN TRY AGAIN")

enter_pin()

if Bone.pincheck(pin):
   
   print("Obi Ezeakachi: £",Bone.checkbalance())
   y1 = int(input("Enter 1 if you for a withdrawal, enter 2 if you don't"))
if y1 == 1:
   w1 = input("How much do you want withdraw")
   int(w1)
   print("Withdrawal: £",Bone.withdraw(w1))
   print("Current Balance:",Bone.checkbalance())
   next_step()
else:
   d1 = int(input("How much do you want to deposit"))   
   Bone.deposit(d1)   
   print("Current Balance:",Bone.checkbalance())
   next_step()
if Btwo.pincheck(pin):
   print("Tasha St.Patrick:",Btwo.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
if y1 == 1:
   w2= int(input("How much do you want to withdraw"))
   print(Btwo.withdraw(w2))
else:
   d2= int(input("How much do you want to deposit"))
   Btwo.deposit(d2)
   print("Current Balance:",Btwo.checkbalance())
   next_step()
if Bthree.pincheck(pin):
    
   print("Tommy Egan:",Bthree.checkbalance())
y1 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))   
if y1 == 1:
   w3= int(input("How much do you want to withdraw"))
   print("Withdrawal:",Bthree.withdraw(w3))
   next_step()
else:
   d3= int(input("How much do you want to deposit"))
   Bthree.deposit(d3) 
   print("Current Balance:",Bthree.checkbalance())
   next_step()
This is what I've done so far. But i'm still a bit confused by what you mean by running it through a one set set of code, my own existing code that checks for deposits or withdrawals. Or make a new section of code that does this.
Reply
#16
You want to return data[pin] from enter_pin, to get the account they logged into. Then you change your call to enter_pin on line 58 to account = enter_pin(). Now you've go the account they have logged into, and you don't need to check the pin. So you don't need line 60. For line 62 on, you don't use Bone.checkbalance() and Bone.withdraw(w1), you use account.checkbalance() and account.withdraw(w1). Then line 75 onward can just be deleted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#17
d1 = 0
d2 = 0
d3 = 0
y1 = 0
w1 = 0
pin = 0
class Bank_Account: 
    def pincheck(self, pin):
       if pin in  data:
           data[pin]
           return True
       else:

          return False
          print("WRONG PIN")
     # constructor or initializer
    def __init__(self, name, money, pin1):
         self.__name = name
         self.__balance = money   # __balance is private now, so it is only accessible inside the class
         self.__pin = pin1
         

    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
    def next_step():
        while True:
              y2 = int(input("Enter 1 if you want to make a withdrawal, enter 2 if you don't"))
        if y2 == 1:
           enter_pin()
        else:
            print("ERROR, TRY AGAIN")


Bone = Bank_Account('Obi Ezeakachi', 5000, 1111) 
Btwo = Bank_Account('Tasha St.Patrick', 80000 , 2222)
Bthree = Bank_Account('Tommy Egan', 7000, 3333)

data = {'1111':  Bone  , '2222' :  Btwo , '3333' :  Bthree }
data = {int(pin):value for key, value in data.items()}

def enter_pin():
     while True:
      pin = int( input("Enter pin "))# keeps asking for the correct pin 
      if pin in data:
         data[pin]
         return True
      else:
       print("INCORRECT PIN TRY AGAIN")

account = enter_pin()

#if Bone.pincheck(pin):  
print("      ",account.checkbalance())
y1 = int(input("Enter 1 if you for a withdrawal, enter 2 if you don't"))
if y1 == 1:
   w1 = input("How much do you want withdraw")
   int(w1)
   print("Withdrawal: £",account.withdraw(w1))
   print("Current Balance:",account.checkbalance())
   next_step()
else:
   d1 = int(input("How much do you want to deposit"))   
   account.deposit(d1)   
   print("Current Balance:",account.checkbalance())
   next_step()
This is the changes I've implemented but i'm still getting the same issue as before
Reply
#18
Why don't you use Ichabod801's suggestion? Perhaps your issue will go away
Reply
#19
I did and i'm still getting the same issue
Reply
#20
You're not returning data[pin] from enter_pin.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different results of code with local account and technical account dreyz64 7 3,689 Mar-05-2020, 11:50 AM
Last Post: dreyz64

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020