I want to limit the size of a list in python 2.7 I have been trying to do it with a while loop but it doesn’t work
l=[]
i=raw_input()//this is the size of the list
count=0
while count<i:
l.append(raw_input())
count=count+1
The thing is that it does not finish the loop. I think this problem has an easy answer but I can’t find it.
Thanks in advance
I think the problem is here:
raw_input()returns a string, not an integer, so comparisons betweeniandcountdon’t make sense. [In Python 3, you’d get the error messageTypeError: unorderable types: int() < str(), which would have made things clear.] If you convertito an int, though:it should do what you expect. (We’ll ignore error handling etc. and possibly converting what you’re adding to
lif you need to.)Note though that it would be more Pythonic to write something like
Most of the time you shouldn’t need to manually keep track of indices by “+1”, so if you find yourself doing it there’s probably a better way.