Ken Villarruel
Ken Villarruel

Reputation: 21

Python: Skip an Iteration in a For Loop if a condition is true

I have written a Python script that reads in values from an Excel worksheet and iterates through the rows.

However, I would like the program to skip a row if a certain condition is met.

I have an xml file that has a value that determines the run type. In the python code, I have written an If / Else block to convert the value to a number (see below)

# If / Else to convert test_run_type text to a value
if test_run_type == "Regression":
    test_run_type_value = '1'
elif test_run_type == "Smoke":
    test_run_type_value = '2'
elif test_run_type == "Sanity":
    test_run_type_value = '3'

Next, I have the for loop which iterates through the rows (see code below)

# Open Test Scenario Workbook; Instantiate worksheet object
wb = xlrd.open_workbook(os.path.join(test_case_directory, Product + '.xlsx'))
sh = wb.sheet_by_index(0)

## Begin For Loop to iterate through Test Scenarios
        i = 1
        rows = sh.nrows
        empty_cell = False
        for x in range(1, sh.nrows):

            cell_val = sh.cell(i, 0).value
            if cell_val == '':
                # If Cell Value is empty, set empty_cell to True
                empty_cell = True
            else:
                # If Cell Value is NOT empty, set empty_cell to False
                empty_cell = False


            regression_check = sh.cell_value(i, 3)
            smoke_check = sh.cell_value(i, 4)
            sanity_check = sh.cell_value(i, 5)

            # If / Else Section to check if a test needs to be run
            #### Program is running ALL rows & NOT skipping rows

            if test_run_type_value == 3 and sanity_check == "False":
                    continue
            else:
                pass

            if test_run_type_value == 2 and smoke_check == "False":
                    continue
            else:
                pass

            if test_run_type_value == 1 and regression_check == "False":
                    continue
            else:
                pass

The Problem: My expectation is that the program will skip a row if one of the following scenarios occur in a row.

However, the program is NOT skipping any of the rows.

I took a screenshot of the Excel sheet.

enter image description here

Based on the worksheet (see attached image), the program should skip the first row when the test_run_type_value is "3" but it is not. The program iterates through all the rows (even when the test_run_type_value is 1, 2 or 3)

Thanks in Advance

Ken

Upvotes: 0

Views: 9275

Answers (1)

poke
poke

Reputation: 388213

test_run_type_value = '1'

This sets test_run_type_value to the string value '1'.

if test_run_type_value == 1 …

This compares test_run_type_value to the integer value 1.

So you are basically comparing strings and integers here, and those are never equal:

>>> '1' == 1
False

So decide on whether you want to use a string, or an integer. E.g. if you assign 1, it should work fine:

test_run_type_value = 1 # no quotes => int!

Btw. you do not need to do this:

else:
    pass

Simply don’t include an else, and nothing will be done if the condition is not true:

if test_run_type_value == 3 and sanity_check == "False":
    continue
if test_run_type_value == 2 and smoke_check == "False":
    continue
if test_run_type_value == 1 and regression_check == "False":
    continue

Upvotes: -1

Related Questions