# Python function only iterates over the first item

Solution for Python function only iterates over the first item
is Given Below:

I wrote a function to convert a group of numbers to string but it only executes the first item.
I want to iterate over the entire list instead.

List = [ 0, 50, 80, 12, 5, 8, 19, 90, 40, 8, 7, 9, 18, 27, 30, 45]

``````def number_group(x):

item = [ ]
for i in x:
if i in range(0, 30):
print (' group 10 and 30')
elif i in range(30, 60):
print ('group 30 and 60')
elif i in range (60, 90):
print ('group 60 and 90')
elif i in range (90, 120):
print ('group 90 and 120')
else:
return ('NAN')
item.append((i))
return
``````

Your code works perfectly for me. The loop will immediately stop if you get a number beyond 119. Are you aware of that?

By the way, doing “in range” is a terrible way to handle that. Instead:

``````def number_group(x):

item = [ ]
for i in x:
if 0 <= i < 30:
print (' group 10 and 30')
elif i < 60:
print ('group 30 and 60')
elif i < 90:
print ('group 60 and 90')
elif i < 120:
print ('group 90 and 120')
else:
return ('NAN')
item.append(i)
return
``````

Are you trying to do this :

``````def number_group(x):
item = [ ]
for i in x:
if 0 <= i < 30:
print ('group 10 and 30')
elif i < 60:
print ('group 30 and 60')
elif i < 90:
print ('group 60 and 90')
elif i < 120:
print ('group 90 and 120')
else:
print ('Not in any group')
item.append(str(i))
return item

print(number_group([ 0, 50, 80, 12, 5, 8, 19, 90, 40, 8, 7, 9, 18, 27, 30, 45]))
"""
group 10 and 30
group 30 and 60
group 60 and 90
group 10 and 30
group 10 and 30
group 10 and 30
group 10 and 30
group 90 and 120
group 30 and 60
group 10 and 30
group 10 and 30
group 10 and 30
group 10 and 30
group 10 and 30
group 30 and 60
group 30 and 60
['0', '50', '80', '12', '5', '8', '19', '90', '40', '8', '7', '9', '18', '27', '30', '45']
"""
``````

Printing, in which group your given number is located and returning the list of string of those number.