Skip to content Skip to sidebar Skip to footer

How To Enter A List Into A Function? In Python

Write a function called “find_even_count” which allows the user to enter an arbitrary sequence of positive integers at the keyboard, and then prints the number of positive even

Solution 1:

Here you go my friend

input_list = raw_input('give me list of numbers: ')
print(filter(lambda x: x.isdigit() andnotint(x) % 2, input_list.split()))

will give you

(ocmg)brunsgaard@archbook /tmp> python pos.py
give me list of numbers: 1234 -2 -1 your mother 54
['2', '4', '54']

Also if you want a more simple solution go with

even = lambda l: len([x for x in l if not x % 2 and x > 0])

This will output

even([1, -2, 3, 4, 6, 5])
2

Because there are 2 even numbers

Solution 2:

deffind_even_count(x):
    even_count = 0for n in x:
            if n%2 ==0:
                    even_count+=1print even_count

Post a Comment for "How To Enter A List Into A Function? In Python"