Python Forum
"while" loop is looping back too early
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"while" loop is looping back too early
#1
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)
                }
            })     

 
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code won't break While loop or go back to the input? MrKnd94 2 967 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Design of borrower early warning syatem amolp 3 2,168 Dec-20-2020, 03:34 PM
Last Post: amolp
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,958 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Virtual Assistant code is looping back in python ShishirModi 1 2,539 Oct-13-2020, 08:04 AM
Last Post: Nickd12
  While loop keeps looping mcoliver88 3 2,217 Jul-29-2020, 12:48 PM
Last Post: buran
  Nested for loop not looping puttingwordstogether 0 1,722 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  while loop will not stop looping TheTechRobo 5 3,743 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  Early data import question VorpalPirate 2 2,262 Dec-09-2019, 10:52 PM
Last Post: snippsat
  out and back to loop vlk 2 1,739 Oct-07-2019, 06:26 PM
Last Post: vlk
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 2,842 Feb-13-2019, 01:26 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020