我正在建立一个跳蚤市场计划。我有一个外部文件托管所有员工的用户名和密码。我试图测试登录部分,要求用户名,然后密码。它测试UN是否在readline()中包含的字典中。你知道吗
这是带有用户名和密码的外部文件。地址:
managers = {"manager":"password", "owner":"apple"}
employees = {"jane":"none", "john":"banana"}
这是密码:
print("Welcome to Flea Master 2000...\n")
read_employee_file = open('employees_list.txt', 'r')
managers = read_employee_file.readline(0)
employees = read_employee_file.readline(1)
print(managers)
read_employee_file.close()
user_id = input("User ID:\n")
user_password = input('Password:\n')
if user_id in managers[:]:
if managers[user_id] == user_password:
print("Welcome, {0}.".format (user_id))
user_status='manager'
if user_id in employees:
if employees[user_id] == user_password:
print("Welcome, {0}".format (user_id))
user_status = 'staff'
if user_status == 'manager':
action_manager = int(input("Options: (Input number to select...)\n1) Add employee.\n2) Remove employee.\n"))
if action_manager == 1:
employee_addition_type=input("What kind of employee is he/she? ('manager' or 'staff')")
if employee_addition_type == 'manager':
new_manager_username = input("Enter the new manager's username...\n")
new_manager_password = input("Enter the new manager's password...\n")
managers[new_manager_username] = new_manager_password
else:
new_staff_username = input("Enter the new staff member's username...\n")
new_staff_password = input("Enter the new staff member's password...\n")
employees[new_staff_username]=new_staff_password
if action_manager == 2:
print("The list of current employees is: \n")
for key in all_staff:
print(key)
print('\n')
which_remove = input("Now, which do you want to remove? Enter the username exactly.\n")
if which_remove in managers:
del managers[which_remove]
else:
del employees[which_remove]
print("\nDone. Updated roster is:\n")
all_staff = dict(managers, **employees)
for key in all_staff:
print(key
)
你的
readline
行有点不正确。readlines
的参数是它将读取的最大字节数。所以readlines(6)
并不意味着“读第六行”,而是意味着“从当前行读不超过六个字符”。我建议只做read_employee_file.readline()
,不要争论。你知道吗现在您已经拥有了每一行的全部内容,但这两个变量仍然是字符串。但是,您可以使用
json
模块加载这些字典。你知道吗结果:
有几种方法可以读取和解析输入文件。假设您的文件是您指定的方式,并且您愿意处理异常,下面是一个示例方法。您需要适当地处理异常。你知道吗
相关问题 更多 >
编程相关推荐