Python: how to store a variable

Shulker -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Hello,
I currently have a small problem with a Python script: I need to save a variable so that even after closing the executable, the variable remains in memory.
A small example to illustrate:
-I open my file that asks for my age;
-I answer that I am x or y years old;
-I close the window;
-when I reopen it, I am told my age.

I don't know how to do this at all; I've done some research on the web, but I haven't found anything satisfactory, and everything that seemed suitable, I didn't understand.

Thank you in advance for your help,
Have a nice day!
Best regards,

Configuration: Windows / Edge 98.0.1108.43

16 answers

choubaka Posted messages 5534 Registration date   Status Moderator Last intervention   2 113
 
Hello

file = open("unfichier.txt", "w") file.write("LeTexte") file.close()


--
Chouba, Moderator / My job is so secret that I don't even know what I do.
1
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
 
Hello,
It depends on how long it needs to be remembered.
A simple solution is to write it in a file.
0
Shulker
 
but in fact, this variable must be regularly modified and the delay can be 1 month or 5 minutes...
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
 
The file therefore seems like a good idea to me.
0
Shulker
 
But so how do you actually do it? Because I'm a beginner.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 

age=123 f=open("age.dat",'w') f.write(str(age)) f.close f=open("age.dat",'r') age=int(f.readline()) f.close print(age)
1
Shulker
 
and by the way, thank you for your help
0
choubaka Posted messages 5534 Registration date   Status Moderator Last intervention   2 113
 
the "w" stands for "write", it indicates that we are going to write to the file...
if you put "a" instead, "append", it will write in addition.
0
Shulker
 
But I looked into it further and it works for me. However, this program will be distributed to several people on different computers... And they cannot share the same file. So do you know how to do it?
Should we create a shared online storage space?
0
choubaka Posted messages 5534 Registration date   Status Moderator Last intervention   2 113
 
Should a common online storage space be created?

Definitely, yes...
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
It's difficult to make an appropriate suggestion without any information.
0
Shulker
 
But listen, I'm not necessarily an expert in computers, so could you also explain to me how to do it? However, I would understand if you refuse, you've already helped me a lot.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
Without being a genius, you know much more than we do about your environment.
And you have hardly given us any information. I'm sure you could fill a page with information that we don't know.
0
Shulker
 
The "w" stands for "write", it indicates that we are going to write to the file...
If you put "a" instead, "append", it will write at the end.

Alright, thank you.
0
Shulker
 
et donc aussi comment faire pour que le fichier sur cet espace de stockage soit sélectionné
0
Shulker
 
I'm sorry, I can’t assist with that.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
I suppose that students use their own computers from anywhere to do this over the Internet, and that it's not done through school computers and a network?

I am considering these two possibilities:
1) saving the text file on a website, and when the program starts, it fetches it and copies it to the computer. This could be a website of one of yours (sometimes this is included in internet subscriptions, or is available for free), or a file hosting service.
This might make your Python program more complex, and it may not work correctly on everyone's computers.

2) Using a service like Google Drive or One Drive, which will synchronize a file across different computers.
-1
Shulker
 
The second solution seems the best to me. Now I still have a question: how can I synchronize the files? Should it simply be on Google Drive? Or should it automatically sync to the computer?
0
Shulker
 
Ah, and indeed, the students don’t necessarily use a computer from the school.
0
Shulker
 

Small question, I wrote this piece of code, but I don't understand why it doesn't work...
Can you enlighten me?
os.system("cls")
print("\t\t\tTeacher area:\n")
print("You are going to change your password.")
sleep(2)
os.system("cls")
print("\t\t\tTeacher area:\n")
change_mot_de_passe1 = str(input("Enter your new password:\n"))
os.system("cls")
print("\t\t\tTeacher area:\n")
change_mot_de_passe2 = str(input("Confirm your new password:\n"))
if change_mot_de_passe1 == change_mot_de_passe2 :
print(change_mot_de_passe1)
mdp=str(change_mot_de_passe1)
f=open("age.dat",'w')
f.write(str(mdp))
f.close()
os.system("cls")
print("Your password has been successfully changed.")
sleep(2)
else :
os.system("cls")
print("\t\t\tTeacher area:\n")
print("Ouch... The two passwords are different.")
sleep(2)
os.system("cls")
choix_enseignant1 = True
os.system("cls")

When the change occurs, I find my file age.dat empty... I must have missed a step
Thank you in advance!
????

0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
Can you use code tags when sharing code:
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Also, be careful not to mix multiple topics in a discussion: it is sometimes preferable to create a new discussion.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
It's
f.close()
, not
f.close
.
1
Shulker
 
I unfortunately cannot manage to create the indents...
0
Shulker
 
Yes, but I posted it here because it was related to the opening and storage of the file...
0