I am writing simple application which has json file for store some user's information.
{
"users": [
{
"last result": 15,
"login": "admin",
"id": 1,
"password": "1"
},
{
"last result": 2,
"login": "user",
"id": 2,
"password": "1"
}
]
}
For example I need to change value "last result" from "user" = admin I have method for take data from json:
def load_data_from_json(test_file_name):
with open(test_file_name, encoding="utf-8") as data_file:
return json.load(data_file)
test_file_name - json's name (in my code it's - "auth.json")
Then I'm trying to set new value:
def set_last_result(login, new_result):
for user in load_data_from_json("auth.json")["users"]:
if user["login"] == login:
user["last result"] = new_result
raise Exception("User '{}' not found.".format(login))
But I have an error:
File "/Users/future/PycharmProjects/module_for_test/user.py", line 53, in set_last_result
raise Exception("User '{}' not found.".format(login))
Exception: User 'admin' not found.
If I just take the "last result"value
def get_last_result(login):
for user in load_data_from_json("auth.json")["users"]:
if user["login"] == login:
return user["last result"]
raise Exception("User '{}' not found.".format(login))
All work's well. Where is my mistake?
P.S. I'm using Python 3.4
P.S.S Fix my error message.