Reputation: 11
I am confused on this homework assignment. I don't understand why this error is coming or how I can fix it.
The homework assignment goes like this:
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.
The specific part I am stuck on:
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.
'''
user_accounts = {}
log_in = {}
# your code here
user = open(filename, 'r')
lines = user.readlines()
for line in lines:
lst = lines.strip().split("-")
if '-' not in l:
continue
username = lst[0].strip()
password = lst[1].strip()
if username == "":
continue
elif password == "":
continue
else:
signup(user_accounts, log_in, username, password)
return user_accounts,log_in
For this code I getting the following error:
-
AttributeError Traceback (most recent call last)
\<ipython-input-46-72d8df424bea\> in \<module\>
2 ### TEST YOUR SOLUTION ###
3 ##########################
\----\> 4 user_accounts, log_in = import_and_create_accounts("user.txt")
5
6 tools.assert_false(len(user_accounts) == 0)
\<ipython-input-45-9e2980d0d173\> in import_and_create_accounts(filename)
64 lines = user.readlines()
65 for line in lines:
\---\> 66 lst = lines.strip().split("-")
67
68 if '-' not in l:
AttributeError: 'list' object has no attribute 'strip'
I tried the code above, I was expecting the code to work, but it is giving an attribute error.
Upvotes: 1
Views: 1328
Reputation: 21
You should replace lst = lines.strip().split("-")
with lst = line.strip().split("-")
. list
object doesn't have strip
method.
Upvotes: 1