2 Replies - 879 Views - Last Post: 18 May 2014 - 09:11 AM Rate Topic: -----

#1 Hj Networks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 14

How to save a program on exit?

Posted 18 May 2014 - 02:11 AM

Hi there, beginner Python coder, anyyyways, i wanted to know if there was a way i could save a program upon exit, like in the program the user would enter his/her name and they would be reading a short paragraph and then after that short paragraph there would be a 2nd paragraph, and lets say the user would exit the program, would there be a way to save the entered name and when the user comes back to the program start them where they last left off? Thanks to all answers.
Is This A Good Question/Topic? 0
  • +

Replies To: How to save a program on exit?

#2 Peter O   User is offline

  • D.I.C Regular

Reputation: 129
  • View blog
  • Posts: 306
  • Joined: 19-October 13

Re: How to save a program on exit?

Posted 18 May 2014 - 03:12 AM

I think you will have to store the names in a file. When the program is closed you write the strings to a file and when the program starts you read the names from the file.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon

Reputation: 11126
  • View blog
  • Posts: 19,069
  • Joined: 19-March 11

Re: How to save a program on exit?

Posted 18 May 2014 - 09:11 AM

Yes, you will need to come up with some sort of persistent storage - a flat file, a database, or something of that nature.

The most common solution for the beginner is the flat file. For that you want to come up with some sort of file format (CSV, or character-separated values, is the solution most people come to, but you might also write it to a file as JSON or find some other solution that suits your needs) and figure out how to write your data to the file, and then how to read it back in.
Should be a good afternoon's work for you to figure all of this out.

Database storage is overkill for this particular problem, but interesting to play with. If you want to experiment with that, I suggest you consider playing with mysqlite, which is a lightweight database management tool using mysql that writes to a flat file instead of a full-fledged database. That's a bit more time investment, but certainly worth while in the long run.


when the user comes back to the program start them where they last left off?

Just to be clear on this - you can't just freeze a program in execution and "pick up where you left off". The program will exit and lose all state. In order to give the user the impression of "picking up where they left off" you have to carefully recreate the state they were in when they left the program. This is not a huge problem for a small program, but for a complex program it can be a very challenging endeavor. To get your head around this, try to think how you'd implement a simple "undo" for some step of a program. For example, suppose you have a tic-tac-toe game, and you want to give the user the chance to "take back" their move. How do you do it? Assume that you want to be able to undo all the way back to the start of the game, and you'd also like to be able to "redo" all the way up to the latest move. Clearly. a similar process of "redo" would be one way (not the only or necessarily the best way) to achieve this "freezing" of state.

This is actually a pretty deep question for a beginner, but it's worth thinking about. It'll help to think about it as a very high-level question to begin with: imagine that you are capable of doing anything that you can describe coherently. Use this power to describe the solution that you'd like to implement. Then maybe think about how you'd break down the new problems you've created.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1