I am writing a program that will increase the XP of a user that you will type in. As this is still in test phase, the username to add the points to and XP to be updated are fixed.
Source code:
import json
f=open("userData2.json")
userData=json.load(f)
usernameArray=[]
for i in userData["data"]:
# print(i["username"])
usernameArray.append(i["username"])
# nameSearch=input("Select name to add XP:")
nameSearch="tinygirl392"
for i in userData["data"]:
if nameSearch == i["username"]:
index=usernameArray.index(nameSearch)
oldData=userData["data"][index]
print(oldData)
# newXP=input("Enter new XP Value: ")
newXP=400
oldData["xp"] += int(newXP)
newData=json.dumps(oldData)
print(newData)
userData["data"].pop(index)
userData["data"].insert(index,newData)
print(userData)
with open("userData2.json", "w") as outfile:
json.dump(userData, outfile,indent=4)
The JSON file with the names and XP:
{
"data": [
{
"username": "linda4837",
"realName": "Linda",
"xp": 0
},
{
"username": "warriorprincess",
"realName": "Sonya",
"xp": 0
},
{
"username": "richarab",
"realName": "Youssef",
"xp": 0
},
{
"username": "cat9221",
"realName": "Hina",
"xp": 0
},
{
"username": "spike_920183",
"realName": "Larry",
"xp": 0
},
{
"username": "tinygirl392",
"realName": "Celia",
"xp": 0
},
{
"username": "groupsixhunter",
"realName": "Ophelia",
"xp": 0
},
{
"username": "fr999281",
"realName": "Agathe",
"xp": 0
},
{
"username": "goldendude99",
"realName": "Mustafa",
"xp": 0
},
{
"username": "m249_girl",
"realName": "Claudia",
"xp": 0
},
{
"username": "rocky888",
"realName": "Dennis",
"xp": 0
},
{
"username": "wjh382772",
"realName": "Wei Jian",
"xp": 0
},
{
"username": "sakura161",
"realName": "Maya",
"xp": 0
}
]
}
When I run it once, the file becomes like this:
{
"data": [
{
"username": "linda4837",
"realName": "Linda",
"xp": 0
},
{
"username": "warriorprincess",
"realName": "Sonya",
"xp": 0
},
{
"username": "richarab",
"realName": "Youssef",
"xp": 0
},
{
"username": "cat9221",
"realName": "Hina",
"xp": 0
},
{
"username": "spike_920183",
"realName": "Larry",
"xp": 0
},
"{\"username\": \"tinygirl392\", \"realName\": \"Celia\", \"xp\": 400}",
{
"username": "groupsixhunter",
"realName": "Ophelia",
"xp": 0
},
{
"username": "fr999281",
"realName": "Agathe",
"xp": 0
},
{
"username": "goldendude99",
"realName": "Mustafa",
"xp": 0
},
{
"username": "m249_girl",
"realName": "Claudia",
"xp": 0
},
{
"username": "rocky888",
"realName": "Dennis",
"xp": 0
},
{
"username": "wjh382772",
"realName": "Wei Jian",
"xp": 0
},
{
"username": "sakura161",
"realName": "Maya",
"xp": 0
}
]
}
What have I done wrong here?