The import json
Def exist_name () :
Name_path="D:/dedicated test documents/remember_me json"
Try:
With the open (name_path) as path_file:
Name_list=json. The load (path_file)
Except FileNotFoundError:
Return None
Except the json. The decoder. JSONDecodeError:
Return None
The else:
Return name_list
Def first_add_name () :
Name_list=input (" please input your name: ")
Name_path="D:/dedicated test documents/remember_me json"
With the open (name_path, "a") as path_file:
Json. Dump (name_list path_file)
Return name_list
Def new_add_name () :
Name_list=input (" please input the name again: ")
Name_path="D:/dedicated test documents/remember_me json"
With the open (name_path, "a") as path_file:
Json. Dump (name_list path_file)
Return name_list
Def name_check () :
Name_list=exist_name # () here want to use name_list
If name_list:
Confirm=input (" please input your name: ")
If confirm in name_list:
Print (" welcome back, "+ STR (confirm))
The else:
Check=input (" the name does not exist in the database, whether to add? (y/n) ")
If the check=="y" :
Name_list=new_add_name ()
Print (" hello, "+ STR (name_list) +" already stored!" )
The else:
Name_list=exist_name ()
Print (" no added "+ STR (name_list))
The else:
Name_list=first_add_name ()
Print (" hello, "+ STR (name_list) +" already stored!" )
Name_check ()
CodePudding user response:
It is recommended to use writing way, don't write additional.