Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- # You can use a User class to make it easier to manipulate
- class User:
- def __init__(self, username, password, firstname, lastname, dateofbirth):
- self.username = username
- self.password = password
- self.firstname = firstname
- self.lastname = lastname
- self.dateofbirth = dateofbirth
- # This UserManager should help you read the users from the csv file
- # So every time you call get_users it will read the users from the csv and return it to you.
- class UserManager:
- def __init__(self):
- self.users = [] # declare an empty list of users
- def get_users(self):
- with open("users.csv", "r") as userCSV:
- user_csv_reader = csv.reader(userCSV)
- # make sure it is always empty when the read the csv
- self.users = []
- # set the first row as zero
- row_number = 0
- # iterate or loop through the content and build the users
- for row in user_csv_reader:
- # first row contains the header information and must be ignored
- # we will only populate the actual user data
- if row_number != 0 and len(''.join(row).strip()) > 0:
- current_username = row[0].strip() # first column in the csv
- current_firstname = row[1].strip() # second column in the csv
- current_lastname = row[2].strip() # third column in the csv
- current_password = row[3].strip() # forth column in the csv
- current_dateofbirth = row[4].strip() # fifth column in the csv
- # create a new user and append it to the list of users
- current_user = User(current_username, current_password, current_firstname, current_lastname,
- current_dateofbirth)
- self.users.append(current_user)
- # increment the row number
- row_number += 1
- return self.users
- def write_user(self, user):
- with open("users.csv", "a") as userCSV:
- user_csv_writer = csv.writer(userCSV, delimiter=',', quoting=csv.QUOTE_MINIMAL)
- user_csv_writer.writerow([user.username, user.firstname, user.lastname, user.password, user.dateofbirth])
- # log in user function
- def log_user_in():
- users = UserManager().get_users()
- login_name = str(input("Enter username: "))
- login_password = str(input("Enter password: "))
- # until we compare the username and password we've received we will set the logged_in_user to None
- logged_in_user = None
- for user in users:
- if user.username == login_name.lower() and user.password == login_password:
- print("{} was logged in successfully".format(user.username))
- logged_in_user = user
- return logged_in_user
- def register_user():
- print("Welcome create an account:")
- print("")
- user_mgr = UserManager()
- username = input("Enter username: ")
- password = input("Enter password: ")
- firstname = input("Enter firstname: ")
- lastname = input("Enter lastname: ")
- dateofbirth = input("Enter date of birth: ")
- user_to_register = User(username, password, firstname, lastname, dateofbirth)
- user_mgr.write_user(user_to_register)
- # Main program starts here and uses the functions above
- print("Hey, welcome to my test example.")
- print("")
- option = int(input("Enter 1 to create a new user or 2 to login with an existing user: "))
- print("")
- if option == 1:
- logged_in_user = log_user_in()
- if logged_in_user is not None:
- print("Username = {} First name={} Last name={} Date of Birth={}".format(logged_in_user.username,
- logged_in_user.firstname,
- logged_in_user.lastname,
- logged_in_user.dateofbirth))
- else:
- print("Log in failed")
- elif option == 2:
- register_user()
- else:
- print("You entered an invalid option")
Add Comment
Please, Sign In to add comment