This is information needed to answer the assignment. Instructions are in red, and most of the coding has been done. The missing code should be written under "your code here". I have answered the first...

1 answer below »
This is information needed to answer the assignment. Instructions are in red, and most of the coding has been done. The missing code should be written under "your code here". I have answered the first part. Thank you
Bank Text:
Brandon: 5Patrick: 18.9Brandon: xyzJack:
Sarah: 825Jack : 45Brandon: 10James: 3.25James: 125.62Sarah: 2.43
Brandon: 100.5
User Text:
Brandon - brandon123ABCJackJack - jac123Jack - jack123POUPatrick - patrick5678Brandon - brandon123ABCDJames - 100jamesABDSarah - sd896ssfJJHJennie - sadsaca


module4-Copy2 March 30, 2021 0.1 Module 4 In this assignment, you will implement an online banking system. Users can sign-up with the system, log in to the system, change their password, and delete their account. They can also update their bank account balance and transfer money to another user’s bank account. You’ll implement functions related to File I/O and dictionaries. Two of the functions require you to import files and create dictionaries. User information will be imported from the “users.txt” file and account information will be imported from the “bank.txt” file. Take a look at the content in the different files. The remaining functions require you to use or modify the two dictionaries created from the files. Each function has been defined for you, but without the code. See the docstring in each function for instructions on what the function is supposed to do and how to write the code. It should be clear enough. In some cases, we have provided hints to help you get started. [1]: ########################################################### ### EXECUTE THIS CELL BEFORE YOU TO TEST YOUR SOLUTIONS ### ########################################################### import nose.tools as tools [4]: def import_and_create_bank(filename): ''' This function is used to create a bank dictionary. The given argument is␣ ↪→the filename to load. Every line in the file should be in the following format: key: value The key is a user's name and the value is an amount to update the user's␣ ↪→bank account with. The value should be a number, however, it is possible that there is no value or that the value is␣ ↪→an invalid number. What you will do: - Create an empty bank dictionary. - Read in the file. - Add keys and values to the dictionary from the contents of the file. - If the key doesn't exist in the dictionary, create a new key:value pair. 1 - If the key does exist in the dictionary, increment its value with the␣ ↪→amount. - You should also handle the following cases: -- When the value is missing or invalid. If so, ignore that line and don't␣ ↪→update the dictionary. -- When the line is completely blank. Again, ignore that line and don't␣ ↪→update the dictionary. -- When there is whitespace at the beginning or end of a line and/or␣ ↪→between the name and value on a line. You should trim any and all whitespace. - Return the bank dictionary from this function. For example, here's how your code should handle some specific lines in the␣ ↪→file: The 1st line in the file has a name and valid number: Brandon: 5 Your code will process this line and add the extracted information to the␣ ↪→dictionary. After it does, the dictionary will look like this: bank = {"Brandon": 5} The 2nd line in the file also has a name and valid number: Patrick: 18.9 Your code will also process this line and add the extracted information to␣ ↪→the dictionary. After it does, the dictionary will look like this: bank = {"Brandon": 5, "Patrick": 18.9} The 3rd line in the file has a name but invalid number: Brandon: xyz Your code will ignore this line and add nothing to the dictionary. It will␣ ↪→still look like this: bank = {"Brandon": 5, "Patrick": 18.9} The 4th line in the file has a name but missing number: Jack: Your code will ignore this line and add nothing to the dictionary. It will␣ ↪→still look like this: bank = {"Brandon": 5, "Patrick": 18.9} The 5th line in the file is completely blank. Your code will ignore this line and add nothing to the dictionary. It will␣ ↪→still look like this: bank = {"Brandon": 5, "Patrick": 18.9} 2 The 8th line in the file has a name and valid number, but with extra␣ ↪→whitespace: Brandon: 10 Your code will process this line and update the value associated with the␣ ↪→existing key ('Brandon') in the dictionary. After it does, the value associated with the key 'Brandon' will be 10: bank = {"Brandon": 15, ...} After processing every line in the file, the dictionary will look like this: bank = {"Brandon": 115.5, "Patrick": 18.9, "Sarah": 827.43, "Jack": 45. ↪→0, "James": 128.87} Return the dictionary from this function. ''' bank = {} # your code here #open file in read mode f=open(filename,'r') #get lines in files as list lines=f.readlines() #iterate over each line in list of lines for line in lines: #strip white spaces from begining and end of line #split line into list based on ":"separator lst=line.strip().split(':') #skip line if it does nat have a name or deposit amount if len (lst) <=1: continue="" #get="" key="" (name)="" or="" value="" (deposit="" amount)="" from="" line="" #strip="" whitespace="" from="" beginning="" of="" key="" (name)="" and="" value="" (deposit␣="" ↪→amount)="" key="lst[0].strip()" value="lst[1].strip()" try:="" #try="" to="" cast="" value="" (deposit="" amount)="" to="" numeric="" value="" value="float(value)" #add="" new="" deposit="" amount="" to="" total="" current="" balance="" #associated="" with="" key="" (name),="" or="" 0="" bank[key]="bank.get(key,0)" +value="" 3="" except:="" #otherwise,="" skip="" this="" line="" continue="" f.close()="" return="" bank="" [16]:="" ##########################="" ###="" test="" your="" solution="" ###="" ##########################="" bank="import_and_create_bank("bank.txt")" tools.assert_false(len(bank)="=" 0)="" tools.assert_almost_equal(115.5,="" bank.get("brandon"))="" tools.assert_almost_equal(128.87,="" bank.get("james"))="" tools.assert_is_none(bank.get("joel"))="" tools.assert_is_none(bank.get("luke"))="" tools.assert_almost_equal(bank.get("sarah"),="" 827.43)="" print("success!")="" success!="" [1]:="" def="" signup(user_accounts,="" log_in,="" username,="" password):="" '''="" this="" function="" allows="" users="" to="" sign="" up.="" if="" both="" username="" and="" password="" meet="" the="" requirements,="" updates="" the="" username␣="" ↪→and="" the="" corresponding="" password="" in="" the="" user_accounts="" dictionary,="" and="" returns="" true.="" if="" the="" username="" and="" password="" fail="" to="" meet="" any="" one="" of="" the="" following␣="" ↪→requirements,="" returns="" false.="" -="" the="" username="" already="" exists="" in="" the="" user_accounts.="" -="" 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.="" for="" example:="" -="" calling="" signup(user_accounts,="" log_in,="" "brandon",="" "123abcabcd")="" will␣="" ↪→return="" false="" -="" calling="" signup(user_accounts,="" log_in,="" "brandonk",="" "123abcd")="" will="" return␣="" ↪→false="" -="" calling="" signup(user_accounts,="" log_in,="" "brandonk","abcdabcd")="" will="" return␣="" ↪→false="" 4="" -="" calling="" signup(user_accounts,="" log_in,="" "brandonk",="" "123aabcd")="" will="" return␣="" ↪→true.="" then="" calling="" signup(user_accounts,="" log_in,="" "brandonk",="" "123aabcd")="" again="" will="" return␣="" ↪→false.="" hint:="" think="" about="" defining="" and="" using="" a="" separate="" valid(password)="" function␣="" ↪→that="" checks="" the="" validity="" of="" a="" given="" password.="" this="" will="" also="" come="" in="" handy="" when="" writing="" the="" change_password()="" function.="" '''="" #="" your="" code="" here="" [2]:="" def="" import_and_create_accounts(filename):="" '''="" this="" function="" is="" used="" to="" create="" an="" user="" accounts="" dictionary="" and="" another␣="" ↪→login="" dictionary.="" the="" given="" argument="" is="" the="" filename="" to="" load.="" every="" line="" in="" the="" file="" should="" be="" in="" the="" following="" format:="" username="" -="" password="" the="" key="" is="" a="" username="" and="" the="" value="" is="" a="" password.="" if="" the="" username="" and␣="" ↪→password="" fulfills="" the="" requirements,="" add="" the="" username="" and="" password="" into="" the="" user="" accounts="" dictionary.="" to="" make␣="" ↪→sure="" that="" the="" password="" fulfills="" these="" requirements,="" be="" sure="" to="" use="" the="" signup="" function="" that="" you="" wrote="" above.="" for="" the="" login="" dictionary,="" the="" key="" is="" the="" username,="" and="" its="" value="" indicates␣="" ↪→whether="" the="" user="" is="" logged="" in,="" or="" not.="" initially,="" all="" users="" are="" not="" logged="" in.="" what="" you="" will="" do:="" -="" create="" an="" empty="" user="" accounts="" dictionary="" and="" an="" empty="" login="" dictionary.="" -="" read="" in="" the="" file.="" -="" if="" the="" username="" and="" password="" fulfills="" the="" requirements,="" adds="" the="" username␣="" ↪→and="" password="" into="" the="" user="" accounts="" dictionary,="" and="" updates="" the="" login="" dictionary.="" -="" you="" should="" also="" handle="" the="" following="" cases:="" --="" when="" the="" password="" is="" missing.="" if="" so,="" ignore="" that="" line="" and="" don't="" update␣="" ↪→the="" dictionaries.="" --="" when="" there="" is="" whitespace="" at="" the="" beginning="" or="" end="" of="" a="" line="" and/or␣="" ↪→between="" the="" name="" and="" password="" on="" a="" line.="" you="" should="" trim="" any="" and="" all="" whitespace.="" -="" return="" both="" the="" user="" accounts="" dictionary="" and="" login="" dictionary="" from="" this␣="" ↪→function.="" for="" example,="" here's="" how="" your="" code="" should="" handle="" some="" specific="" lines="" in="" the␣="" ↪→file:="" the="" 1st="" line="" in="" the="" file="" has="" a="" name="" and="" password:="" 5="" brandon="" -="" brandon123abc="" your="" code="" will="" process="" this="" line,="" and="" using="" the="" signup="" function,="" will="" add␣="" ↪→the="" extracted="" information="" to="" the="" dictionaries.="" after="" it="" does,="" the="" dictionaries="" will="" look="" like="" this:="" user_accounts="{"Brandon":" "brandon123abc"}="" log_in="{"Brandon":" false}="" the="" 2nd="" line="" in="" the="" file="" has="" a="" name="" but="" missing="" password:="" jack="" your="" code="" will="" ignore="" this="" line.="" the="" dictionaries="" will="" still="" look="" like␣="" ↪→this:="" user_accounts="{"Brandon":" "brandon123abc"}="" log_in="{"Brandon":" false}="" the="" 3rd="" line="" in="" the="" file="" has="" a="" name="" and="" password:="" jack="" -="" jac123="" your="" code="" will="" process="" this="" line,="" and="" using="" the="" signup="" function,="" will="" not␣="" ↪→add="" the="" extracted="" information="" to="" the="" dictionaries="" because="" the="" password="" is="" invalid.="" the="" dictionaries="" will="" still␣="" ↪→look="" like="" this:="" user_accounts="{"Brandon":" "brandon123abc"}="" log_in="{"Brandon":" false}="" the="" 4th="" line="" in="" the="" file="" has="" a="" name="" and="" password:="" jack="" -="" jack123pou="" your="" code="" will="" process="" this="" line,="" and="" using="" the="" signup="" function,="" will="" add␣="" ↪→the="" extracted="" information="" to="" the="" dictionaries.="" after="" it="" does,="" the="" dictionaries="" will="" look="" like="" this:="" user_accounts="{"Brandon":" "brandon123abc,="" "jack":="" "jack123pou"}="" log_in="{"Brandon":" false,="" "jack":="" false}="" after="" processing="" every="" line="" in="" the="" file,="" the="" dictionaries="" will="" look="" like␣="" ↪→this:="" user_accounts="{"Brandon":" "brandon123abc,="" "jack":="" "jack123pou",="" "james":="" ↪→="" "100jamesabd",="" "sarah":="" "sd896ssfjjh"}="" log_in="{"Brandon":" false,="" "jack":="" false,="" "james":="" false,="" "sarah":="" false}="" return="" the="" dictionaries="" from="" this="" function.="" '''="" user_accounts="{}" log_in="{}" #="" your="" code="" here="" with="" open="" (filename)="" as="" f:="" for="" line="" in="" f:="" lst="line.strip().split('-')" 6="" if=""><=1: continue="" username="lst[0].strip()" password="lst[1].strip()" if="" signup(user_accounts,log_in,username,password):="" log_in[username]="False" return="" user_accounts,="" log_in="" [="" ]:="" ##########################="" ###="" test="" your="" solution="" ###="" ##########################="" user_accounts,="" log_in="import_and_create_accounts("user.txt")" tools.assert_false(len(user_accounts)="=" 0)="" tools.assert_false(len(log_in)="=" 0)="" tools.assert_equal(user_accounts.get("brandon"),"brandon123abc")="" tools.assert_equal(user_accounts.get("jack"),"jack123pou")="" tools.assert_is_none(user_accounts.get("jennie"))="" tools.assert_false(log_in["sarah"])="" print("success!")="" [="" ]:="" ##########################="" ###="" test="" your="" solution="" ###="" ##########################="" bank="import_and_create_bank("bank.txt")" user_accounts,="" log_in="import_and_create_accounts("user.txt")" tools.assert_false(signup(user_accounts,log_in,"brandon","123abcabcd"))="" tools.assert_false(signup(user_accounts,log_in,"brandonk","123abcd"))="" tools.assert_false(signup(user_accounts,log_in,"brandonk","1234abcd"))="" tools.assert_false(signup(user_accounts,log_in,"brandonk","abcdabcd"))="" tools.assert_false(signup(user_accounts,log_in,"brandonk","1234abcd"))="" tools.assert_false(signup(user_accounts,log_in,"123abcabcd","123abcabcd"))="" tools.assert_true(signup(user_accounts,log_in,"brandonk","123aabcd"))="" tools.assert_false(signup(user_accounts,log_in,"brandonk","123aabcd"))="" tools.assert_true("brandonk"="" in="" user_accounts)="" tools.assert_equal("123aabcd",user_accounts["brandonk"])="" tools.assert_false(log_in["brandonk"])="" print("success!")="" [4]:="" def="" login(user_accounts,="" log_in,="" username,="" password):="" '''="" this="" function="" allows="" users="" to="" log="" in="" with="" their="" username="" and="" password.="" users_accounts="" stores="" the="" usernames="" and="" passwords.="" 7="" if="" the="" username="" does="" not="" exist="" or="" the="" password="" is="" incorrect,="" return="" false.="" otherwise,="" return="" true.="" for="" example:="" -="" calling="" login(user_accounts,="" "brandon",="" "123abcab")="" will="" return="" false="" -="" calling="" login(user_accounts,="" "brandon",="" "brandon123abc")="" will="" return="" true="" '''="" #="" your="" code="" here="" [5]:="" ##########################="" ###="" test="" your="" solution="" ###="" ##########################="" bank="import_and_create_bank("bank.txt")" user_accounts,="" log_in="import_and_create_accounts("user.txt")" tools.assert_false(login(user_accounts,="" log_in,"brandon","123abcab"))="" tools.assert_true(login(user_accounts,="" log_in,"brandon","brandon123abc"))="" tools.assert_false(login(user_accounts,="" log_in,"brandonk","123abcabc"))="" print("success!")="" ␣="" ↪→---------------------------------------------------------------------------="" nameerror="" traceback="" (most="" recent="" call␣="" ↪→last)=""> in 2 ### TEST YOUR SOLUTION ### 3 ########################## ----> 4 bank = import_and_create_bank("bank.txt") 5 user_accounts, log_in = import_and_create_accounts("user.txt")
Answered Same DayMar 30, 2021

Answer To: This is information needed to answer the assignment. Instructions are in red, and most of the coding...

Vibhav answered on Mar 31 2021
150 Votes
BankingSystem/BankingSystem.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import nose.tools as tools\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def import_and_create_bank(filename):\n",
" bank = {}\n",
" # your code here\n",
" \n",
" #open file in read mode\n",
" f=open(filename,'r')\n",
" \n",
" #get lines in files as list\n",
" lines=f.readlines()\n",
" \n",
" #iterate over each line in list of lines\n",
"
for line in lines:\n",
" #strip white spaces from begining and end of line\n",
" #split line into list based on \":\"separator\n",
" lst=line.strip().split(':')\n",
" #skip line if it does nat have a name or deposit amount\n",
" if len (lst) <=1:\n",
" continue\n",
" #get key (name) or value (deposit amount) from line\n",
" #strip whitespace from beginning of key (name) and value (deposit␣,!amount)\n",
" key=lst[0].strip()\n",
" value=lst[1].strip()\n",
" try:\n",
" #try to cast value (deposit amount) to numeric value\n",
" value=float(value)\n",
" #add new deposit amount to total current balance\n",
" #associated with key (name), or 0\n",
" bank[key]=bank.get(key,0) +value\n",
" except:\n",
" #otherwise, skip this line\n",
" continue\n",
" f.close()\n",
" return bank"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success!\n"
]
}
],
"source": [
"bank = import_and_create_bank(\"bank.txt\")\n",
"tools.assert_false(len(bank) == 0)\n",
"tools.assert_almost_equal(115.5, bank.get(\"Brandon\"))\n",
"tools.assert_almost_equal(128.87, bank.get(\"James\"))\n",
"tools.assert_is_none(bank.get(\"Joel\"))\n",
"tools.assert_is_none(bank.get(\"Luke\"))\n",
"tools.assert_almost_equal(bank.get(\"Sarah\"), 827.43)\n",
"print(\"Success!\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Brandon': 115.5, 'Patrick': 18.9, 'Sarah': 827.43, 'Jack': 45.0, 'James': 128.87}\n"
]
}
],
"source": [
"print(bank)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def valid(password):\n",
" isValid = True\n",
" if(len(password)<8):\n",
" isValid = False\n",
" elif not re.search(\"[a-z]\", password):\n",
" isValid = False\n",
" elif not re.search(\"[A-Z]\", password):\n",
" isValid = False\n",
" elif not re.search(\"[0-9]\", password):\n",
" isValid = False\n",
" else:\n",
" isValid = True\n",
" return isValid\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def signup(user_accounts, log_in, username, password):\n",
" if username in user_accounts.keys():\n",
" return False\n",
" elif username==password:\n",
" return False\n",
" if valid(password):\n",
" user_accounts[username] = password\n",
" log_in[username] = False\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have changed one line of this function, for consistency. log_in[user_name]=False. Because, in the later part of the code assert_false has been used to test log_in dictionary"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def import_and_create_accounts(filename):\n",
" user_accounts = {}\n",
" log_in = {}\n",
" \n",
" # your code here\n",
" with open (filename) as f:\n",
" for line in f:\n",
" lst = line.strip().split('-')\n",
" if len(lst)<=1:\n",
" continue\n",
" username = lst[0].strip()\n",
" password = lst[1].strip()\n",
" if signup(user_accounts,log_in,username,password):\n",
" log_in[username]=False\n",
" return user_accounts, log_in\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success!\n"
]
}
],
"source": [
"user_accounts, log_in = import_and_create_accounts(\"user.txt\")\n",
"tools.assert_false(len(user_accounts) == 0)\n",
"tools.assert_false(len(log_in) == 0)\n",
"tools.assert_equal(user_accounts.get(\"Brandon\"),\"brandon123ABC\")\n",
"tools.assert_equal(user_accounts.get(\"Jack\"),\"jack123POU\")\n",
"tools.assert_is_none(user_accounts.get(\"Jennie\"))\n",
"tools.assert_false(log_in[\"Sarah\"])\n",
"print(\"Success!\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Brandon': 'brandon123ABC', 'Jack': 'jack123POU', 'James': '100jamesABD', 'Sarah': 'sd896ssfJJH'}\n"
]
}
],
"source": [
"print(user_accounts)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Brandon': False, 'Jack': False, 'James': False, 'Sarah': False}\n"
]
}
],
"source": [
"print(log_in)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success!\n"
]
}
],
"source": [
"bank = import_and_create_bank(\"bank.txt\")\n",
"user_accounts, log_in = import_and_create_accounts(\"user.txt\")\n",
"tools.assert_false(signup(user_accounts,log_in,\"Brandon\",\"123abcABCD\"))\n",
"tools.assert_false(signup(user_accounts,log_in,\"BrandonK\",\"123ABCD\"))\n",
"tools.assert_false(signup(user_accounts,log_in,\"BrandonK\",\"1234ABCD\"))\n",
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here