I have a glitch in a code that I wanted to solve: it’s on No. 19 of the “Bank Teller Project”. For some reason I can’t understand, the code tries to transfer 25 from savings to checking despite when calling the acc_transfer() with the amount argument as 5. Anyone have any ideas why this may be happening? Thanks in advance.
checking_balance = 0
savings_balance = 0
def check_balance(account_type, checking_balance, savings_balance):
# My own input: Input foolproofing
account_type = account_type.lower()
if account_type == "saving":
account_type = "savings"
elif account_type == "checkings":
account_type = "checking"
# Step2 Checking if it is a checking balance or a savings balance
if account_type == "checking":
# Step 4 Assigning the checking balance
balance = checking_balance
elif account_type == "savings":
# Step 3: Assigning the savings balance
balance = savings_balance
# Step 5: Error handling.
else:
return "Unsuccessful, please enter \"checking\" or \"savings\""
# Step 6: Creating a balance statement
balance_statement = "Your %s balance is %s" % (account_type, balance)
return balance_statement
# 3. Calling and Printing the check_balance() function for Checking Account
print(check_balance("checking", checking_balance, savings_balance))
# 4. Calling and Printing the check_balance() function for Savings Account
print(check_balance("savings", checking_balance, savings_balance))
# 5. make_deposit function
def make_deposit(account_type, amount, checking_balance, savings_balance):
# My own input: Input foolproofing
account_type = account_type.lower()
if account_type == "saving":
account_type = "savings"
elif account_type == "checkings":
account_type = "checking"
# Step 2. Defining deposit_status as an empty string
deposit_status = ""
# Step 3. if statement that checks whether the passed in amount is greater than 0
if amount > 0:
# Step 5. Sorting between savings and checking
if account_type == "savings":
# Step 6: Depositing to savings account
savings_balance += amount
deposit_status = "successful"
# Step 7:: If the account is a checking balance
elif account_type == "checking":
checking_balance += amount
deposit_status = "successful"
# Step 8: Error message if account type input error
else:
deposit_status = "unsuccessful, please enter \"checking\" or \"savings\""
# Step 4. Error message if amount less than zero
else:
deposit_status = "unsuccessful, please enter an amount greater than 0."
# Step 9: Outside of all the if statements but still in the function, compose a statement composing of strings and variables used in this function. Then assign it to the new deposit_statement variable. The statement should be: "Deposit of amount to your account_type account was deposit_status".
deposit_statement = "Deposit of %s to your %s account was %s" % (
amount, account_type, deposit_status)
# Step 10: Printing deposit statement
print(deposit_statement)
# Step 11: Returning both savings balance and checking balance
return checking_balance, savings_balance
# 6 call the make_deposit() function with these arguments; "savings", 10,checking_balance and savings_balance
checking_balance, savings_balance = make_deposit(
"savings", 10, checking_balance, savings_balance)
# 7 Print savings balance call after making a savings deposit
print(check_balance("savings", checking_balance, savings_balance))
# 8 call the make_deposit() function with these arguments; "checking", 200,checking_balance and savings_balance
checking_balance, savings_balance = make_deposit(
"checking", 200, checking_balance, savings_balance)
# 9 check_balance() function with these arguments; "checking", checking_balance and savings_balance and print it
print(check_balance("checking", checking_balance, savings_balance))
# 10 Making a withdrawal function
# Step 1: Defining function
def make_withdrawal(account_type, amount, checking_balance, savings_balance):
# My own input: Input foolproofing
account_type = account_type.lower()
if account_type == "saving":
account_type = "savings"
elif account_type == "checkings":
account_type = "checking"
# Step 2: Initialising withdrawal_status
withdrawal_status = ""
# Step 3: Initialise error message
fail = "unsuccessful, please enter an amount less than balance."
# Step 4: Check account_type
if account_type == "savings":
# Step 5: Withdrawal from savings account
# Step 6: Ensuring withdrawal amount is no bigger than the savings balance
if amount <= savings_balance:
# Step 7: SUbtracting amount from the savings account and updating withdrawal status
savings_balance -= amount
withdrawal_status = "successful"
else:
withdrawal_status = fail
# Step 8 Withdrawing from checking account
elif account_type == "checking":
# Step 9 Ensuring amount does not exceed balance
if amount <= checking_balance:
# Step 10: Subtracting amount from checking balance
checking_balance -= amount
withdrawal_status = "successful"
else:
withdrawal_status = fail
# Step 11: Resolving input errors
else:
withdrawal_status = "unsuccessful. Please enter either \"checking\" or \"savings\"."
# Step 12: Creating a withdrawal statement
wtihdrawal_statement = "Youur withdrawal of %s from %s was %s" % (
amount, account_type, withdrawal_status)
# Step 13: Printing withdrawal statement
print(wtihdrawal_statement)
return checking_balance, savings_balance
# 11. call the make_withdrawal() function with these arguments: "savings", 11,checking_balance and savings_balance. Assign the function call to the matching checking_balance and savings_balance variables
checking_balance, savings_balance = make_withdrawal(
"savings", 11, checking_balance, savings_balance)
# 12. Call the check_balance() function with these arguments; "savings", checking_balance and savings_balance within a print function. Your new savings balance should print.
print(check_balance("savings", checking_balance, savings_balance))
# 13. call the make_withdrawal() function with these arguments; "checking", 170,checking_balance and savings_balance. Assign the function call to the matching checking_balance and savings_balance variables that are also being returned by the function.
checking_balance, savings_balance = make_withdrawal(
"checking", 170, checking_balance, savings_balance)
# 14. Call the check_balance() function with these arguments; "checking", checking_balance and savings_balance within a print function
print(check_balance("checking", checking_balance, savings_balance))
# 15 15. Create a function to make a transfer between accounts
# Step 1: define function
def acc_transfer(acc_from, acc_to, amount, checking_balance, savings_balance):
# My own input: Input foolproofing
acc_from = acc_from.lower()
if acc_from == "saving":
acc_from = "savings"
elif acc_from == "checkings":
acc_from = "checking"
acc_to = acc_to.lower()
if acc_to == "saving":
acc_to = "savings"
elif acc_to == "checkings":
acc_to = "checking"
# Step 2: Initialise transaction_status
transaction_status = ""
# Step 3: Initialise error message
trans_error = "unsuccessful. Please enter amount less than "
# Step 4: Account transfer
# Checking to savings
if acc_from == "checking" and acc_to == "savings":
# Step 5: Ensuring amount does not exceed checking balance
if amount <= checking_balance:
# Step 6: Transferring amount from checking to savings
amount -= checking_balance
amount += savings_balance
transaction_status = "successful"
else:
transaction_status = trans_error + str(checking_balance)
# Savings to checking
elif acc_from == "savings" and acc_to == "checking":
# Step 7. Ensuring amount does not exceed savings account
if amount <= savings_balance:
#Step 8: Transferring from savings to checking (see Step 6 for corresponding transfer)
amount -= savings_balance
amount += checking_balance
transaction_status = "successful"
else:
transaction_status = trans_error + str(savings_balance)
#Step 9: Error in the else statement
else:
transaction_status = "unsuccessful, please enter \"checking\" or \"savings\""
#Step 10: Creating transfer_statement
transaction_statement = "Transfer of %s from your %s to your %s account was %s." % (amount, acc_from, acc_to, transaction_status)
#Step 11: Printing transfer_statement
print (transaction_statement)
#Step 12: Return checking statement and savings statement
return checking_balance, savings_balance
#16. call the acc_transfer() function with these arguments; "checking", "savings", 40,checking_balance and savings_balance. Assign the function call to the matching checking_balance and savings_balance
checking_balance, savings_balance = acc_transfer("checking", "savings", 40, checking_balance, savings_balance)
#17. print your checking balance. Call the check_balance() function with these arguments; "checking", checking_balance and savings_balance within a print function.
print(check_balance("checking", checking_balance, savings_balance))
#18. Print savings
print(check_balance("savings", checking_balance, savings_balance))
#19 Transfer 5 from savings to checking (I transfer 25 on VSCode somehow despite amount being 5)
checking_balance, savings_balance = acc_transfer("savings", "checking", 5, checking_balance, savings_balance)
print(check_balance("checking", checking_balance, savings_balance))
print(check_balance("savings", checking_balance, savings_balance))