How do I use variables in a loop with range()? (Python)

Viewed 45333

I've been searching for quite a while, and I can't seem to find the answer to this. I want to know if you can use variables when using the range() function. For example, I can't get this to work:

l=raw_input('Enter Length.')
#Let's say I enter 9.
l=9
for i in range (0,l):
    #Do something (like append to a list)

Python tells me I cannot use variables when using the range function. Can anybody help me?

3 Answers

Try this code:

x = int(input("the number you want"))
for i in range(x):
Related