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?
Since the user inputs is a string and you need integer values to define the range, you can typecast the input to be a integer value using int method.
>> l=int(raw_input('Enter Length: '))  # python 3: int(input('Enter Length: '))
>> for i in range (0,l):
>>    #Do something (like append to a list)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With