Home > other >  Want to seek advice, the json. The decoder problem how should solve
Want to seek advice, the json. The decoder problem how should solve

Time:11-19

Specific code is such, no files originally, running, and then produce a new file, first enter name: Jimmy, after the second input name, such as Tom, and then prompted to name if there is no new, choose Y, then add Tom's name, here are normal, and then the third input name is found to have problems, system error shows the json decoder. JSONDecodeError, then I also joined the except in exist_name json. The decoder JSONDecodeError:, but it still has a problem, until after losing the second new name was OK, input the third name, such as a Kitty, but the system did not prompt said, name if there is no new, but the new Kitty this name, then enter the fifth have prompted again, when a new name to the sixth was no again, anyhow is two cycle, all want to consult your next, what this code where the problem is, thank you very much

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.
  • Related