Playwright + Python · Lesson 2 of 37

0/37 done

Module 2: Conditional Statements & Loops

2. Flow Control, Retry Loops & Enumerate

15 minlesson

Your task

Implement find_index_of_target(items, target):

  1. Use enumerate(items) in a for loop.
  2. If item == target, return index.
  3. Return -1 if not found.

Write it in the editor, then press Check answer (Ctrl + Enter).

Flow Control in Automation

python
# Evaluating status codes:
status_code = 200
if status_code == 200:
    print("API Success")
elif status_code == 404:
    print("Endpoint not found")
else:
    print(f"Unexpected status: {status_code}")

# Retry loop pattern:
max_retries = 3
for attempt in range(1, max_retries + 1):
    print(f"Attempt {attempt} of {max_retries}")
    # poll element state

Key Takeaways & Walkthrough Summary

In automated tests, you often evaluate response status codes, retry failed assertions, and iterate through lists of table rows. In this lesson, you will master if, elif, else, while retry loops, and enumerate(). Let's dive into control flow!

Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why use enumerate() instead of range(len(items))?

enumerate() is Pythonic and yields both the 0-based index and the item directly without requiring manual list indexing.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip