Banking Code assignment, calling methods within a class - python

Being relatively new to coding, some of the small issues I have not yet learn to tweak out. In this code my goal is to print the account's name, plus their balance based off of a withdraw, deposit, or simply checking. The name and values will print, however, they are not in a line, as well as "none" prints at the end, and i cannot seem to rid it. I need suggestions on how to fix. I believe the problem stems somewhere within the methods and calling them but am not sure.
class Account:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def Withdraw(self):
if Amount1 > self.balance:
self.withdraw = "You have exceeded your balance."
print (self.withdraw)
elif Amount1 < self.balance:
self.withdraw = (self.balance - Amount1)
print (self.withdraw)
def Deposit(self):
self.deposit = (self.balance + Amount2)
print (self.deposit)
def CheckBalance(self):
self.check = self.balance
print (self.check)
account1 = Account("Barbara", 500.00)
account2 = Account("Joe", 150.00)
account3 = Account("Frank", 20.00)
Action = input("What would you like to do; check balance(c), deposit(d), or withdraw(w)? ")
if Action == "w":
Amount1 = int(input("What amount would you like to withraw?"))
print (account1.name + str(account1.Withdraw()))
print (account2.name + str(account2.Withdraw()))
print (account3.name + str(account3.Withdraw()))
if Action == "d":
Amount2 = int(input("What amount would you like to deposit? "))
print (account1.name + str(account1.Deposit()))
print (account2.name + str(account2.Deposit()))
print (account3.name + str(account3.Deposit()))
if Action == "c":
print (account1.name + str(account1.CheckBalance()))
print (account2.name + str(account2.CheckBalance()))
print (account3.name + str(account3.CheckBalance()))
This is the code i have running so far

Related

NameError: name 'warehouse_initial_quantity_aircondSec' is not defined

remaining code:
if id_parts == "EBS04":
piston_quantity_warehouse = warehouse_initial_quantity_aircondSec + order_from_supplier
return piston_quantity_warehouse
elif id_parts == "BWBS03":
door_quantity_warehouse = warehouse_initial_quantity_bodyworkSec + order_from_supplier
return door_quantity_warehouse
else:
print("No such id")
else:
print("Wrong supplier")
else:
print("Wrong supplier")
print(warehouseUpdateSupplier)
updateBiosWarehouseInventory()
Input:
Enter Supplier name: tab
The id of the part: abs01
How many orders from supplier: 2
Output:
NameError: name 'warehouse_initial_quantity_aircondSec' is not defined
Can anyone help me with this >< Been spending hours on it :\ Thank You so much
The variable warehouse_initial_quantity_aircondSec is local to the method initial_quantity(), you're calling it, but that doesn't give you the variable you have to store it. The name may be different
def initial_quantity():
warehouse_initial_quantity_aircondSec = 1000
return warehouse_initial_quantity_aircondSec
def updateBiosWarehouseInventory():
warehouse_initial_quantity_aircondSec = initial_quantity()
...
You can even do
def initial_quantity():
return 1000

Why are my classes not working properly? even though they work when only one class is active

so my classes won't recognize each other but when left to work alone they do work as seen in the consult printing at the bottom of the post. However the deposit and transfer are popping up the ram location, however, they do work fine when done separately just as shown in the consult action in the bottom part.
<function cashier.deposit at 0x0000023A9B656048>
import datetime
now = datetime.datetime.now()
class account:
owner = "name"
pin = "1823"
balance = 800
def __init__(self, transfer, withdraw, deposit, consult):
self.transfer = transfer
self.withdraw = withdraw
self.deposit = deposit
self.consult = consult
class cashier:
def __init__(self, withdraw, deposit, transfer, consult):
self.transfer = transfer
self.consult = consult
self.withdraw = withdraw
self.deposit = deposit
def deposit(self):
print("Please type pin to proceed")
if account.pin == 1823:
print("who would you like to send money to?")
else:
print("Invalid pin")
def transfer(self):
pass
def withdraw(self):
withdrawal = input("How much money do you want to withdraw? (there is a limit up to 10,000$ a day!)")
account.balance -= int(withdrawal)
if int(withdrawal) > 10000:
print("withdrawals cannot be larger than 10,000 a day!")
exit()
elif int(withdrawal) > account.balance:
print("your account does not have enough funds to complete your transaction")
else:
print("Transaction succesfull!\nPlease collect your money")
print('Today is', now)
print("Hello %s!\nWhat can I do for you today?" % account.owner)
action = input("Use commands such as withdraw, deposit, transfer or consult to continue!\n")
if action == "withdraw":
print(cashier.withdraw)
if action == "consult":
print("Your account's balance is %s $" % str(account.balance))
if action == "transfer":
print(cashier.transfer)
if action == "deposit":
print(cashier.deposit)
else:
print("Uknown command, exiting programm")
exit()
The various functions of cashier do not return anything, so the prints are just printing the string representation of the function. You could have the functions return a string like so:
class Cashier: # PEP8 calls for CamelCase here
def deposit(self):
pin = input("Please type pin to proceed")
if Account.pin == pin:
return "who would you like to send money to?"
else:
return "Invalid pin"
def transfer(self):
pass
def withdraw(self):
withdrawal = input("How much money do you want to withdraw? (there is a limit up to 10,000$ a day!)")
Account.balance -= int(withdrawal)
if int(withdrawal) > 10000:
return "withdrawals cannot be larger than 10,000 a day!"
elif int(withdrawal) > Account.balance:
return "your Account does not have enough funds to complete your transaction"
else:
return "Transaction successful!\nPlease collect your money"
Edit: I removed the initializer, as these weren't doing anything. You need to instantiate your cashier with cashier = Cashier(), then call the functions of Cashier with cashier.withdraw().
There's a fundamental misunderstanding on how classes work here (and OOP in general). You should read through the Python docs on building classes before you go any further.
To use your classes, you need to instantate them like this
Cashier = cashier()//I don't think you should ask for the transfer details while instantiating. But you can do that by adding the parameter in the bracket
Now you can use the functions on the Cashier variable like:-
Cashier.deposit(deposit_money)
I think if you don't want to instantiate and want a function which does not actually store data, you can use a static function but I don't think that is what you want to do

Python saying 'int' object isn't callable

I got a small project for my course, I am to write a program that simulates a person's bank account.
I'll spare the talk, the code is down below, along with the commenting....
# -*- coding: utf-8 -*-
#This program starts by taking in a user input as shown in the While loop..
#the 2 Methods in the class, 1 is for depositing money and the second is for a withdrawal..
class Account:
newBal = 0
post_bal=0
def __init__(self, balance):
self.balance = balance
def deposit(self, deposit):
self.deposit = int(deposit)
#newBal is the variable that takes the new Balance
Account.newBal = self.balance + self.deposit
print("Your Balance is now {}".format(Account.newBal))
return Account.newBal
def withdraw(self, withdraw):
self.withdraw = int(withdraw)
if self.withdraw > Account.newBal:
return "Error, we have a hard time finding that kind of money..."
else:
print("Withdrawal Accepted, have fun with that dough!")
#post_bal is the variable that stores the balance with the withdrawal taken from it
Account.post_bal = Account.newBal - self.withdraw
return("Your Balance is now {}".format(Account.post_bal))
a = Account(balance=0)
while True:
input_1 = input("What would you like to do today? [1] Deposit, [2] Withdraw ")
if int(input_1) == 1:
print(a.deposit(input("How much would you like to deposit? ")))
elif int(input_1) == 2:
print(a.withdraw(input("How much would you like to withdraw? ")))
else:
print(" I'm not too sure I understand what you're saying...")
With this I've been able to run a full loop successfully, depositing and amount and then withdrawing another, with all the outputs being returned. However, once I get either action for the second time in the loop, I get the error call...
TypeError
Traceback (most recent call last)
<ipython-input-4-6a19e620e3d6> in <module>
33 print(a.deposit(input("How much would you like to deposit? ")))
34 elif int(input_1) == 2:
---> 35 print(a.withdraw(input("How much would you like to withdraw? ")))
36 else:
37 print(" I'm not too sure I understand what you're saying...")
TypeError: 'int' object is not callable
I'm not sure what I did wrong here...
You are changing the withdraw to an int here.
def withdraw(self, withdraw):
self.withdraw = int(withdraw)
Use a different name like
def withdraw(self, withdraw):
self.withdraw_value = int(withdraw)

check_balance() function not giving out correct values / values are not stored

I'm new to Python and I'm writing a bank-teller project.
I'm creating a function called check_balance() to check the two types of balances and two other functions one named make_deposit() and the other make_withdrawal().
The thing is my check_balance() function is not giving out the supposedly-updated balances...seems like the make_deposit function did not store the values at all. I'm copy-pasting all codes below, please let me know why it's not working properly. Thanks !!
Code below:
checking_balance - 0
savings_balance = 0
def check_balance(account_type, checking_balance, savings_balance):
if account_type == "savings":
balance = savings_balance
elif account_type == "checking":
balance = checking_balance
else:
return "Unsuccessful, please enter \"checking\" or \"savings\""
balance_statement = "Your " + account_type + " balance is "+ str(balance)
return balance_statement
def make_deposit(account_type, amount, checking_balance, savings_balance):
deposit_status = ""
if amount > 0:
if account_type == "savings":
savings_balance += amount
deposit_status = "successful"
elif account_type == "checking":
checking_balance += amount
deposit_status = "successful"
else:
deposit_status = "Unsuccessful, please enter \"checking\" or \"savings\""
else:
deposit_status = "unsuccessful, please enter an amount greater than 0"
deposit_statement = "Deposit of " + str(amount) + " to your " + account_type + " account was " + deposit_status + " ."
print(deposit_statement)
return checking_balance, savings_balance
print(make_deposit("savings", 10, checking_balance, savings_balance)) ---> this gives a savings_balance of 10.
print(check_balance("savings", checking_balance, savings_balance)) ---> Now this gives 0 instead of 10.
I would recommend creating a class for this project and putting your functions in there. You can modify the code below to better fit your needs. This is a really good article showing how to use a class for a banking project (ironically enough) https://www.geeksforgeeks.org/python-program-to-create-bankaccount-class-with-deposit-withdraw-function/
class Bank_Account:
def __init__(self):
self.Chekingsbalance=0
self.Savingsbalance=0
print("Hello!!! Welcome to the Python bank")
def Deposit(self, AccountType):
if AccountType == "checking":
amount=float(input("Enter amount to be Deposited: "))
self.Chekingsbalance += amount
print("\n Amount Deposited into checking:",amount)
if AccountType == "savings":
amount=float(input("Enter amount to be Deposited: "))
self.Savingsbalance += amount
print("\n Amount Deposited into savings:",amount)
def Balance(self, AccountType):
if AccountType == "savings":
print("\n Net Available Balance=",self.Savingsbalance)
if AccountType == "checking":
print("\n Net Available Balance=",self.Chekingsbalance)
# Driver code
# creating an object of class
s = Bank_Account()
StopBanking = False
while StopBanking ==False:
tasks = (input('What would you like to do? enter [deposit], [check balance], or [quit]'))
# Calling functions with that class object
if tasks == 'deposit':
account = (input('deposit into which account? enter [checking] [savings]'))
if account == 'checking':
s.Deposit(account)
if account == 'savings':
s.Deposit(account)
if tasks == 'check balance':
accountbalance = (input('check balance of which account? enter [checking] [savings]'))
if accountbalance == 'checking':
s.Balance(accountbalance)
if accountbalance == 'savings':
s.Balance(accountbalance)
if tasks == 'quit':
print("thanks for banking!")
StopBanking =True

How to properly use __repr__ when two classes are involved

I am working on some Python code where I create a basic ATM. The issue I am having is I am not able to get the result that I want its printing "<'function Account.balance at 0x012CBC90>" Instead of the actual balance number. So far I have only tested using jsmith. Feel free to call out any other issues that may cause a problem later.
class Account:
def __init__(self,user,pin,balance):
self.user = user
self.pin = pin
self.balance = int(balance)
def get_user(self):
return self.user
def get_pin(self):
return self.pin
def balance(self):
return int(self.balance)
def setBalance(self,newBalance):
self.balance = newBalance
def __repr__(self):
return str(self.user) + " " + str(self.pin) + " " + str(self.balance)
class ATM:
def withdraw(self,Person,amount):
result = Person - amount
return result
def check(self,Person):
Person = Account.balance
return str(Person)
def transfer(self,id1,id2):
pass
def __str__(self):
return self
def main():
Chase = ATM()
Database = []
Teron_Russell = Account("trussell",1738,0)
Joe_Smith = Account("jsmith",1010,1350)
print(Teron_Russell)
Database.append(Teron_Russell)
Database.append(Joe_Smith)
print("Welcome to the ATM")
id = input("Please enter your user ID: ")
pin = input("Enter your pin: ")
chosen = ""
for i in Database:
print("Test1")
name = Account.get_user(i)
print(name)
checkPin = Account.get_pin(i)
print(checkPin)
if id == name and pin == checkPin:
chosen = i
choice = input("What would you like to do. (Type 'Check','Withdraw','Transfer': ")
if(choice == "Check" or "check"):
print(Chase.check(chosen))
# if(choice == "Withdraw" or "withdraw"):
# wAmount = eval(input("How much would you like to Withdraw: "))
# # Chase.withdraw(Account.balance,)
# elif(choice == "Check" or "check"):
# Chase.check()
# else:
# print("Invalid Choice!")
if __name__ == "__main__":
main()
You named a variable and a method the same name, so the interpreter is confused on which one to use. Change the name of either the method or variable balance and you won't have this problem. Additionally, this isn't java, and you shouldn't use classes for no reason. Since you aren't using any instance variables, it is pointless to have all of those methods inside that class.

Categories