简体   繁体   中英

How can I save an input value so I can retrieve it next time I run the code (Python)?

Is there an easy way for me to store user input data so next time I run the program, it considers the previous value assigned to a variable?

I want to create a program to add 1 absence every time I don't attend a class. However, I don't know how to store that data. Ex.: I added 1 absence, and next time I run the code and add another, I want the program to retrieve 2 and not 1.

absence = None
userinput = input("add absence:")
userinput = int(userinput)
if absence == None:
   absence = userinput
else:
   absence = absence+userinput
print(absence)

I am getting a feeling you dont want to set up and maintain a database for this, you could go with the approach of saving or persisting the data in a json or xml file to your local path (or a cloud location) and access it every time your code runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM