# Using a variable inside range()

**URL:** <https://community.hubspot.com/t/using-a-variable-inside-range/3567>\
**Category:** CMS Development\
**Created:** [October 13, 2017, 2:45pm UTC](https://community.hubspot.com/t/using-a-variable-inside-range/3567 "2017-10-13T14:45:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MvdLanden](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mvdlanden/32/22307_2.png) [@MvdLanden](https://community.hubspot.com/u/MvdLanden)\
**Post date:** [October 13, 2017, 2:45pm UTC](https://community.hubspot.com/t/using-a-variable-inside-range/3567/1 "2017-10-13T14:45:38Z")

</div>

Hi all,

I’m trying to create a custom module which enables me to use an editable text field to modify a variable to use in an options variable in order to make repeatable blocks.

I have a text field named **widget.repeats** which has a default value of 5, but could be anything. I’m storing this by using this variable:

`{% set repeats = widget.repeats %}`

Next I have another variable which I’m going to use to loop through:

`{% set my_options = range(1, repeats) %} // Not working, should output range(1, 5)``{% set my_options = range(1, {{repeats}}) %} // Not working, should output range(1, 5)`

However these variables throw an error. How am I supposed to make this work? When I get this to work I could do something like:

```auto
{% for options in my_options %} {% if loop.index <= widget.repeats %} // Repeat this 
 {% endif %}
{% endfor %}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Jsum](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jsum/32/20405_2.png) [@Jsum](https://community.hubspot.com/u/Jsum)\
**Post date:** [October 15, 2017, 1:01pm UTC](https://community.hubspot.com/t/using-a-variable-inside-range/3567/2 "2017-10-15T13:01:15Z")

</div>

@MvdLanden

I didn’t have an issue getting this to work:

```auto
{% set test2 = [1,2,3,4,5,6,8,9,0] %}

{% set test3 = 9 %}

{% set range1 = range(2, test3, 2) %}

{% for items in range1 %}

 "boom"

{% endfor %}

```

I used test3 to simulate a choice from my test2 list. I set test3 as the stop for the range the for loop started at position 2, stopped at position 9, and stepped by 2 positions.

Your issue is most likely that because you are using a text input to capture the desired number, the number is coming in as string not an int. filter it as an int:

```auto
{% text "my_text" value='25', export_to_template_context=True %} 
{{ widget_data.my_text.value|int + 28 }}

```

equals 53

---

<div class="post-metadata">

**Author:** ![MvdLanden](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mvdlanden/32/22307_2.png) [@MvdLanden](https://community.hubspot.com/u/MvdLanden)\
**Post date:** [October 17, 2017, 7:56am UTC](https://community.hubspot.com/t/using-a-variable-inside-range/3567/3 "2017-10-17T07:56:53Z")

</div>

Adding the int filter did the trick. Big thanks!
