Advertisement
Guest User

Sys log

a guest
Feb 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. """
  2.  
  3.    Log in system with a feature of changing username and password
  4.  
  5. """
  6.  
  7. import os
  8. import json
  9.  
  10.  
  11. def input_func(part):        # 2, the user will be inputting here the choice of what he want to do.
  12.     if part == 1:            # part 1 == while not logged in | part 2 == while logged in.
  13.         while True:
  14.             choice = input("""Input the number of your choice:
  15.            1. Sing up
  16.            2. Login
  17.            7. Exit\n""")
  18.             try:
  19.                 choice = int(choice)
  20.  
  21.             except ValueError:
  22.                 print('Input the number of your choice')
  23.  
  24.             if choice in [1, 2, 7]:
  25.                 break
  26.  
  27.             else:
  28.                 print('Invalid number')
  29.  
  30.         return choice
  31.  
  32.     if part == 2:
  33.         while True:
  34.             choice2 = input("""Input the number of your choice:
  35.            3. Change password
  36.            4. Change username
  37.            5. Logout
  38.            7. Exit\n""")
  39.             try:
  40.                 choice2 = int(choice2)
  41.  
  42.             except ValueError:
  43.                 print('Input the number of your choice')
  44.  
  45.             if choice2 in [3, 4, 5, 7]:
  46.                 break
  47.  
  48.             else:
  49.                 print('Invalid number')
  50.  
  51.         return choice2
  52.  
  53.  
  54. def sign_up_system():    # 2, function that responsible for the signing up.
  55.     user_name = input('Input Username: ')
  56.     password = input('Input password: ')
  57.     storage_info[user_name] = password
  58.  
  59.     with open('logins.json', 'w') as j:
  60.         json.dump(storage_info, j)
  61.     print('''Registered!
  62.    Username: {}
  63.    Password: {}
  64.    '''.format(user_name, password))
  65.  
  66.  
  67. def change_pass(user_name):       # 3, function that changes your pass.
  68.     new_pass = input('Type your new password: ')
  69.     print('Your new password is: {}'.format(new_pass))
  70.     storage_info[user_name] = new_pass
  71.     with open('logins.json', 'w') as j:
  72.         json.dump(storage_info, j)
  73.  
  74.  
  75. def change_usrname(old_user_name):    # 4, function that changes your user name.
  76.     new_username = input('Type your new username: ')
  77.     print('Your new username is: {}'.format(new_username))
  78.     storage_info[new_username] = storage_info.pop(old_user_name)
  79.     with open('logins.json', 'w') as j:
  80.         json.dump(storage_info, j)
  81.  
  82.  
  83. def check_login(username, password):
  84.     if storage_info[username] == password:
  85.         return True
  86.     else:
  87.         return False
  88.  
  89.  
  90. def main():              # The main function that includes everything and runs the program.
  91.     print("Hi welcome to the Login system\n")
  92.  
  93.     with open('logins.json', 'r')as j:
  94.         global storage_info
  95.         storage_info = json.load(j)
  96.  
  97.     done = False
  98.     while not done:
  99.  
  100.         logged_in = False
  101.         while not logged_in:
  102.  
  103.             choice = input_func(1)
  104.  
  105.             if choice == 1:  # Sign up
  106.                 sign_up_system()
  107.  
  108.             elif choice == 2:
  109.                 logging_in = False
  110.                 while not logging_in:  # Log in checks checks if the user and pass are valid or not.
  111.                     log_in_user_name = input('Username: \n')
  112.                     log_in_user_pass = input('Password: ')
  113.                     logging_in = check_login(log_in_user_name, log_in_user_pass)
  114.  
  115.                     if logging_in is False:
  116.                         'Invalid username or password'
  117.  
  118.                     elif logging_in is True:
  119.                         print('Hi {}, You are logged in!\n'.format(log_in_user_name))
  120.                         logged_in = True
  121.  
  122.             elif choice == 7:  # Exit
  123.                 raise SystemExit(0)
  124.  
  125.         while logged_in:
  126.  
  127.                 choice2 = input_func(2)  # input function part 2
  128.  
  129.                 if choice2 == 3:  # change password
  130.                     change_pass(log_in_user_name)
  131.  
  132.                 elif choice2 == 4:  # Change username
  133.                     change_usrname(log_in_user_name)
  134.  
  135.                 elif choice2 == 5:  # Logout
  136.                     logged_in = False
  137.  
  138.                 elif choice2 == 7:  # Exit
  139.                     raise SystemExit(0)
  140.  
  141.  
  142. if __name__ == '__main__':
  143.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement