Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Log in system with a feature of changing username and password
- """
- import os
- import json
- def input_func(part): # 2, the user will be inputting here the choice of what he want to do.
- if part == 1: # part 1 == while not logged in | part 2 == while logged in.
- while True:
- choice = input("""Input the number of your choice:
- 1. Sing up
- 2. Login
- 7. Exit\n""")
- try:
- choice = int(choice)
- except ValueError:
- print('Input the number of your choice')
- if choice in [1, 2, 7]:
- break
- else:
- print('Invalid number')
- return choice
- if part == 2:
- while True:
- choice2 = input("""Input the number of your choice:
- 3. Change password
- 4. Change username
- 5. Logout
- 7. Exit\n""")
- try:
- choice2 = int(choice2)
- except ValueError:
- print('Input the number of your choice')
- if choice2 in [3, 4, 5, 7]:
- break
- else:
- print('Invalid number')
- return choice2
- def sign_up_system(): # 2, function that responsible for the signing up.
- user_name = input('Input Username: ')
- password = input('Input password: ')
- storage_info[user_name] = password
- with open('logins.json', 'w') as j:
- json.dump(storage_info, j)
- print('''Registered!
- Username: {}
- Password: {}
- '''.format(user_name, password))
- def change_pass(user_name): # 3, function that changes your pass.
- new_pass = input('Type your new password: ')
- print('Your new password is: {}'.format(new_pass))
- storage_info[user_name] = new_pass
- with open('logins.json', 'w') as j:
- json.dump(storage_info, j)
- def change_usrname(old_user_name): # 4, function that changes your user name.
- new_username = input('Type your new username: ')
- print('Your new username is: {}'.format(new_username))
- storage_info[new_username] = storage_info.pop(old_user_name)
- with open('logins.json', 'w') as j:
- json.dump(storage_info, j)
- def check_login(username, password):
- if storage_info[username] == password:
- return True
- else:
- return False
- def main(): # The main function that includes everything and runs the program.
- print("Hi welcome to the Login system\n")
- with open('logins.json', 'r')as j:
- global storage_info
- storage_info = json.load(j)
- done = False
- while not done:
- logged_in = False
- while not logged_in:
- choice = input_func(1)
- if choice == 1: # Sign up
- sign_up_system()
- elif choice == 2:
- logging_in = False
- while not logging_in: # Log in checks checks if the user and pass are valid or not.
- log_in_user_name = input('Username: \n')
- log_in_user_pass = input('Password: ')
- logging_in = check_login(log_in_user_name, log_in_user_pass)
- if logging_in is False:
- 'Invalid username or password'
- elif logging_in is True:
- print('Hi {}, You are logged in!\n'.format(log_in_user_name))
- logged_in = True
- elif choice == 7: # Exit
- raise SystemExit(0)
- while logged_in:
- choice2 = input_func(2) # input function part 2
- if choice2 == 3: # change password
- change_pass(log_in_user_name)
- elif choice2 == 4: # Change username
- change_usrname(log_in_user_name)
- elif choice2 == 5: # Logout
- logged_in = False
- elif choice2 == 7: # Exit
- raise SystemExit(0)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement