[Tutor] [maryknauth at mac.com: Re: Input parameters for functions]

Mary Knauth maryknauth at mac.com
Sat Jun 20 09:08:27 EDT 2020


David,

Thank you, I’ll remember to reply to the list from now on :)

You actually touched down on another question I had in my head, the value of accout_balance is temporary just as you stated.  I think I need to add return to my functions.

‘return account_balance = account_balance + deposit_amount’ is an invalid syntax, so I need to read more about how to utilize return.

Correct?

Warm Regards,

Mary Knauth
954-412-9280
maryknauth at mac.com



> On Jun 19, 2020, at 7:47 PM, David Rock <david at graniteweb.com> wrote:
> 
> Mary,
> 
> I've forwarded your reply to the mailing list. Remember to reply to the list
> email and not individuals so others can see the conversation (both to help and
> to learn).  Comments inline below
> 
>> ----- Forwarded message from Mary Knauth <maryknauth at mac.com> -----
>> 
>> Hello David,
>> 
>> Thanks very much for the welcome, I’ve written a couple question so far to the
>> python tutor group, and it’s amazing what knowledge you all give back, so thank
>> you!
>> 
>> Here is my code:
>> 
>> import sys
>> 
>> # -------------------------- DECLARE variables for balance, deposit, and withdrawal --------------------------
>> 
>> account_balance = float(500.25)                                                 # Starting balance indicated by Codio
>> deposit_amount = 0                                                              # Declare variable 'deposit_amount'
>> withdrawal_amount = 0                                                           # Declare variable 'withdrawal_amount'
>> 
>> 
>> # -------------------------- DEFINE FUNCTIONS - balance, withdrawal, and deposit -----------------------------
>> 
>> def balance(account_balance):                                                   # Define balance function
>>  print("Your current balance: ", account_balance)                              # Prints the current avalible balance
>> 
>> def deposit(account_balance, deposit_amount):                                   # Define DEPOSIT function with parameters account_balance and deposit_amount
>>  deposit_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 + deposit_amount                                    # This addition assigns the updated value of the account blance, to the variable 'BALANCE'
>>  print("Deposit was $%.2f , your new current balance is $%.2f" % (deposit_amount, balance))  # Prints depost amount and account balance
>> 
>> def withdrawal(account_balance, withdrawal_amount):                             # Define WITHDRAWAL function with parameters account_balance and withdrawal_amount
>>  withdrawal_amount = float(input("How much would you like to withdraw today?\n")) #  Accept user input for the withdrawal amount, in float format
>>  if withdrawal_amount > account_balance:                                       # Checking to see if the amount requested, is greater than the amount avalible
>>    print("Insuffient funds, $%.2f is greater than your account balance of $%.2f" % (withdrawal_amount, account_balance)) # If the amount requested is greater than the account balance, there are insuffient funds
>>  else:                                                                         # Suffient amount of funds are avalible, the function continues
>>    balance = account_balance - withdrawal_amount                               # Variable 'balance' is assigned to reflect the new avalible balance
>>    print ("Withdrawal amount was $%.2f, your new current balance is $%.2f" % (withdrawal_amount, balance))  # Prints withdrawal amount and account balance
> 
> 
> These are mostly fine for individual use-cases, but all they do is print
> the temporary updated value.  For deposit and withdrawal, do you have a
> requirement to remember the new values?  How would you update the global
> variables with the new values from the functions to make them persistent? 
> 
> 
>> # ------------------------------------ ACCEPT USER INPUT - D, B, W, or Q -------------------------------------
>> userChoice = 'go'                                                               # Setting the variable 'userChoice' to 'go', so we can impliment a while loop
>> 
>> while userChoice != 'E':                                                        # As long as the user does not select 'E' (Exit), the program will keep looping with user choices
>> 
>> # Step ONE =>  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 TWO => conditional statement begins based on the value of variable 'userchoice' from user input
>> # Four branches ustilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT
>>    if (userChoice == 'D'):                                                     # Accepts input D and proceeds with function 'deposit'
>>        deposit (account_balance, deposit_amount)                               # DEPOSIT function is called with parameters 'account_balance' and 'deposit_amount'
>> 
>>    elif (userChoice == 'B'):                                                   # Accepts input B and proceeds with function 'balance'
>>        balance (account_balance)                                               # BALANCE function is called with parameter 'account_balance'
>> 
>>    elif (userChoice == 'W'):                                                   # Accepts input D and proceeds with function 'withdrawal'
>>        withdrawal (account_balance, withdrawal_amount)                         # WITHDRAWAL function is called with parameters 'account_balance' and 'withdrawal_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
> 
> 
> Overall, This is structured just fine.  The main thing I would look at
> is what I mentioned above regarding value permanence.  To see what I
> mean, run the program, but make several transactions in a row.  If you
> subtract $100 and then check your balance, does it change?  If not, how
> would you fix it?
> 
> 
> -- 
> David Rock
> david at graniteweb.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list