---
id: 68da6138d70960c197ec5ef3
title: Step 11
challengeType: 20
dashedName: step-11
---

# --description--

Now you need to handle adding closing parentheses. The key rule is that you can only add a closing parenthesis if it maintains balance, meaning there must be more opening parentheses used than closing parentheses.

Still within the `else` block, add second `if` statement to check if `closes_used < opens_used`.

If this condition is true, append another new tuple to the `queue`: `(current + ')', opens_used, closes_used + 1)`. This represents the state after adding a closing parenthesis.

# --hints--

You should have a second `if` statement inside your `else` block.

```js
({ test: () => runPython(`
import ast
assert isinstance(_Node(_code).find_function("gen_parentheses").find_whiles()[0].find_bodies()[0].find_ifs()[0].tree.orelse[1], ast.If)
`) })
```

Your second nested `if` statement should check if `closes_used < opens_used`.

```js
({ test: () => runPython(`
assert _Node(_Node(_code).find_function("gen_parentheses").find_whiles()[0].find_bodies()[0].find_ifs()[0].tree.orelse[1].test).is_equivalent("closes_used < opens_used")
`) })
```

You should append `(current + ')', opens_used, closes_used + 1)` to the queue when the condition is true.

```js
({ test: () => runPython(`
assert _Node(_Node(_code).find_function("gen_parentheses").find_whiles()[0].find_bodies()[0].find_ifs()[0].tree.orelse[1]).has_call("queue.append((current + ')', opens_used, closes_used + 1))")
`) })
```

# --seed--

## --seed-contents--

```py
def gen_parentheses(pairs):
    if not isinstance(pairs, int):
        return 'The number of pairs should be an integer'
    if pairs < 1:
        return 'The number of pairs should be at least 1'
    
    queue = [('', 0, 0)]
    result = []
    while queue:
        print(queue)
        current, opens_used, closes_used = queue.pop(0)
        if len(current) == 2 * pairs:
            result.append(current)
        else:
            if opens_used < pairs:
                queue.append((current + '(', opens_used + 1, closes_used))
--fcc-editable-region--
            
--fcc-editable-region--

    return result

print(gen_parentheses(1))
```
