Reputation: 23
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 integers user enters. The sequence of numbers is unknown at the beginning. User may enter negative number to show the end of the sequence. For example: If user enters the sequence, 1, 3, 5, 23, 56, 14, 68, 25, 12, -1 then your function needs to print “4 even numbers”, since there are 4 even numbers in the sequence. Hint: Use while loop
here is my code
find_even_count(x):
i = x
even_count = 0
while x> 0:
if i%2 ==0:
even_count+=1
print even_count
I keep getting the error code
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
find_even_count(2,22,224,24,-1)
File "/Users/rayhow/Desktop/assignment2_Q4.py", line 5, in find_even_count
if i%2 ==0:
TypeError: unsupported operand type(s) for %: 'tuple' and 'int'
Why am I getting this error message?
Upvotes: 0
Views: 286
Reputation: 5168
Here you go my friend
input_list = raw_input('give me list of numbers: ')
print(filter(lambda x: x.isdigit() and not int(x) % 2, input_list.split()))
will give you
(ocmg)brunsgaard@archbook /tmp> python pos.py
give me list of numbers: 1 2 3 4 -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
Upvotes: 1
Reputation: 2921
def find_even_count(x):
even_count = 0
for n in x:
if n%2 ==0:
even_count+=1
print even_count
Upvotes: 0