Python Forum

Full Version: "while" loop is looping back too early
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some code with a "while" loop. The loop encompasses fewer instructions than I want. In other words, it loops back too early. I am a hobbiest and am learningn Python to write scripts.
I want to loop on all of the code following the "while." The while loop only gets to the lines before the asterisks comment before it loops back.
Here is the output I get:
Executing RunScript
2
3
4
5
Executing EnableOptimizedScriptUndo
Executing LayerDuplicate (I want this within the loop)
Executing Vector Selection Update (I want this within the loop)
Script '000testmove' has completed successfully.

Here is the code:

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'P',
        'Host Version': u'24.00'
        }
    # Begin Loop ++++++++++++++++++++++++++++++++
numtimes = 1
while numtimes < 5: 
  numtimes += 1
  print numtimes  
  def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((24,0,0),1)
                }
            })
    # LayerDuplicate
    App.Do( Environment, 'LayerDuplicate', {
          'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((24,0,0),1)
                }
            })
    # Vector Selection Update
    App.Do( Environment, 'VectorSelectionUpdate', {
            'Path': (0,-1,[],False), 
            'Type': App.Constants.ObjectSelection.Select, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Default, 
                'Version': ((24,0,0),1)
                }
            })     

 
Within the loop you define function "Do()". And this happens four times, it is not practical to define a function more than once. But instead you should call the function. So define the function above the loop and call it inside the loop.