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:

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?

Recommended Answers

All 6 Replies

here you go for odd and even values u can modify this code for largest and smallest.

[code=python]
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:

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

commented: Actually it's one to 499, but I agree that a list omprehension is the easiest route. +5

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.

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

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".

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'

@Lizapotter: To preserve indentation and get syntax highlighting (and also spare the forum-goer's eyes) please use code tags as such:

[code=python]
# Your code goes in here
[/code]

A small function that will check if an integer is odd or even:

def is_odd(n):
    """return True if n is an odd integer, else False"""
    if n & 1:
        return True
    else:
        return False
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.