#๐Ÿ”’ I need help with saving multiples peoples data

24 messages ยท Page 1 of 1 (latest)

autumn lightBOT
#

@lyric bobcat

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wise ridge
#

...

lyric bobcat
#

it wont let me send the code

wise ridge
#

!code

autumn lightBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

#

Hey @lyric bobcat!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
lyric bobcat
#
#-----import statements-----
import turtle as trtl
import random as rand
import json

#-----game configuration----
var1 = 0
var2 = 0
var3 = 0
var4 = 0
#username = "sal"
x = input("Enter a username: ")
username = x
    
#-----initialize turtle-----
#Button
t = trtl.Turtle()
t.penup()
t.shape("circle")
t.color("black")
t.fillcolor("red")
t.shapesize(10, 10, outline=10)
#Write Variables
text = trtl.Turtle()
text.penup()
text.speed(100)
text.hideturtle()
#Write Info
info = trtl.Turtle()
info.penup()
info.speed(100)
info.hideturtle()
info.goto(-300,300)
info.write("Save: 1", font = ("Arial", 25, "normal"))
info.goto(-300,250)
info.write("Load: 2", font = ("Arial", 25, "normal"))
info.goto(-300,200)
info.write("Reset: 3", font = ("Arial", 25, "normal"))
#
#-----game functions--------
def Roll(x,y):
    global var1, var2, var3, var4
    var1 = rand.randint(100,10000)
    var2 = rand.randint(100,10000)
    var3 = rand.randint(100,10000)
    var4 = rand.randint(100,10000)
    Write()

def Write():
    text.clear()
    text.goto(-100,300)
    text.write("var1: " + str(var1), font = ("Arial", 25, "normal"))
    text.goto(-100,250)
    text.write("var2: " + str(var2), font = ("Arial", 25, "normal"))
    text.goto(-100,200)
    text.write("var3: " + str(var3), font = ("Arial", 25, "normal"))
    text.goto(-100,150)
    text.write("var4: " + str(var4), font = ("Arial", 25, "normal"))

#fix this so that when you save it checks if you are already in the file and if not it adds
#a new item to the list and if you are in the list it replaces that item with your save and
#make it so it actually saves your variables and not just random numbers which i put for testing
def Save():
    global username, var1, var2, var3, var4, data
    with open("save.json", "w") as f:
        data = [
            {"Username": "sal", "var1": 7135, "var2": 9236, "var3": 1247, "var4": 4819},
            {"Username": "test", "var1": 4630, "var2": 2181, "var3": 9534, "var4": 2338},
            {"Username": "abc", "var1": 3265, "var2": 4175, "var3": 6408, "var4": 6307},
        ]
        with open("save.json", "w") as f:
            json.dump(data, f, indent=4)
#
#fix this so it checks each item of the data list and see which item of the list has your username and if its not in the first item
#of the list check the next item and repeat till it went through everything and set all the variables to each item in the list. 
#each item in the data list is a list and use that list to set the variables.
def Load():
    global username, var1, var2, var3, var4
    with open ("save.json", "r") as f:
        data = json.load(f)
        for i in range(3):
            if str(username) in int(data["Username"]):
                print("loaded")
            else:
                print(username)
                print(int(data["Username"]))

        #[{'Username': 'sal', 'var1': 7135, 'var2': 9236, 'var3': 1247, 'var4': 4819},
        # {'Username': 'test', 'var1': 4630, 'var2': 2181, 'var3': 9534, 'var4': 2338},
        # {'Username': 'abc', 'var1': 3265, '0': 4175, 'var3': 6408, 'var4': 6307}]

        #data[0] = {'Username': 'sal', 'var1': 7135, 'var2': 9236, 'var3': 1247, 'var4': 4819}
        #data[1] = {'Username': 'test', 'var1': 4630, 'var2': 2181, 'var3': 9534, 'var4': 2338}
        #data[2] = {'Username': 'abc', 'var1': 3265, '0': 4175, 'var3': 6408, 'var4': 6307}


        '''username = int(data["Username"])
        var1 = int(data["var1"])
        var2 = int(data["var2"])
        var3 = int(data["var3"])
        var4 = int(data["var4"])
        Write()'''

'''def ResetSave():
    global saveString
    with open ("save.json", "w") as data:
        saveString = [username, 0, 0, 0, 0]
        json.dump(str(saveString))
    Load()'''

#-----events----------------
wn = trtl.Screen()
Write()
t.onclick(Roll)
wn.onkeypress(Save, "1")
wn.onkeypress(Load, "2")
#wn.onkeypress(ResetSave, "3")
wn.listen()
wn.mainloop()
#
#this is the save.json
[
    {
        "Username": "sal",
        "var1": 7135,
        "var2": 9236,
        "var3": 1247,
        "var4": 4819
    },
    {
        "Username": "test",
        "var1": 4630,
        "var2": 2181,
        "var3": 9534,
        "var4": 2338
    },
    {
        "Username": "abc",
        "var1": 3265,
        "var2": 4175,
        "var3": 6408,
        "var4": 6307
    }
]
livid grove
#

is there a question here?

lyric bobcat
#

yeah im trying to make it so it saves the data of each person who saves and loads the list of the persons username they typed in

wise ridge
#

well, what is it doing; and how does that differ from what you expected it to do?

#

I notice your Save function opens the output file twice.

lyric bobcat
#

when you start the game you type a username you want and you click a button to make 4 variables equal to a random number then when you press 1 it saves it to a json file and if you already have a save in the list it replaces it with the new numbers and when you press 2 it reads the json file and makes it a list and it checks each item of the list to see which has your username in it and if its the 3rd item it will set each of your variables to the each item of the list in item 3

wise ridge
#

That's certainly not necessary, and possibly an error

lyric bobcat
wise ridge
lyric bobcat
#

im trying to make it do that but rn it doesnt load anything and doesnt save anything yet

wise ridge
#

I doubt I'll be able to help, since I don't understand your actual problem.

lyric bobcat
#

im trying to make it save multiples peoples variables and load the persons variables

#

i was able to figure out the loading on my own but i still cant figure out the saving

autumn lightBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.