0% found this document useful (0 votes)
788 views7 pages

This Study Resource Was

This document contains functions for a banking application including: 1) Validating passwords by checking length and character requirements. 2) Allowing users to sign up by adding their username and password to dictionaries if valid. 3) Allowing users to log in by checking their password against stored credentials. 4) Updating user balances by depositing or transferring amounts between users. 5) Allowing password changes if the old password is correct and new meets requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
788 views7 pages

This Study Resource Was

This document contains functions for a banking application including: 1) Validating passwords by checking length and character requirements. 2) Allowing users to sign up by adding their username and password to dictionaries if valid. 3) Allowing users to log in by checking their password against stored credentials. 4) Updating user balances by depositing or transferring amounts between users. 5) Allowing password changes if the old password is correct and new meets requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

# use for validate password format

def validate(username,password):
l, u, d = 0, 0, 0
if (len(password) >= 8):
for i in password:

# counting lowercase alphabets


if (i.islower()):
l+=1

# counting uppercase alphabets


if (i.isupper()):
u+=1

# counting digits
if (i.isdigit()):
d+=1

if (l>=1 and u>=1 and d>=1 and l+u+d==len(password)):


if username != password:
result = True
else:

m
result = "The username & password cannot be the same"

er as
else:

co
result = "Fail!!\n"+"- The password must be at least 8 characters. \n"+

eH w
"- The password must contain at least one lowercase character.\n "+"- The
password must contain at least one uppercase character.\n "+"- The password must

o.
contain at least one number.\n " rs e
return result
ou urc
def signup(user_accounts, bank, log_in, username, password):
'''
o

This function allows users to sign up.


aC s

If both username and password meet the requirements, updates the username
v i y re

and the corresponding password in the user_accounts,


and returns success message.

If the username and password fail to meet any one of the following
requirements, returns error message.
ed d

- The username already exists in the user_accounts.


ar stu

- The password must be at least 8 characters.


- The password must contain at least one lowercase character.
- The password must contain at least one uppercase character.
- The password must contain at least one number.
- The username & password cannot be the same.
sh is

'''
Th

if username not in user_accounts:


result = validate(username, password)
print(result)
if result == True:
# add to dict
user_accounts[username] = password
## not logged in = False, logged in = True
log_in[username] = False
bank[username] = 0
return "sign up success"
else:
return result

else:
return result

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
def login(user_accounts, log_in, username, password):
'''
This function allows users to log in with their username and password.
The users_accounts stores the usernames and passwords.

If the username does not exist or the password is incorrect, return error
message.
Otherwise, return success message.

'''

if username in user_accounts:
#check password
if user_accounts[username] == password:
result = "log in success"
# update status
log_in[username] = True
else:
result = "Invalid password"
else:
result = "Not found account with username {}".format(username)
return result

m
er as
def update(bank, log_in, username, amount):

co
'''

eH w
In this function, you will try to update the given user's bank account with
the given amount.

o.
The amount is an integer, and can either be positive or negative.
rs e
ou urc
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.
o
aC s

If the user doesn't exist in the bank, create the user.


v i y re

- The given amount can not result in a negative balance in the bank account.
'''

# check logged in
if log_in[username]:
ed d

if amount < 0:
ar stu

return "Not enough amount"


else:
if username in bank:
bank[username] += float(amount)
return "deposit success"
sh is

else:
Th

# bank[username] = float(amount)
return "Not found account"
else:
return "Not log in"

def transfer(bank, log_in, userA, userB, amount):


'''
In this function, you will try to make a transfer between two user accounts.
The bank is a dictionary, where the key is the username and the value is the
amount to transfer.
Amount is always positive.

- Deduct the amount from userA and add it to userB, which makes a transfer.
- Consider some following cases:
- userA must be in accounts and his/her log-in status must be True.
- userB must be in log_in, regardless of log-in status.

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
- No user can have a negative amount. He/she must have a positive or zero
amount.

Return success message if a transfer is made. If a user is invalid or the


amount is invalid, return error message.
userA must be in bank and userB can be absent from bank. No user can have a
negative balance.
Each must have a positive or zero balance

Note that the arguments correspond as follows:


o bank: The bank accounts dictionary
o log_in: The user accounts dictionary
o userA: The account from which the funds will be transferred
o amount: The amount to transfer
'''

try:
if log_in[userA]:
#ckeck amount
if amount <= bank[userA]:
bank[userA]-= amount

m
if userB in bank:

er as
bank[userB]+= amount

co
return "transfer success"

eH w
else:
return "Not fount recipient"

o.
else: rs e
return "Not enough amount"
ou urc
else:
return "user not log in"
except:
o

return "Not found account with username {}".format(userA)


aC s
v i y re

def change_password(user_accounts, log_in, username, old_password,


new_password):
'''
This function allows users to change their password.
ed d
ar stu

If all of the following requirements are met, change the password and return
True. Otherwise, return error message.
- The username exists in the user_accounts.
- The old_password is the user's current password.
- The new_password should be different from the old one.
sh is

- The new_password fulfills the requirement in signup.


Th

This will also come in handy when writing the signup() function.
'''

if username in user_accounts and log_in[username]:


if old_password == user_accounts[username]:
if user_accounts[username] != new_password:
result = validate(username, new_password)
if result == True:
# update password
user_accounts[username] = new_password
return "Change password success"
return result
return result
else:
return "invalid old password"
else:

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
return "Not found account with username {} or user not log
in".format(username)
def delete_account(user_accounts, log_in, bank, username, password):
'''
Deletes the user from the user_accounts.
If the following requirements are met, delete the user from user_accounts
and return success message. Otherwise, return error message.
- The user exists in user_accounts and the password is correct.
'''

try:
if user_accounts[username] == password:
if log_in[username]:
# delete account
del user_accounts[username]
del log_in[username]
del bank[username]
return "delete account success"
else:
return "user not log in"
else:
return "Invalid username or password"

m
except:

er as
return "Not found account with username : {}".format(username)

co
eH w
def main():
pointer = True

o.
while pointer: rs e
# for debugging
ou urc
print("############################ ***Available bank account**
############################")
print('Available bank account:')
for key in bank:
o

print("username: ",key)
aC s
v i y re

print("################################################################")
print('')
#

option = input("What do you want to do? Please enter a numerical option


ed d

below.\n"
ar stu

"1. login\n"
"2. signup\n"
"3. change password\n"
"4. delete account\n"
"5. deposit\n"
sh is

"6. make a transfer\n"


Th

"7. exit\n")
if option == "1":
username = input("Please input the username\n")
password = input("Please input the password\n")

# add code to login


response = login(user_accounts, log_in, username, password)
print("############################ ***Result**
############################")
print(response)

print("################################################################")
continue_flag = True
while continue_flag:
pt = input("Continuous? yes or no\n")
if pt == "yes":
pointer = True

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
continue_flag = False
elif pt == "no":
pointer = False
continue_flag = False
else:
print("The option is not valid. Please re-enter the
option.\n")
elif option == "2":
username = input("Please input the username\n")
password = input("Please input the password\n")

# add code to signup


response = signup(user_accounts, bank, log_in, username, password)
print("############################ ***Result**
############################")
print(response)

print("################################################################")
continue_flag = True
while continue_flag:
pt = input("Continuous? yes or no\n")
if pt == "yes":

m
pointer = True

er as
continue_flag = False

co
elif pt == "no":

eH w
pointer = False
continue_flag = False

o.
else: rs e
print("The option is not valid. Please re-enter the
ou urc
option.\n")
elif option == "3":
username = input("Please input the username\n")
old_password = input("Please input the old password\n")
o

new_password = input("Please input the new password\n")


aC s
v i y re

# add code to change password


response = change_password(user_accounts, log_in, username,
old_password, new_password)
print("############################ ***Result**
############################")
ed d

print(response)
ar stu

print("################################################################")
continue_flag = True
while continue_flag:
pt = input("Continuous? yes or no\n")
sh is

if pt == "yes":
Th

pointer = True
continue_flag = False
elif pt == "no":
pointer = False
continue_flag = False
else:
print("The option is not valid. Please re-enter the
option.\n")
pointer = False
elif option == "4":
username = input("Please input the username\n")
password = input("Please input the password\n")

# add code to delete account


response = delete_account(user_accounts, log_in, bank, username,
password)
print("############################ ***Result**

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
############################")
print(response)

print("################################################################")
continue_flag = True
while continue_flag:
pt = input("Continuous? yes or no\n")
if pt == "yes":
pointer = True
continue_flag = False
elif pt == "no":
pointer = False
continue_flag = False
else:
print("The option is not valid. Please re-enter the
option.\n")
elif option == "5":
username = input("Please input the username\n")
amount = input("Please input the amount\n")
try:
amount = float(amount)
# add code to update amount

m
response = update(bank, log_in, username, amount)

er as
print("############################ ***Result**

co
############################")

eH w
print(response)

o.
print("################################################################")
rs e
continue_flag = True
ou urc
while continue_flag:
pt = input("Continuous? yes or no\n")
if pt == "yes":
pointer = True
o

continue_flag = False
aC s

elif pt == "no":
v i y re

pointer = False
else:
print("The option is not valid. Please re-enter the
option.\n")
except:
ed d

print("The amount is invalid. Please reenter the option\n")


ar stu

elif option == "6":


userA = input("Please input the user who will be deducted\n")
userB = input("Please input the user who will be added\n")
amount = input("Please input the amount\n")
sh is

try:
Th

amount = float(amount)
# add code to transfer amount
response = transfer(bank, log_in, userA, userB, amount)
print("############################ ***Result**
############################")
print(response)

print("################################################################")
continue_flag = True
while continue_flag:
pt = input("Continuous? yes or no\n")
if pt == "yes":
pointer = True
continue_flag = False
elif pt == "no":
pointer = False
continue_flag = False

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
else:
print("The option is not valid. Please re-enter the
option.\n")
except:
print("The amount is invalid. Please re-enter the option.\n")
elif option == "7":
break
else:
print("The option is not valid. Please re-enter the option.\n")

#This will automatically run the main function in your program


if __name__ == '__main__':
# innitail data
bank = {"account001":100.0,"account002":152.3} #{"username":amount}
user_accounts = {"account001":"password001","account002":"password002"} #
{"username":"password"}
log_in = {"account001":False,"account002":False} #{"username":True or False}
main()

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000823114299 from CourseHero.com on 08-11-2021 08:00:54 GMT -05:00

https://www.coursehero.com/file/84274580/bank-managementpy/
Powered by TCPDF (www.tcpdf.org)

You might also like