Update assigned variable used in multiple functions using a while loop - PYTHON - python

I completed an assignment for a scripting class I'm taking, and it worked fine, but I wanted to take it a step further, which has sent me down a rabbit hole. The script is for an ATM transaction.
I added a while loop to give the user additional options, instead of just ending the program. But I found the my account_balance variable remained equal to the default value set initially (in this case 500.25). I've been trying to find ways to make this update as the user goes through the program, in theory it should go as follows:
User checks balance = $500.25 --------------> balance(account_balance)
User makes deposit = $500.00 ---------------> deposit(account_balance, amount)
User checks balance again = $1,000.25 ---> program always returns with $500.25
To try and make this work, I made another function transactionGo(pin), and set all the other functions inside it, hoping this would update the variable account_balance, that didn't work either, and in fact caused more problems, I feel I'm going down a road I shouldn't.
import sys
# -------------------------- DECLARE variables for balance, deposit, and withdrawal --------------------------
account_balance = float(500.25) # Starting balance indicated by Codio
#balance = 100
amount = 0
#deposit_amount = 0 # Declare variable 'deposit_amount'
#withdrawal_amount = 0 # Declare variable 'withdrawal_amount'
pin = ''
# -------------------------- DEFINE FUNCTIONS - balance, withdrawal, and deposit -----------------------------
def transactionGo (pin):
pin = int(input("Please enter your 5 digit pin number:\n"))
def balance(account_balance): # Define balance function
print("Your current balance is $%.2f" % (account_balance)) # Prints the current available balance
def deposit(account_balance, amount): # Define DEPOSIT function with parameters account_balance and amount
amount = float(input("How much would you like to deposit today?\n")) # Accept user input for the deposit amount, in float format
balance = account_balance + amount # This addition assigns the updated value of the account balance, to the variable 'account_balance'
print("Deposit was $%.2f , your new current balance is $%.2f" % (amount, balance)) # Prints deposit amount and account balance
return balance # Return records the new value of account_balance to reflect accordingly in other transactions
def withdrawal(account_balance, amount): # Define WITHDRAWAL function with parameters account_balance and withdrawal_amount
amount = float(input("How much would you like to withdraw today?\n")) # Accept user input for the withdrawal amount, in float format
if amount > account_balance: # Checking to see if the amount requested, is greater than the amount available
print("Insufficient funds, $%.2f is greater than your account balance of $%.2f" % (amount, account_balance)) # If the amount requested is greater than the account balance, there are insufficient funds
else: # Sufficient amount of funds are available, the function continues
balance = account_balance - amount # Variable 'balance' is assigned to reflect the new available balance
print ("Withdrawal amount was $%.2f, your new current balance is $%.2f" % (amount, balance)) # Prints withdrawal amount and account balance
return balance # Return records the new value of balance to reflect accordingly in other transactions
# Lines 18 and 20 compose a conditional statement with the withdrawal function
# Line 18 => if the requested withdrawal amount is greater than the account balance, the conditional statement stops, and prints to the user there are insufficient funds
# Line 20 => if there are sufficient funds available, the conditional statement continues, updates the 'balance', and outputs to the user their withdrawal amount and new available balance
# ------------------------------------ ACCEPT USER INPUT - D, B, W, or Q -------------------------------------
userAccess = input ("Welcome to Tom & Kate Banking, if you would like to sign into your account, please press (C)ontinue, or (E)xit\n").upper()
if userAccess == 'C':
transactionGo (pin)
userChoice = 'go' # Setting the variable 'userChoice' to 'go', so we can implement a while loop
# Step ONE => Create a WHILE loop to offer the user additional options after they have completed a transaction
while userChoice != 'E': # As long as the user does not select 'E' (Exit), the program will keep looping with user choices
# Step TWO => Ask user what action they would like to proceed with, user input is accepted and assigned to the variable 'userchoice'
userChoice = input ("Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n").upper()
# Step THREE => conditional statement begins based on the value of variable 'userchoice' from user input
# Four branches utilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT
if (userChoice == 'D'): # Accepts input D and proceeds with function 'deposit'
deposit (account_balance, amount) # DEPOSIT function is called with parameters 'balance' and 'amount'
elif (userChoice == 'B'): # Accepts input B and proceeds with function 'balance'
balance (account_balance) # BALANCE function is called with parameter 'balance'
elif (userChoice == 'W'): # Accepts input D and proceeds with function 'withdrawal'
withdrawal (account_balance, amount) # WITHDRAWAL function is called with parameters 'balance' and 'amount'
elif (userChoice == 'E'): # Accepts input E for EXIT
print("Thank you for banking with us.") # There is no function for EXIT, and therefore the user has a printed message ending their session
else:
print("We hope to see you again soon, have a nice day!")
When I run this, the first function runs - transactionGo(pin), but when I go into the other transaction functions, IDLE tells me my variables are undefined.

PROBLEM: The scope of the account_balance variable and the functions within transactionGo are different to how you are using them. For example, if you wish to modify a variable outside of a function you need to declare it global at the top of your function definition.
Furthermore, the functions within transactionGo cannot be accessed anywhere in the file except for the transactionGo function itself as the scope of the functions is just within the transactionGo function and not anywhere else. This is why IDLE tells you the functions are undefined, because they are not a part of the file but a part of the transactionGo function and inside of that function only. So in your code those functions are never called
SOLUTIONS: You need to ensure you understand scope in python. The following link is an amazing explanation of what is meant by scope:
https://www.w3schools.com/python/python_scope.asp
In the following methods used to solve this issue, I will assume you want the functions themselves to change and modify the banking variables themselves.
Method #1: Using the 'global' keyword and fixing the functions scopes
# Describe the problem
# How to learn more about the problem
# Full solutions explained.
import sys
# -------------------------- DECLARE variables for balance, deposit, and withdrawal --------------------------
account_balance = float(500.25) # Starting balance indicated by Codio
#balance = 100
amount = 0
#deposit_amount = 0 # Declare variable 'deposit_amount'
#withdrawal_amount = 0 # Declare variable 'withdrawal_amount'
pin = ''
# -------------------------- DEFINE FUNCTIONS - balance, withdrawal, and deposit -----------------------------
def transactionGo (pin):
pin = int(input("Please enter your 5 digit pin number:\n"))
def balance(account_balance): # Define balance function
print("Your current balance is $%.2f" % (account_balance)) # Prints the current available balance
def deposit(): # Define DEPOSIT function with parameters account_balance and amount
global account_balance # Using the 'global' keyword allows you to edit and modify the value of account_balance
amount = float(input("How much would you like to deposit today?\n")) # Accept user input for the deposit amount, in float format
account_balance += amount # This addition assigns the updated value of the account balance, to the variable 'account_balance'
print("Deposit was $%.2f , your new current balance is $%.2f" % (amount, account_balance)) # Prints deposit amount and account balance # Return records the new value of account_balance to reflect accordingly in other transactions
def withdrawal(): # Define WITHDRAWAL function with parameters account_balance and withdrawal_amount
global account_balance
amount = float(input("How much would you like to withdraw today?\n")) # Accept user input for the withdrawal amount, in float format
if amount > account_balance: # Checking to see if the amount requested, is greater than the amount available
print("Insufficient funds, $%.2f is greater than your account balance of $%.2f" % (amount, account_balance)) # If the amount requested is greater than the account balance, there are insufficient funds
else: # Sufficient amount of funds are available, the function continues
account_balance -= amount # Variable 'balance' is assigned to reflect the new available balance
print ("Withdrawal amount was $%.2f, your new current balance is $%.2f" % (amount, account_balance)) # Prints withdrawal amount and account balance
return balance # Return records the new value of balance to reflect accordingly in other transactions
# Lines 18 and 20 compose a conditional statement with the withdrawal function
# Line 18 => if the requested withdrawal amount is greater than the account balance, the conditional statement stops, and prints to the user there are insufficient funds
# Line 20 => if there are sufficient funds available, the conditional statement continues, updates the 'balance', and outputs to the user their withdrawal amount and new available balance
# ------------------------------------ ACCEPT USER INPUT - D, B, W, or Q -------------------------------------
userAccess = input ("Welcome to Tom & Kate Banking, if you would like to sign into your account, please press (C)ontinue, or (E)xit\n").upper()
if userAccess == 'C':
transactionGo (pin)
userChoice = 'go' # Setting the variable 'userChoice' to 'go', so we can implement a while loop
# Step ONE => Create a WHILE loop to offer the user additional options after they have completed a transaction
while userChoice != 'E': # As long as the user does not select 'E' (Exit), the program will keep looping with user choices
# Step TWO => Ask user what action they would like to proceed with, user input is accepted and assigned to the variable 'userchoice'
userChoice = input ("Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n").upper()
# Step THREE => conditional statement begins based on the value of variable 'userchoice' from user input
# Four branches utilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT
if (userChoice == 'D'): # Accepts input D and proceeds with function 'deposit'
deposit () # DEPOSIT function is called with parameters 'balance' and 'amount'
elif (userChoice == 'B'): # Accepts input B and proceeds with function 'balance'
balance (account_balance) # BALANCE function is called with parameter 'balance'
elif (userChoice == 'W'): # Accepts input D and proceeds with function 'withdrawal'
withdrawal () # WITHDRAWAL function is called with parameters 'balance' and 'amount'
elif (userChoice == 'E'): # Accepts input E for EXIT
print("Thank you for banking with us.") # There is no function for EXIT, and therefore the user has a printed message ending their session
else:
print("We hope to see you again soon, have a nice day!")
Method #2: Making a BankAccount class to store these methods and attributes
BankAccount.py
# This file will contain the class, to keep our project organised
class BankAccount:
def __init__(self, account_balance, pin):
self.account_balance = account_balance # The account_balance is a class attribute
self.pin = pin # Class attribute
def __repr__(self):
return f'Account Balance: {self.account_balance}, Pin: {pin}'
def check_pin(self):
number_of_tries = 3 # Number of pin tries the user gets
for i in range(number_of_tries, 0, -1): # Counts backwards from the number_of_tries to the last digit 0
print(f'Number of attempts remaining: {i}')
while True:
try:
inputted_pin = input('Please enter your 5 digit pin number:\n')
if len(inputted_pin) != 5: # Checks and then handles whether or not the pin is 5 digits
incorrect_pin_length_error_message = 'Please ensure the pin you enter is 5 digits long\n\n'
print(incorrect_pin_length_error_message)
inputted_pin = int(inputted_pin)
if inputted_pin == self.pin: # If the valid pin is the actual pin of this bank account
return True # Allows the user to enter
else:
break # Breaks out of the while loop making onto the next iteration of the for loop
except ValueError: # If the pin entered contains any character that is not a number then this except block will be hit when int(inputted_pin) is called
did_not_enter_all_numbers_error_message = 'Please ensure that you have entered pin number.\n\n'
print(did_not_enter_all_numbers_error_message)
# If there is an error in the way a pin was entered it does not count as an attempt
# Only when the pin is valid does it use up an attempt
return False # Returns False if the user fails to input the correct pin in the number of tries given
def check_balance(self):
"""
Prints the balance of the bank account.
"""
print('Your current balance is $%.2f\n' % self.account_balance)
def make_deposit(self):
try:
amount_to_deposit = input('How much would you like to deposit today?\n')
try:
if len(amount_to_deposit.split('.')[1]) != 2: # Checks whether or not the user inputted two decimal places and two decimals only
more_than_two_decimal_place_error_message = 'Please ensure that your deposit is to two decimal places only.\n\n'
print(more_than_two_decimal_place_error_message)
else:
if amount_to_deposit.count('.') > 1: # Ensures that there is not more than one decimal point in the given input
multiple_decimal_points_error_message = 'Please ensure that the amount you wish to withdraw does not have multiple decimal points.\n\n'
print(multiple_decimal_points_error_message)
else:
self.account_balance += float(amount_to_deposit) # Adds the amount once validated
self.check_balance() # Prints out the current balance of the user
except IndexError: # Occurs if the user does not enter a decimal point
did_not_enter_two_decimal_places_error_message = 'Please ensure that you enter exactly two decimal places in your deposit.\n\n'
print(did_not_enter_two_decimal_places_error_message)
except ValueError: # Ensures that the input are all numbers
did_not_enter_number_error_message = 'Please ensure that you enter a numerical amount.\n\n'
def make_withdrawal(self):
try:
amount_to_withdraw = input('How much would you like to withdraw today?\n')
if len(amount_to_withdraw.split('.')[1]) != 2: # Ensures that there are two decimal places inputted and hta
more_than_two_decimal_place_error_message = 'Please ensure that your withdrawal is to two decimal places only.\n\n'
print(more_than_two_decimal_place_error_message)
else:
if amount_to_withdraw.count('.') > 1: # Ensures that there is not more than one decimal point in the given input
multiple_decimal_points_error_message = 'Please ensure that the amount you wish to withdraw does not have multiple decimal points.\n\n'
print(multiple_decimal_points_error_message)
else:
amount_to_withdraw = float(amount_to_withdraw)
if amount_to_withdraw > self.account_balance: # Ensures that the user has enough funds to withdraw money from
insufficient_funds_message = 'Insufficient funds, $%.2f is greater than your account balance of $%.2f' % (amount_to_withdraw, self.account_balance)
else:
self.account_balance -= amount_to_withdraw # Once validated withdraws the amount from the account
self.check_balance() # Prints the user's balance
except ValueError: # Ensures that the input is only numbers
did_not_enter_number_error_message = 'Please ensure that you enter a numerical amount.\n\n'
def exit_transaction():
exit_message = 'Thank you for banking with us.'
print(exit_message)
def do_transaction():
# The main loop that you defined in your original code.
if self.check_pin():
while True:
user_choice = input('Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n').upper()
if user_choice == 'B':
self.check_balance()
elif user_choice == 'D':
self.make_deposit()
elif user_choice == 'W':
self.make_withdrawal()
elif user_choice == 'E':
self.exit_transaction()
else:
unsuccessful_pin_error_message = 'It seems like you ran out of pin tries, please try again later.'
print(unsuccessful_pin_error_message)
main.py
from BankAccount import BankAccount
person1_bank_account = BankAccount(account_balance=500.25, pin=12345)
person2_bank_account = BankAccount(account_balance=1100.00, pin=54321)
# As many as you want
# Perform any functions on them
person1_bank_account.make_deposit()
person2_bank_account.make_withdrawal()
# Print values
print(person1_bank_account.account_balance)
print(person2_bank_account.pin)
This way your code is more flexible, clean, and scalable. For example in your class you can add ass many methods as you want in whatever way you want, and every instance of that class gains that functionality on top of being easy to manage. You can handle any exception you want in whatever way you want. So this is what I would suggest.

Related

My inputs do not show up when I run my program. Why?

How can I fix the inputs? Because they do not show up when I run my program.
## Define the main module
def main():
## Initialize local Variables / Set Constant
homeValue = 0
propertyTaxRate = 0
bedrooms = 0
BEDROOMSURCHARGERATE = 0.0025
## Set initialized varialbes to user inputs
homeValue = float(input("How much is your home worth?"))
propertyTaxRate = float(input("What is your Property Tax Rate entered as a decimal?"))
bedrooms = int(input("How many bedrooms will your house have?"))
## Set Variables equal to results of module outputs
propertyTax = getPropertyTax(homeValue, propertyTaxRate)
bedroomSurcharge = getBedroomSurcharge(BEDROOMSURCHARGERATE, bedrooms, homeValue)
totalPropertyTax = getTotalPropertyTax(propertyTax, bedroomSurcharge)
## Report All information with a propertyTaxReport
propertyTaxReport(homeValue, propertyTaxRate, bedrooms, propertyTax, bedroomSurcharge, totalPropertyTax)
## Define getPropertyTax Module
def getPropertyTax(homeValue, propertyTaxRate):
## Calculate property tax
propertyTax = homeValue*propertyTaxRate
return propertyTax
## Define getBedroomSurcharge
def getBedroomSurcharge(BEDROOMSURCHARGERATE, bedrooms, homeValue):
## Calculate Bedroom Surcharge
bedroomSurcharge = BEDROOMSURCHARGERATE*bedrooms*homeValue
return bedroomSurcharge
## Define getTotalPropertyTax
def getTotalPropertyTax(propertyTax, bedroomSurcharge):
## Calculate totalProperty Tax
totalPropertyTax = propertyTax + bedroomSurcharge
return totalPropertyTax
## Define propertyTaxReport
def propertyTaxReport(homeValue, propertyTaxRate, bedrooms, propertyTax, bedroomSurcharge, totalPropertyTax):
## Output Variables
print("Your home costs ", homeValue, " dollars.")
print("Your property tax rate is ", propertyTaxRate)
print("Your house has ", bedrooms, "bedrooms")
print("Your property tax is equal to ", propertyTax, " dollars.")
print("Your bedroom surcharge is equal to ", bedroomSurchage, " dollars")
print("Your total property tax comes out to ", totalPropertyTax, " dollars")
return
I think you are drying a bit with your code.
A few suggestions:
You do not need to initialise a variable if this is an input from the user.
If you can simplify a calculation, just do it. Do not create many functions if they are not necessary.
You do not to print as output variables that the user gives as input. I guess the user already knows that. But if it is a serious report, maybe I can give you that.
Try this and let me know:
def main_function():
BEDROOMSURCHARGERATE = 0.0025
## Set initialized variables to user inputs
homeValue = float(input("How much is your home worth?"))
propertyTaxRate = float(input("What is your Property Tax Rate entered as a decimal?"))
bedrooms = int(input("How many bedrooms will your house have?"))
## Set Variables equal to results of module outputs
propertyTax = homeValue*propertyTaxRate
bedroomSurcharge = BEDROOMSURCHARGERATE*bedrooms*homeValue
totalPropertyTax = propertyTax + bedroomSurcharge
## Report all information with a propertyTaxReport
return (totalPropertyTax, f"bedroom Surcharge was {bedroomSurcharge}")
To call the function just do:
main_function()
You can return this tuple or print the whole thing you have at the end, up to you. I just point out that you can return just a value or a sentence.

Unit Test functions which include user input, while loops and whose input is another functions output - Python

I have made a program for an ATM machine, which checks the pin that a user inputs, allows the user to enter an amount to withdraw if the pin matches and then withdraws the money from the account_balance. However, each function is seemingly dependent: the return of one function is the parameter of another. This is confusing to test each function separately and when I try to do so, the whole program runs and asks for input from each function.
As my get_pin and get_amount function also have while loops with user input, I feel like this has added another complication to the way I need to unit test.
Is there a way to do this?
Or do you think it is best to refactor my code to remove user input and while loops and just have parameters in each individual function?
The test I would like to check would be:
withdraw_cash function - to check if the correct amount is withdrawn
get_pin function - check if the pin matches
get_amount function - check incorrect amount is added bringing up an error message.
My atm program is:
user = {
'pin': 1234
}
def withdraw_cash(amount):
balance_account = 100
if amount > balance_account:
raise ValueError("You don't have sufficient balance to make this withdrawal")
else:
new_balance = balance_account - amount
return new_balance
def get_pin():
count = 0
to_exit = False
while (count < 3) and (not to_exit):
try:
pin = int(input('Please enter your four digit pin: '))
except ValueError:
print("Please enter correct pin")
count += 1
if pin != user['pin']:
print("Pin does not match.. Try Again")
count += 1
else:
return get_amount(pin)
if count == 3:
a = '3 UNSUCCESFUL PIN ATTEMPTS, EXITING \n !!!!!YOUR CARD HAS BEEN LOCKED!!!!!'
return a
def get_amount(pin):
while True:
try:
amount = int(input("Enter the amount of money you want to withdraw: "))
except ValueError as v:
print(f"Enter correct amount: ")
else:
return withdraw_cash(amount)
try:
get_pin()
except ValueError as v:
print(f"ERROR: {v}")

How do I print the actions done by previous functions using for loops?

As an assignment I am making a simple interface in python which can show a balance, add to that balance, take out money from that balance, check the interest rate and then lastly check the three previous actions done. This will be choice == 5. So what do I write in the bottom def in order to do so?
usd = 500
def main():
print("""
1. Balance
2. Add
3. Retrieve
4. Interest
5. Last Changes
""")
choice = int(input("Choose: "))
if choice == 1:
global usd
balance()
elif choice == 2:
add()
elif choice == 3:
retrieve()
elif choice == 4:
interest()
elif choice == 5:
changes()
def balance():
global usd
print("Balance: ", round((usd),2))
main()
def add():
global usd
amount = int(input("How much do you want to add? "))
usd = amount + usd
print("New balance = ", round((usd),2))
main()
def retrieve():
global usd
amount = int(input("How much do you want to retrieve: "))
usd = usd - amount
print("New balance = ", round((usd),2))
main()
def interest():
global usd
if usd<=1000000:
usd = ((usd/100)*101)
print("New balance: ", round(((usd/100)*101), 2))
elif usd>=1000000:
usd = ((usd/100)*102)
print("New balance: ", round(((usd/100)*102), 2))
main()
def changes():
main()
main()
The desired output will look a little like this;
Choose: 5
+6105
-500000
+1110000
It sounds like you want to keep a log of the previous actions. You could do this by creating a list and appending a new entry each time an action is done. Then your changes function could print out the last 3 items in the list.
You could make the list global and access it in the same way you access usd.
You could also make the list in main and pass it to changes as an argument. If you decide to do it this way, you could make each function return their log so that you can append it to the list in main.
For example (using only the add function illustrate)
Using a global variable (this is bad practice, but is shorter):
usd = 500
log = []
def add():
global usd
amount = int(input("How much do you want to add? "))
usd = amount + usd
print("New balance = ", round((usd),2))
log.append(f"+{amount}") # add change to log
main()
def changes():
# print last 3 items in log
for change in log[-3:]:
print(change)
main()
Or the more typical way, using a loop (without a global variable)
usd = 500
def main():
log = []
choice = 0
while choice != 6:
print("""
1. Balance
2. Add
3. Retrieve
4. Interest
5. Last Changes
6. Quit
""")
choice = int(input("Choose: "))
if choice == 1:
balance()
elif choice == 2:
log.append(add()) # add change to log
elif choice == 3:
log.append(retrieve()) # add change to log
elif choice == 4:
interest()
elif choice == 5:
changes(log)
def add():
global usd
amount = int(input("How much do you want to add? "))
usd = amount + usd
print("New balance = ", round((usd),2))
return f"+{amount}"
def changes(log):
# print last 3 items in log
for change in log[-3:]:
print(change)
main()
A potential problem with this approach is because you are adding items to the log list indefinitely, theoretically you may eventually run out of memory on your computer. To remedy, you can remove extra logs from the list whenever the length of the list is greater than 3.
Documentation for lists: https://docs.python.org/3/tutorial/datastructures.html
Global variables are bad practice in general, so it would be preferable to avoid making usd a global variable, but I'll leave that up to you

sqlite withdraw/deposit function in python

The last stretch of my banking project I've been working on is the deposit/withdraw function. I've gotten most of the other bits working (outside of cleaning up the code) I'm not fully understanding how one adds and subtracts a number from a sql statement inside of python...so here's the part of the code I'm struggling with:
here are my tables:
sqlite_file = 'banking2_db.sqlite'
table_1 = 'Bartertown'
table_2 = 'PINs'
id_column = 'Card_Numbers'
column_1 = 'Character_PINs'
column_2 = 'Balances'
column_3 = 'Card_Numbers'
column_4 = 'Characters'
class LoginPrompt:
def Login(self):
while True:
print(menu[1])
self.Card_number=str(input('>> '))
print(menu[2])
while True:
try:
self.Character_PINs = getpass.getpass('>> ')
self.one_row = c.execute('SELECT * FROM {tn} WHERE {cn}=? and {cnn}=?'.\
format(tn=table_1, cn=column_1, cnn=column_3), (self.Character_PINs, self.Card_number,))
for row in self.one_row.fetchone():
print('Welcome: ', row)
input('Press any key to continue... ')
return
except:
print('PIN incorrect; try again')
break
#MINOR ISSUE, ONLY QUERIES CHAR COLUMN
def loginMenu(self):
while True:
print(menu[5])
print("\n1 - Deposit funds")
print("2 - Withdraw funds")
print("3 - Check balance")
print("4 - Reset Pin")
print("5 - Exit")
while True:
try:
choice = int(input("Please enter a number: "))
except ValueError:
print("Please choose a valid entry")
if choice >= 1 and choice <=5:
choice == 1:
amount = input("\nPlease enter the deposit amount: ")
if amount != '' and amount.isdigit():
int(amount)
balance = c.execute('UPDATE {tn} SET {cn} = Balances +:amount WHERE Card_Numbers =:self.Card_number' .\
format(tn=table_1, cn=column_2,))
new_bal = balance + (int(amount))
print('${} has been deposited to account {} and the new balance is ${}'.\
format(amount, self.Card_number, balance + (int(amount))))
for row in self.Balances.fetchone():
print('Your new balance is: ', new_bal)
return self.loginMenu()
basically, I'm trying to make sure the program can only pull the balance where the PIN and the Card Number are specified. It selects the balance (This part is working, it is another option in the menu) however, the UPDATE function is still a mystery to me. I understand how to update both the ENTIRE column...on accident, and also how to change the value presented in the Balance field to the value the user submitted ie: the user selects to deposit 100 caps, and then their balance becomes 100 caps. The column I'm trying to update is called Balances, Card_Numbers is the column containing the users "credit card" and amount is the value the user just entered.
Thank you for your help.
edit: added tables, and initial input of the data.
If you want to update the column Balances then your statement should be:
...SET Balances = Balances + :amount...
so do it like this:
c.execute("UPDATE " + table_1 + " SET Balances = Balances + ? WHERE Card_Numbers = ?", (amount, self.Card_number,))
Finally figured it out, silly me.
choice == 1:
amount = input("\nPlease enter the deposit amount: ")
if amount != '' and amount.isdigit():
int(amount)
balance = c.execute('UPDATE {tn} SET {cn} = Balances +:amount WHERE Card_Numbers =:self.Card_number' .\
format(tn=table_1, cn=column_2,))
new_bal = balance + (int(amount))
I was trying to reference it by printing the output of the of the db while it hadn't been committed yet, :eyeroll:
Thank you for the help forpas, it worked like a charm.

Functions And While Loop Complication In Python

def main():
totalprofit = 0
stockname = input("Enter the name of the stock or -999 to quit: ")
while stockname != "-999":
sharesbought, purchasingprice, sellingprice, brokercommission = load()
amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss = calc(sharesbought, purchasingprice, sellingprice, brokercommission)
output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofpaidcommission, profitorloss)
stockname = input("Enter the name of the next stock (or -999 to quit): ")
totalprofit += profitorloss
print("\n Total profit is: ", format(totalprofit, '.2f'))
def load():
sharesbought = int(input("Number of shares bought: "))
purchasingprice = float(input("Purchasing price: "))
sellingprice = float(input("Selling price: "))
brokercommission = float(input("Broker commission: "))
return sharesbought, purchasingprice, sellingprice, brokercommission
def calc(sharesbought, purchasingprice, sellingprice, brokercommission):
amountpaid = sharesbought * purchasingprice
amountofpaidcommission = amountpaid * (brokercommission/100)
amountstocksoldfor = sharesbought * sellingprice
amountofsoldcommission = amountstocksoldfor * (brokercommission/100)
profitorloss = (amountpaid + amountofpaidcommission) - (amountstocksoldfor - amountofsoldcommission)
return amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss
def output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss,):
print("\n Stock name: ", stockname, sep = '')
print("Amount paid for the stock: ", format(amountpaid, '.2f'))
print("Commission paid to broker when the stock was bought: ", format(amountofpaidcommission, '.2f'))
print("Amount the stock sold for: ", format(amountstocksoldfor, '.2f'))
print("Commission paid to broker when the stock was sold: ", format(amountofsoldcommission, '.2f'))
print("Profit or loss: ", format(profitorloss, '.2f'))
main ()
The objective of the first function is to allow a user to input the followings as many times as she or he wants until the user decides is done:
Stock name
Shares bought
Selling price
Broker commission
My main problem is then in the main function. I am skeptical whether I am using the while loop correctly or if it's correct at all. I tried to run the program but it won't output anything.
Also, shouldn't I add this at the end of the program with the values inputted to call all the functions above:
def main()
load()
calc()
output()
Or is it fine within the while loop?
I think a while loop is perfectly appropriate for this use case, where you want to loop an indeterminate number of times, stopping when some condition is not met.
There is one obvious problem, on this line:
stockname +=1
This doesn't make any sense. Since stockname is a string, you can't add one to it. Instead, you should be asking the user for the next stock name (or a "special" value to signal they're done). Try replacing that line with something like:
stockname = input("Enter the name of the next stock (or -999 to quit): ")
The rest of your code appears correct, if rather verbose. Unless you think it's likely you'll call some of your other functions in some other place in your code, it might be simpler and cleaner to include all the logic in one function. Functions are nice, but you should balance the benefits of isolating each part of your code in its own function against the effort of passing lots of values between them.

Categories

Resources