I have the following code which prompts the user for a username, then checks if that username exists in a json file, if so the script should prompt the user 3 times maximum to enter another username, otherwise just accept the entered username and continue.
The issue comes when I try to reinitiate the for loop j = test1["users"][0]
so that in the second attempt, this script checks the json file from the beginning not from the point it was.
When the script comes back to the for loop after the else statement, it just simply ignores the whole loop and continue to the next section of the code without any error...
The for loop is looping in a dictionary which is in a list:
#prompt for username
x["User Name"] = input("Please type your user name: ")
i = 0
#for loop to check if the entered username already exists
for j in test1["users"]:
while j["User Name"] != x["User Name"] and i < 3:
break
else:
print("username already exists, try again")
x["User Name"] = input("Please type a new user name: ")
i += 1
j = test1["users"][0]
#prompts the user for additional information if username pass `succesfully`
x["First Name"] = input("Please type your first name: ")
x["Last Name"] = input("Please type your last name: ")
x["Age"] = input("Please type your age: ")
A sample of test1["users]
:
{
"users": [
{
"First Name": "jhon",
"Age:": "30",
"Last Name": "kerry",
"User Name": "jhon",
"Height": "170",
"Weight": "70"
},