Ask coding questions

← Back to all posts
Remembering variables - Python
h
XanthusPettitt (10)

I am trying to make the code remember variables from a past run(for example lets say you ran a repl today and you save and quit so next time you run the repl it has your data saved.) I want a way to have it save the data

Answered by AarjavKavathia (5) [earned 5 cycles]
View Answer
Comments
hotnewtop
AarjavKavathia (5)

To save the variables permanently, you need to use a database. I would suggest using SQLite or the ReplitDB. You can just store the variables there.

XanthusPettitt (10)

@AarjavKavathia ok thank you I think I can do that but how would I get the variables in or out?

AarjavKavathia (5)

You can set the variable values to the database by using db["key"] = "value" In your case "value" is the value of the variable and "key" is whatever you want to identify the variable by. To get the variable and display it you can just use value = db["key"] and then print(value). Lastly, if you want to delete the variable, just use del db["key"]. Just remember to import db from replit before this. In a python repl, you can view the commands in the cylinder icon on the left.
@XanthusPettitt

XanthusPettitt (10)

@AarjavKavathia ok that makes sense thank you so much!