import json
json_filename = 'users.json'
def get_users():
try:
with open(json_filename, 'r') as json_file:
return json.load(json_file)['users']
except Exception:
print(f'Файл {json_filename} не найден')
def set_user(user):
try:
data = ''
with open(json_filename, 'r') as json_file:
data = json.load(json_file)
data['users'].append(user)
with open(json_filename, 'w') as json_file:
json.dump(data, json_file)
except Exception:
print(f'Файл {json_filename} не найден')
def check_user(login, password):
for user in users:
if user['login'] == login and user['password'] == password:
return True
return False
def log_in(login, password):
if check_user(login, password):
for user in users:
if user['login'] == login:
return user
else:
return 'Неправильный логин или пароль'
def registration():
login = input('Введите логин: ')
password = input('Введите пароль: ')
name = input('Введите имя: ')
age = int(input('Введите возраст: '))
if len(login) > 3 and len(password) > 6 and len(name) > 1 and isinstance(age, int):
user = {
'login': login,
'name': name,
'password': password,
'age': age
}
set_user(user)
users = get_users()
is_guest = True if isinstance(users, list) else False
while is_guest:
print('Выберите действие, которое хотите выполнить:')
print('*\tВведите "Авторизация" чтобы войти')
print('*\tВведите "Регистрация" чтобы создать пользователя')
print('*\tВведите "Выход" чтобы выйти из программы')
user_command = input("Команда: ")
if user_command == "Авторизация":
login = input('Введите логин: ')
password = input('Введите пароль: ')
response = log_in(login, password)
if isinstance(response, dict):
authorized = True
while authorized:
...
else:
print(response)
elif user_command == "Регистрация":
registration()
elif user_command == "Выход":
is_guest = False
else:
print('Вы ввели неизвестную комманду, попробуйте снова')