Thread: Stop Loop

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155

    Stop Loop


    Can anyone show me how to stop the code below at a designated time? You will probably notice what I'm trying to do in the code.
    Code:
    class StartSafelist:
        if not os.path.isfile('safelist.txt'):
            print 'Could not find safelist.txt!'
            safefound = False
        else:
            safefound = True
            fileSafe = open ( 'safelist.txt' )
            fileList = fileSafe.readlines()
            for item in fileList:
                tempSafe.append(item)
            for item in tempSafe:
                item = item.replace('\n', '')
                safelist.append(item)
            fileSafe.close()
    class FoundSafe:
        safesent = ['boolean']
        if StartSafelist.safefound == True:
            pass
        else:
            while True:
                if os.path.isfile('safelist.txt'):
                    StartSafelist.safefound = True
                    if safesent[0] == 'True':
                        pass
                    else:
                        if StartSafelist.safefound == True:
                            print 'safelist.txt has been found!'
                            safesent[0] == 'True'
                        else:
                            pass
                elif not os.path.isfile('safelist.txt'):
                    pass
                else:
                    pass
    I wan't the code to print 'safelist.txt has been found!' only once. With that code, it sends it over and over and over, and never stops. Thanks in advance.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The simplest way would be to use an if statement inside the to check the time then use the break keyword to exit the loop. I say the simplest way because you wouldn't have to change you while loop at all, however it would make sense to integrate the check into the loop. Here’s a little pseudocode:

    Code:
    exitTime = setTheTimeToBreak.
    currentTime  = getTheCurrentTime.
    
    while (currentTime <= exitTime):
        ...preform action...
        currentTime  = getTheCurrentTime.
    Hope this helps, Night all.

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Actually, I had forgotten all about break. I'm not sure if you understood me correctly, maybe I didn't ask my question properly. But it works fine with using break. Thanks.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There were a few things I was wondering about your code, but only had time for a short reply; for instance, why have the loop at all? It would probably be better to create the file than to wait forever for the user to create one themselves. Maybe you do this elsewhere in your program?

    It would also make more sense to me to use a function rather than a class container for StartSafelist, but that could just be me .

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo