0

I am trying to write a very simple program. I would take a list of numbers, and would check whether all the members of the list is evenly divided by a given integer. Here is the present situation of my code:

def evenlist(lst,y):
   print lst
   for i in range(len(lst)):
       print int(lst[i]) % y == 0
x = '2,5,6,8,10'
lst = x.split(',')
y = 2
if evenlist (lst,y):  #(?????)
# Here is the problem....
    print 'All are evenly divided by', y
else:
    print 'All are not evenly divided by', y

How can I say, if all of evenlist (lst,y) is true, print this.


Update:

Now my code is solved. Corrected code:

def evenlist(lst,y):
print lst
result = []
for i in range(len(lst)):
   result.append(int(lst[i]) % y == 0)
return result   
x = '2,4,6,8,10'
lst = x.split(',')
y = 2
if all (evenlist(lst,y)):
    print 'All are evenly divided by', y
else:
    print 'All are not evenly divided by', y
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sheikh Ahmad Shah
  • 281
  • 1
  • 4
  • 12

3 Answers3

4

Currently, your evenlist function does not return anything. It just prints whether all the numbers are even or not and then implicitly returns None, which is interpreted as False in the condition.

To test all the members in the list, use the all builtin function and return it's result:

def evenlist(lst,y):
    return all(int(x) % y == 0 for x in lst)

What this does: It generates a new list, in which each element is the result of the condition (True or False), and then tests whether all the values in that list are True (or otherwise "truthy"):

>>> lst = [2, 5, 6, 8, 10]
>>> [x % 2 == 0 for x in lst]
[True, False, True, True, True]
>>> all(x % 2 == 0 for x in lst)
False

Example:

>>> evenlist("2,5,6,8,10".split(','), 2)
False
>>> evenlist("2,12,6,8,10".split(','), 2)
True
tobias_k
  • 81,265
  • 12
  • 120
  • 179
1

Continuing on the path that you are on, you can turn evenlist() into a generator and then iterate through it with all():

def evenlist(lst,y):
   print lst
   for i in range(len(lst)):
       yield int(lst[i]) % y == 0

x = '2,5,6,8,10'
lst = x.split(',')
y = 2

if all(x for x in evenlist(lst,y)):
    print 'All are evenly divided by', y
else:
    print 'All are not evenly divided by', y
MrAlexBailey
  • 5,219
  • 19
  • 30
  • I don't know much about 'yield' command. If I use 'return' instead of 'yield', the code doesn't work. Can you please explain this yield feature. – Sheikh Ahmad Shah Jun 17 '15 at 18:40
  • `yield` turns the function into a generator, it's essentially an object that you can iterate through. `all()` then goes through the generator, which one by one yields true or false for each element of the list you pass to it. – MrAlexBailey Jun 17 '15 at 18:41
  • 2
    Also see [What does the yield keyword do in Python?](http://stackoverflow.com/q/231767/1639625) – tobias_k Jun 17 '15 at 18:45
1

hi you should check if they are even in def

def evenlist(lst,y):
 # print lst
x = 0
for i in range(len(lst)):
   x += int(lst[i]) % y
if x==0:
    return True
else:
    return False

x = '2,4,6,8,10'
lst = x.split(',')
y = 2
if evenlist (lst,y):
 print 'All are evenly divided by', y
else:
 print 'All are not evenly divided by', y
uelei
  • 36
  • 1
  • 4