PYTHON. допоможіть, будь ласка. буду вдячна!
bank.txt
Brandon: 5
Patrick: 18.9
Brandon: xyz
Jack:
Sarah: 825
Jack : 45
Brandon: 10
James: 3.25
James: 125.62
Sarah: 2.43
Brandon: 100.5
user.txt
Brandon - brandon123ABC
Jack
Jack - jac123
Jack - jack123POU
Patrick - patrick5678
Brandon - brandon123ABCD
James - 100jamesABD
Sarah - sd896ssfJJH
Jennie - sadsaca
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:
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.
'''
#your code here
Ответы
BankingSystem/BankingSystem.ipyn
{
"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 cu
ent 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\"),\"
andon123ABC\")\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': '
andon123ABC', '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",