Question: Can you please help me to define this function in Python to update a user's bank account? I am stuck here and I don't really
Can you please help me to define this function in Python to update a user's bank account? I am stuck here and I don't really know how to solve this task in my HW.
Thank you guys so much!
User information is imported from a "user.txt" file and account information is imported from a "bank.txt" file.
def update(bank, log_in, username, amount): ''' In this function, you will try to update the given user's bank account with the given amount. The amount is an integer, and can either be positive or negative.
To update the user's account with the amount, the following requirements must be met: - The user exists in log_in and his/her status is True, meaning, the user is logged in.
If the user doesn't exist in the bank, create the user. - The given amount can not result in a negative balance in the bank account.
return True if the user's account was updated.
For example: - Calling update(bank, log_in, "Brandon", 100) will return False, unless "Brandon" is first logged in. Then it will return True. - Calling update(bank, log_in, "Brandon", -200) will return False '''
# your code here # defining empty bank dict bank = {} log_in = {} username = input(" Enter your username: ") password = input(" Enter your password: ") for line in open("bank.txt", "r").readlines(): #Read the lines bank = line.split() if username == bank[0] and amount == bank[1]: bank.update([username, amount]) print('Correct credentials!') return True return (bank, log_in, username, amount) while True: try: amount = float(input(" Enter the amount you want to change: ")) except: print("The given amount can not result in a negative balance in the bank account")
#using login function to check if the user is logged in login(user_accounts, log_in, username, password) #using signup function to add username and password in user_accounts signup(user_accounts, log_in, username, password) print('Incorrect credentials') return False
""""The above code raises an error !!!!!"""""
def main(): ''' The main function is a skeleton for you to test if your overall programming is working. Note we will not test your main function. It is only for you to run and interact with your program. '''
bank = import_and_create_dictionary("bank.txt") user_accounts, log_in = import_and_create_accounts("user.txt")
while True: # for debugging print('bank:', bank) print('user_accounts:', user_accounts) print('log_in:', log_in) print('') #
option = input("What do you want to do? Please enter a numerical option below. " "1. login " "2. signup " "3. change password " "4. delete account " "5. update amount " "6. make a transfer " "7. exit ") if option == "1": username = input("Please input the username ") password = input("Please input the password ")
# add code to login login(user_accounts, log_in, username, password); elif option == "2": username = input("Please input the username ") password = input("Please input the password ")
# add code to signup signup(user_accounts, log_in, username, password) elif option == "3": username = input("Please input the username ") old_password = input("Please input the old password ") new_password = input("Please input the new password ")
# add code to change password change_password(user_accounts, log_in, username, old_password, new_password) elif option == "4": username = input("Please input the username ") password = input("Please input the password ")
# add code to delete account delete_account(user_accounts, log_in, bank, username, password) elif option == "5": username = input("Please input the username ") amount = input("Please input the amount ") try: amount = float(amount)
# add code to update amount update(bank, log_in, username, amount) except: print("The amount is invalid. Please reenter the option ")
elif option == "6": userA = input("Please input the user who will be deducted ") userB = input("Please input the user who will be added ") amount = input("Please input the amount ") try: amount = float(amount)
# add code to transfer amount transfer(bank, log_in, userA, userB, amount) except: print("The amount is invalid. Please re-enter the option. ") elif option == "7": break; else: print("The option is not valid. Please re-enter the option. ")
#This will automatically run the main function in your program #Don't change this if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
