Some list issue

Hi there,

I was doing Python loops practice and when nearing the end, I got stuck in the below. I know it might seem a bit silly… but i was struggling to understand what exactly went wrong as I strictly followed the format in the hint.

Thanks in advance!
Jane

for i in range(hairstyles) is the problem, here. hairstyles is a list. range takes only integer arguments.

range( start_index, end_index (exclusive), stride )

Hi Roy,

Thanks so much for your prompt help!

I see that the hint here also refers a list in range but I agree with you said here re range function. So if I were to make this work, what shall I change in my coding.
image
Thanks,
Jane

Range is a form of iterable to which we give bounds, step and direction. If we give it another iterable, how are we to expect it to behave?

>>> for i in range([2, 4, 6, 8]):
	print (i)

	

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    for i in range([2, 4, 6, 8]):
TypeError: range() integer end argument expected, got list.
>>> 

To tell range to construct an iterable with the same number of elements, but in sequence, as the iterable we wish to iterate, we give it the length of the iterable.

for i in range(len(old_list)):

Brilliant- it worked! Thanks so much!

1 Like