| | |
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:
Solved Threads: 0
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:
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?
Python Syntax (Toggle Plain Text)
smallest = 124301293123 largest = 0 location = 0 loc_largest = 0 loc_smallest = 0 value = '-1' while True: location = location + 1 num = input("enter number ") if num == "0": #check for end of input print "the largest is ", largest print "at location ", loc_largest print "the smallest is ", smallest print "at location ", loc_smallest print "the count of numbers is", len(num) li = [] li.append(num(n)) print min(li) x = l1[0] list(num%2 == 0) for l in l1: if x>l: x=l print "Smallest is", x print (num*len(num)) / sum print "average is", average(num) print "The count of even numbers is", len(num%2 == 1) print len(num%2 == 0) print len(num%2 == 1) sys.exit(0) else: if num > largest: #found new largest largest = num loc_largest = location if num < smallest: #found new smallest smallest = num 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?
here you go for odd and even values u can modify this code for largest and smallest.
[code]
[code]
python Syntax (Toggle Plain Text)
Even=[] Odd=[] def IsEven(a): even=False while not even: calculate=a%2 if calculate==0: even=True return even def find_Even(): for val in range (1,50): found_Even=IsEven(val) if found_Even==True: Even.append(val) else: Odd.append(val) def output(): print "\t\t\t THE EVEN NUMBERS ARE\n" count=0 while count<len(Even): print "%3i"%Even[count],"\t", count+=1 print"\n" print "\t\t\t THE ODD NUMBERS ARE\n" i=0 while i<len(Odd): print "%3i"%Odd[i],"\t", i+=1 find_Even() output()
there is a really quick way to get a list of even numbers:
that should give all the even numbers from 1 to 500 in a list
python Syntax (Toggle Plain Text)
list_of_evens= [f for f in range(1,500) if f%2==0]
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Dec 2006
Posts: 1,065
Reputation:
Solved Threads: 299
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. 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".
Python Syntax (Toggle Plain Text)
else: if num > largest: #found new largest largest = num loc_largest = location if num < smallest: #found new smallest smallest = num loc_smallest = location] if num % 2 == 0: ## even even_list.append(num) # if you want a list of even numbers even_ctr += 1 # if you just want a total else: odd_list.append(num) # list of odd numbers odd_ctr += 1 # total
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.
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 2
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'
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.
•
•
•
•
to find count of even number from a list of number, you can use the following control structure code:
python Syntax (Toggle Plain Text)
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'
[noparse][code=python]
# Your code goes in here
[/code]
A small function that will check if an integer is odd or even:
python Syntax (Toggle Plain Text)
def is_odd(n): """return True if n is an odd integer, else False""" if n & 1: return True else: return False
No one died when Clinton lied.
![]() |
Similar Threads
- A little problem (C++)
- linked list problem in my program (C)
- Help for comparing strings and copying strings please. (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- beginners help please (Python)
- What in the world is the problem? (Java)
- Insertion Sort and Number Occurence (C++)
- Homework Help. (C++)
- Sorted linked list help (C++)
- floating point (C++)
Other Threads in the Python Forum
- Previous Thread: HELP! functions
- Next Thread: how to list the subdirrectories in a folder?
Views: 1235 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
anti assignment avogadro beginner bluetooth code convert count csv decimals def dictionary dynamic dynamically enter examples excel file float format frange ftp function gnu gui heads homework import input jaunty java lapse leftmouse line lines linux list lists loop microcontroller module mouse multiple newb number numbers output parsing path pointer port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect scrolledtext slicenotation software sqlite ssh stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape windows word wx.wizard wxpython






