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
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",
...