Skip to main content

How to Start/Stop your Script in Python

Sometimes when we’re running a script, we want to be able to break down and review portions of our code. In Alteryx, we can easily do that with the cached results function in every tool or we can use the Browse tool.

In Python, we can use this little trick with the “raw_input()” function.

Say for instance we want to run 2 pieces of code

  1. Give me a list of odd numbers
  2. Give me a list of even numbers

But I want to be able to check over my work in the first section before running the 2nd. This is really useful if your data is EXTREMELY large and takes a long time to run. To avoid the pain of spending 20 minutes waiting for your script to finish running, only to find bugs early in the process, checking your results throughout (in conjunction with a filter!) will hopefully save you from lots of hassle.

Here’s the script:

# How to start and stop your code

# Step 1: Prints some odd numbers

for x in xrange(1, 20, 2):
print x

# Step 2: Pop up messsage that stops the script from running the next function until "enter"

raw_input("Press <enter> to continue")

# Step 3: Code continues on and prints some even numbers
for x in xrange(2, 20, 2):
print x

Here’s the video on how I did this:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *