Oct-31-2018, 06:55 AM
Need Help


How do I permanently change a list after user's input?
|
Oct-31-2018, 06:55 AM
Need Help
![]() ![]()
Oct-31-2018, 07:41 AM
Could you elaborate what does permanently change a list means?
Also it's good to show some example code what you try to do so that we can be helpful.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Oct-31-2018, 10:03 AM
(Oct-31-2018, 07:41 AM)buran Wrote: Could you elaborate what does permanently change a list means? lst = ["Laptop","Phone","Python"] user = input("Enter an element : ") lst.append(user) print(lst) #I want "lst" to be updated when i restart the program.
Oct-31-2018, 10:15 AM
To save information between separate runs you need to store information somewhere, e.g. database, simple text file, etc. Then when you run your script - read from storage, process the information in your script, e.g. - add, delete, update, etc. Depending on storage type you may need/want to update continuously while script runs, but in any case you need to save the information at least once just before before the script ends in order to store information for future use in a next run.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Oct-31-2018, 12:47 PM
(This post was last modified: Oct-31-2018, 12:47 PM by Ablazesphere.)
(Oct-31-2018, 10:15 AM)buran Wrote: To save information between separate runs you need to store information somewhere, e.g. database, simple text file, etc. Then when you run your script - read from storage, process the information in your script, e.g. - add, delete, update, etc. Depending on storage type you may need/want to update continuously while script runs, but in any case you need to save the information at least once just before before the script ends in order to store information for future use in a next run. Please show me if you have any idea to update a program permanently.
Oct-31-2018, 04:14 PM
(This post was last modified: Oct-31-2018, 04:16 PM by Gribouillis.)
(Oct-31-2018, 12:47 PM)Ablazesphere Wrote: Please show me if you have any idea to update a program permanently.You need to use a module for data persistence. There are the pickle module or the json module in the standard library, but you can also use third party modules such as tinydb from tinydb import TinyDB, Query record = Query() db = TinyDB('db.json') if db.search(record.name == 'lst'): lst = db.search(record.name == 'lst')[0]['value'] else: lst = ["Laptop", "Phone", "Python"] db.insert({'name': 'lst', 'value': lst}) user = input("Enter an element : ") lst.append(user) db.update({'value': lst}, record.name == 'lst')
Oct-31-2018, 08:30 PM
You can also just use open to create a new file and write to it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures |
|