Question:
Can someone help me with this? I am trying to make a game and save data using JSON, but it wont let me load in the JSON file.
Repl link:
https://replit.com/@SalladShooter/Python-Clicker#main.py
import json
import extrapy as e
e.clear()
import os
username = os.environ['REPL_OWNER']
data = {}
data[username] = ""
f = open('mydata.json')
data = json.load(f)
if data[username]["run":1]:
run = int(data[username]["run"])
code = int(data[username]["code"])
codePerClick = int(data[username]["codePerClick"])
codePerSecond = int(data[username]["codePerSecond"])
data[username] = {"username":username, "code":code, "codePerClick":codePerClick, "codePerSecond":codePerSecond,"run":run}
else:
run = 1
code = 0
codePerClick = 1
codePerSecond = 0
data[username] = {"username":username, "code":code, "codePerClick":codePerClick, "codePerSecond":codePerSecond,"run":run}
while True:
if username in data:
f = open('mydata.json')
data = json.load(f)
print("Press [ENTER] To Click")
option = input()
if option == "":
code = code + codePerClick
with open('mydata.json', 'w') as f:
json.dump(data[username]["code"], f)
print(code)
f.close()
that json error happens when there’s an error with the json at line 1, col 1 so you should prolly check the json file
1 Like
Currently there isn’t anything in the JSON, but when I put something the error keeps happening.
1 Like
can you send what you put?
{'SalladShooter' {"code":0}}
corrected json data:
{"SalladShooter": {"code":0}}
firstly, you forgot a colon for the key and always use double quotes ("
)
2 Likes
Now I get this error @bigminiboss ,
First of all, you can’t make a slice out of strings. Secondly, as the error says, you can index a dictionary via a slice. Did you mean to make the slices two different index operations?
How would I check if “run” is 1 using the JSON then @dragonhunter1 ?
:
is not the equal operator in python. ==
is. So you would change line 13 too:
if data[username]["run"] == 1:
Also, on line 14 to 17 using the int
operator is useless, as json can store integer formats.
1 Like
Could you clarify what you mean here? I’ve sliced strings before.
1 Like
Making a slice out of strings, not slicing a string. For example this would be incorrect:
myVar["not even a int":"not even a int 2"]
1 Like
@dragonhunter1 It can’t find my username, I went to look at the JSON and it deleted it. It should format like this
{"SalladShooter":{"code": 46, "codePerClick": 5, "codePerSecond": 0, "run": 1}
But it goes to,
{"code": 46, "codePerClick": 5, "codePerSecond": 0, "run": 1}
And it can’t find my name because of it.
This is my updated python code,
import json
import extrapy as e
e.clear()
import os
username = os.environ['REPL_OWNER']
data = {}
data[username] = ""
f = open('mydata.json')
data = json.load(f)
if data[username]["run"] == 1:
code = int(data[username]["code"])
codePerClick = int(data[username]["codePerClick"])
codePerSecond = int(data[username]["codePerSecond"])
else:
run = 1
code = 0
codePerClick = 1
codePerSecond = 0
data[username] = {"code":code, "codePerClick":codePerClick, "codePerSecond":codePerSecond,"run":run}
while True:
f = open('mydata.json')
data = json.load(f)
print("Press [ENTER] To Click")
option = input()
if option == "":
code = code + codePerClick
data[username] = {"code":code, "codePerClick":codePerClick, "codePerSecond":codePerSecond,"run":run}
with open('mydata.json', 'w') as f:
json.dump(data[username], f)
print(code)
f.close()
That is because you coded it to do that. Look at line 35:
json.dump(data[username], f)
You are not saving the whole dictionary, only the one with your username. Try this: json.dump(data, f)
Also, something else, on line 10 please use a with statement or at least close the file
Also, as I have said, the int()
’s here are unnecessary, as they are already an int type.
1 Like
system
Closed
June 15, 2023, 9:23pm
15
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.