"python Learning: from beginner to practice" I modeled on an example in the book. Look for the user name, find it, show it, and write to the file if you can"t find it. Now use json to store, only one name can work properly. I think json didn"t parse correctly, and I couldn"t find the answer after looking for it on the Internet. I hope you can tell me how to modify the code.
import json
def save_name():
print("")
name = input("\n :")
with open("name.json","a") as file:
json.dump(name,file)
print(""+str(name))
return name
def find_name(fn):
try:
file = open("name.json","r")
except FileNotFoundError:
return save_name()
else:
name = json.load(file)
for i in name:
if fn == i:
return fn
else:
return save_name()
def hello_name():
name = input("\n :")
print(""+str(find_name(name)))
hello_name()