Python Forum
How do I permanently change a list after user's input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I permanently change a list after user's input?
#1
Need Help Pray Pray
Reply
#2
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

Reply
#3
(Oct-31-2018, 07:41 AM)buran Wrote: 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.

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. 
Reply
#4
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

Reply
#5
(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.
Reply
#6
(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')
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 136 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 353 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  How to time a user's input Tbot1000 2 279 Aug-31-2020, 09:39 PM
Last Post: Larz60+
  converting user input to float troubles RecklessTechGuy 3 398 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  Use of input function to change screen background color in Turtles Oldman45 3 511 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Validating user input WJSwan 2 360 Jul-06-2020, 07:21 AM
Last Post: menator01
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 454 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  taking input doesnt print as list bntayfur 2 354 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  if the input is not number, let user input again teatea 14 969 Jun-02-2020, 07:03 PM
Last Post: ibutun
  How to allow user input only if certain requirements are met vininhosts 3 460 May-28-2020, 06:15 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020