Guest User

Untitled

a guest
Oct 17th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import csv
  2.  
  3.  
  4. # You can use a User class to make it easier to manipulate
  5. class User:
  6. def __init__(self, username, password, firstname, lastname, dateofbirth):
  7. self.username = username
  8. self.password = password
  9. self.firstname = firstname
  10. self.lastname = lastname
  11. self.dateofbirth = dateofbirth
  12.  
  13.  
  14. # This UserManager should help you read the users from the csv file
  15. # So every time you call get_users it will read the users from the csv and return it to you.
  16. class UserManager:
  17. def __init__(self):
  18. self.users = [] # declare an empty list of users
  19.  
  20. def get_users(self):
  21. with open("users.csv", "r") as userCSV:
  22. user_csv_reader = csv.reader(userCSV)
  23. # make sure it is always empty when the read the csv
  24. self.users = []
  25. # set the first row as zero
  26. row_number = 0
  27. # iterate or loop through the content and build the users
  28. for row in user_csv_reader:
  29. # first row contains the header information and must be ignored
  30. # we will only populate the actual user data
  31. if row_number != 0 and len(''.join(row).strip()) > 0:
  32. current_username = row[0].strip() # first column in the csv
  33. current_firstname = row[1].strip() # second column in the csv
  34. current_lastname = row[2].strip() # third column in the csv
  35. current_password = row[3].strip() # forth column in the csv
  36. current_dateofbirth = row[4].strip() # fifth column in the csv
  37.  
  38. # create a new user and append it to the list of users
  39. current_user = User(current_username, current_password, current_firstname, current_lastname,
  40. current_dateofbirth)
  41. self.users.append(current_user)
  42.  
  43. # increment the row number
  44. row_number += 1
  45.  
  46. return self.users
  47.  
  48. def write_user(self, user):
  49. with open("users.csv", "a") as userCSV:
  50. user_csv_writer = csv.writer(userCSV, delimiter=',', quoting=csv.QUOTE_MINIMAL)
  51. user_csv_writer.writerow([user.username, user.firstname, user.lastname, user.password, user.dateofbirth])
  52.  
  53.  
  54. # log in user function
  55. def log_user_in():
  56. users = UserManager().get_users()
  57.  
  58. login_name = str(input("Enter username: "))
  59. login_password = str(input("Enter password: "))
  60.  
  61. # until we compare the username and password we've received we will set the logged_in_user to None
  62. logged_in_user = None
  63.  
  64. for user in users:
  65. if user.username == login_name.lower() and user.password == login_password:
  66. print("{} was logged in successfully".format(user.username))
  67. logged_in_user = user
  68.  
  69. return logged_in_user
  70.  
  71.  
  72. def register_user():
  73. print("Welcome create an account:")
  74. print("")
  75. user_mgr = UserManager()
  76.  
  77. username = input("Enter username: ")
  78. password = input("Enter password: ")
  79. firstname = input("Enter firstname: ")
  80. lastname = input("Enter lastname: ")
  81. dateofbirth = input("Enter date of birth: ")
  82.  
  83. user_to_register = User(username, password, firstname, lastname, dateofbirth)
  84.  
  85. user_mgr.write_user(user_to_register)
  86.  
  87.  
  88. # Main program starts here and uses the functions above
  89. print("Hey, welcome to my test example.")
  90. print("")
  91.  
  92. option = int(input("Enter 1 to create a new user or 2 to login with an existing user: "))
  93. print("")
  94.  
  95. if option == 1:
  96. logged_in_user = log_user_in()
  97. if logged_in_user is not None:
  98. print("Username = {} First name={} Last name={} Date of Birth={}".format(logged_in_user.username,
  99. logged_in_user.firstname,
  100. logged_in_user.lastname,
  101. logged_in_user.dateofbirth))
  102. else:
  103. print("Log in failed")
  104.  
  105. elif option == 2:
  106. register_user()
  107.  
  108. else:
  109. print("You entered an invalid option")
Add Comment
Please, Sign In to add comment