I'm currently making a register system in Python. It did work. It appends new data ever time it inputs. But I want to make it so it denied the register process if there's an existing data. Either from the unique id, or the username. Here are my codes.
# The data input from client
uniqueid = input("Please write the desired uniqueid : ")
os.system("cls")
name = input("Write your desired username : ")
os.system("cls")
# Loading up json file
with open("uniqueid.json") as fp:
jsondata = json.load(fp)
# Appending data file
jsondata.append({
"uniqueid" : uniqueid,
"Name" : name,
"Permission level" : "1"
})
# Dumping the data
with open("uniqueid.json", 'w') as json_file:
json.dump(jsondata, json_file,
indent=4,
separators=(',',': '))
CodePudding user response:
There are several flaws in your code but here I just show one possible way to check if user with the uniqueid
is already registered.
with open("uniqueid.json") as fp:
jsondata = json.load(fp)
# you can use same syntax to check Name if you want
if any(x for x in jsondata if x['uniqueid'] == uniqueid):
print(f'user with {uniqueid} already registered, so denied')
exit(0) # stop the following code execution