January 2nd, 2005, 05:38 PM
having some problems with a class
I'm trying to create a simple program that will, depending on what you type in, open up a file and read it, create a file and write to it, delete a file, or list all files in a directory. I created that successfully with just using a while loop and some if-elif statments, but you could only perform one option then the script exited. I want to be able to perform whatever option as many times as I want, so I thought about putting it into a class.
So, when initiated, it would print out a list of options plus a raw_input() asking which one you wanted to do. Once you have entered in a value, it would perform the option, then ask you again what you would like to do.
Here is my somewhat ugly code:
(code: see below post)
The above code, when ran, shows nothing and just waits for my raw_input at the bottom. Can anyone see what I am doing wrong? Does anyone understand what I am trying to get at?
January 2nd, 2005, 06:17 PM
nvm, I have it working now!
January 2nd, 2005, 06:22 PM
You have defined a class, but you are not using it. You need to create an instance of the class like this:
This will call the __init__ function of the class to create the instance, which will run your code.
myNotepad = Notepad()
1) there are errors in the code, so it will not run. I could tell you what they are, but it will be more instructive for you to find and fix them yourself. If you get stuck then ask here. (Hint: read the docs about global and local variables).
2) The point of OO programming with classes is that you can bundle data and functions together into a single object. You have moved all of your functions into a class, but the class has no data, so there is nothing gained from having the class.
Since you wanted to stop your original code from exiting after processing one option then the solution was to wrap your original code in a 'while 1:' loop, not put it in a class. Classes are useful for solving a lot of problems, but this isn't one of them.
Dave - The Developers' Coach