How do you find the count of even numbers in a list?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 5
Reputation: Sohvkhan is an unknown quantity at this point 
Solved Threads: 0
Sohvkhan Sohvkhan is offline Offline
Newbie Poster

How do you find the count of even numbers in a list?

 
0
  #1
Feb 23rd, 2009
As of this moment I have been switching back and forth between various iterations of my first foray into Python programming software, in any case part of my assignment calls for finding the count of even and odd numbers in a set of values that the user inputs, here is the code below:


  1. smallest = 124301293123
  2. largest = 0
  3. location = 0
  4. loc_largest = 0
  5. loc_smallest = 0
  6.  
  7.  
  8. value = '-1'
  9. while True:
  10. location = location + 1
  11. num = input("enter number ")
  12. if num == "0": #check for end of input
  13. print "the largest is ", largest
  14. print "at location ", loc_largest
  15. print "the smallest is ", smallest
  16. print "at location ", loc_smallest
  17. print "the count of numbers is", len(num)
  18. li = []
  19. li.append(num(n))
  20. print min(li)
  21. x = l1[0]
  22. list(num%2 == 0)
  23. for l in l1:
  24. if x>l:
  25. x=l
  26. print "Smallest is", x
  27. print (num*len(num)) / sum
  28. print "average is", average(num)
  29. print "The count of even numbers is", len(num%2 == 1)
  30. print len(num%2 == 0)
  31. print len(num%2 == 1)
  32. sys.exit(0)
  33. else:
  34. if num > largest: #found new largest
  35. largest = num
  36. loc_largest = location
  37.  
  38. if num < smallest: #found new smallest
  39. smallest = num
  40. loc_smallest = location


Parts of the code are unworkable and represent attempts at guesswork and general tooling around to get the solution that I want. For finding the len and the len of even and odd I am at loss as the len returns only 1 and my attempt at finding the len of even numbers was mostly based around trying to use remainders as an indicator as to whether or not the input was even e.g print len(num%2 == 0). Any thoughts on the solution to my conundrum?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 55
Reputation: sfar_furqan is an unknown quantity at this point 
Solved Threads: 1
sfar_furqan's Avatar
sfar_furqan sfar_furqan is offline Offline
Junior Poster in Training

Re: How do you find the count of even numbers in a list?

 
0
  #2
Feb 23rd, 2009
here you go for odd and even values u can modify this code for largest and smallest.
[code]
  1. Even=[]
  2. Odd=[]
  3. def IsEven(a):
  4. even=False
  5. while not even:
  6. calculate=a%2
  7. if calculate==0:
  8. even=True
  9. return even
  10.  
  11. def find_Even():
  12. for val in range (1,50):
  13. found_Even=IsEven(val)
  14. if found_Even==True:
  15. Even.append(val)
  16. else:
  17. Odd.append(val)
  18.  
  19. def output():
  20. print "\t\t\t THE EVEN NUMBERS ARE\n"
  21. count=0
  22. while count<len(Even):
  23. print "%3i"%Even[count],"\t",
  24. count+=1
  25.  
  26. print"\n"
  27. print "\t\t\t THE ODD NUMBERS ARE\n"
  28. i=0
  29. while i<len(Odd):
  30. print "%3i"%Odd[i],"\t",
  31. i+=1
  32. find_Even()
  33. output()
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: How do you find the count of even numbers in a list?

 
2
  #3
Feb 23rd, 2009
there is a really quick way to get a list of even numbers:
  1. list_of_evens= [f for f in range(1,500) if f%2==0]
that should give all the even numbers from 1 to 500 in a list
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: How do you find the count of even numbers in a list?

 
0
  #4
Feb 23rd, 2009
You have the right idea. To add it to your program, add another if statement to the smallest & largest calcs using your num %2==0 idea. This code is not tested.
  1. else:
  2. if num > largest: #found new largest
  3. largest = num
  4. loc_largest = location
  5.  
  6. if num < smallest: #found new smallest
  7. smallest = num
  8. loc_smallest = location]
  9.  
  10. if num % 2 == 0: ## even
  11. even_list.append(num) # if you want a list of even numbers
  12. even_ctr += 1 # if you just want a total
  13. else:
  14. odd_list.append(num) # list of odd numbers
  15. odd_ctr += 1 # total
Also, initializing this way if fine
smallest = 124301293123
largest = 0
unless your largest number is less than zero. Another way to do it is to use the first number entered to initialize both fields, which is what will happen anyway as it will be larger than zero and less than 124301293123. That way, you don't have to guess how big to make "smallest" so it will be larger than the numbers entered, and vice versa for "largest".
Last edited by woooee; Feb 23rd, 2009 at 1:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: Lizapotter is an unknown quantity at this point 
Solved Threads: 2
Lizapotter Lizapotter is offline Offline
Newbie Poster

Re: How do you find the count of even numbers in a list?

 
0
  #5
Feb 24th, 2009
to find count of even number from a list of number, you can use the following control structure code:

i = 0
j=0
while i <= 10 :
if i % 2 == 0 :
j=j+i:
print str(i) + ' is an even number'
else :
print str(i) + ' is an odd number'
i += 1
print str(j) + 'is total count'
Last edited by Lizapotter; Feb 24th, 2009 at 2:08 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: How do you find the count of even numbers in a list?

 
0
  #6
Feb 24th, 2009
Originally Posted by Lizapotter View Post
to find count of even number from a list of number, you can use the following control structure code:
  1. i = 0
  2. j=0
  3. while i <= 10 :
  4. if i % 2 == 0 :
  5. j=j+i:
  6. print str(i) + ' is an even number'
  7. else :
  8. print str(i) + ' is an odd number'
  9. i += 1
  10. print str(j) + 'is total count'
@Lizapotter: To preserve indentation and get syntax highlighting (and also spare the forum-goer's eyes) please use code tags as such:
[noparse][code=python]
# Your code goes in here
[/code]
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: How do you find the count of even numbers in a list?

 
0
  #7
Feb 24th, 2009
A small function that will check if an integer is odd or even:
  1. def is_odd(n):
  2. """return True if n is an odd integer, else False"""
  3. if n & 1:
  4. return True
  5. else:
  6. return False
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1235 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC