#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    53
    Rep Power
    12

    having some problems with a class


    Hi all,

    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?

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    53
    Rep Power
    12
    nvm, I have it working now!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You have defined a class, but you are not using it. You need to create an instance of the class like this:
    Code:
    myNotepad = Notepad()
    This will call the __init__ function of the class to create the instance, which will run your code.

    However...

    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

IMN logo majestic logo threadwatch logo seochat tools logo