I am trying to generate a program using the builtin Random and generate an algorithm that determines whether the number is odd or even. I need to write a program that generates 100 random numbers, determine if its even or odd and finally count the number of even and odd. I am new to Python.

This is what I have thus far, but nothin is working. Could someone give me a push in the right direction?? Thank you so much.

import random


def main():
    # explain program
    intro()
    # get a random number
    number = random.randint(1,100)    
    # generate Boonlean variables
    is_even(number)
    while True:
        status = True
        for is_even in range(1,100):
            number = random.randint(1,100)
            print ("The positive number count is: ",number)
    else:
        status = False
        for is_even in range (1,100):
            number = random.randint(1,100)
            print ("The odd number count is: ", number)
    input("press enter to continue.") # required

def is_even(number):
    if (number % 2) == 0:
        status = True
    else:
        status = False
    return status

This is what I now have but I need to generate the total count for each.
total odd
total even

What would be the best way?

import random
def main():
    # explain program
    intro()
    number = random.randint(1,100)
    is_even(number)
    for number in range(1,100):
        if is_even (number):
            status = True
            print ("The positive number count is: ",number)
        else:
            status = False
            print ("The odd number count is: ", number) 

    input("press enter to continue.") # required

def intro():
    print ("This program will generate 100 random numbers.")
    print ("Once complete it will display total count of odd & even numbers.")
    print ("******************* Let's Begin! *********************")

def is_even(number):
    if (number % 2) == 0:
        status = True
    else:
        status = False
    return status

One hint:
print(random.sample(range(1000), 100))

This will show you a list of 100 unique random integers with values from 0 to 999.

Edited 3 Years Ago by HiHe

Thank you HiHe! This is what I did to show total even and total odd which does show total of each but they also want the program to "keep a count of how many of those random numbers are odd and even"
So I am guessing my structure isn't quite what they mean't.

I think they are looking for more of
total odds = total
total evens = total

*** hitting head on wall ***

def main():
    total= 0
    # explain program
    intro()
    # generate random numbers
    number = random.randint(1,100)
    # 
    is_even(number)
    # generate total even numbers
    even = [number for number in range(1,100)if number %2 ==0]
    # generate total odd numbers
    odd = [number for number in range (1,100) if number%2 ==1]
    # for loop to display even and odd numbers
    for number in range(1, 100):
        if is_even (number):
            status = True
            print ("The positive number is: ",number)
        else:
            status = False
            print ("The odd number is: ", number)
    print ("Total even numbers are: ", even)
    print ("Total odd numbers are: ", odd)
    input("press enter to continue.") # required

Some hint and look at differnt ways to count.
For counting this is a standar way.

import random

n = 100
even = 0
for item in range(n):
    if random.randint(1,n) % 2 == 0:
        even += 1

print('Even numer count is {}\nOdd number count is {}'.format(even, n - even))

"""Output-->
Even numer count is 53
Odd number count is 47
"""

So here i just count and dont't show numbers,if you need to show numbers use "else" and lists you append number to.

Your list comprehension stuff in last post is wrong.
A quick demo.

>>> lst = [random.randint(1,10) for i in range(10)]
>>> even = [i for i in lst if i % 2 == 0]
>>> odd = [i for i in lst if i % 2 == 1]
>>> even
[4, 10, 10, 2, 8, 8]
>>> odd
[5, 5, 5, 7]
>>> len(even)
6
>>> len(odd)
4

In first post you have a is_even helper function,this is a good way.
So here almost same function i use name to make it clearer.

def odd_even(n):
    if n % 2 == 0:
        return 'even'
    return 'odd'

Use function.

    lst = [random.randint(1,10) for i in range(10)]
    >>> map(odd_even, lst)
    ['even', 'even', 'odd', 'even', 'odd', 'odd', 'odd', 'even', 'odd']
    >>> # Or list comprehension
    >>> [odd_even(i) for i in lst]
    ['even', 'even', 'odd', 'even', 'odd', 'odd', 'odd', 'even', 'odd']

So here it show odd/even next step can be to count them.
So here i counting using a dictionary(standar way) or more fancy colletions.Counter.

my_dict = {}
odd_even_lst = ['even', 'even', 'odd', 'even', 'odd', 'odd', 'odd', 'even', 'odd']
for c in odd_even_lst:
    my_dict[c] = my_dict.get(c, 0) + 1

print(my_dict) # {'even': 4, 'odd': 5}

--

>>> from collections import Counter
>>> odd_even_lst = ['even', 'even', 'odd', 'even', 'odd', 'odd', 'odd', 'even', 'odd']
>>> Counter(odd_even_lst)
Counter({'odd': 5, 'even': 4})
>>> Counter(odd_even_lst).most_common(1)
[('odd', 5)]

Edited 3 Years Ago by snippsat

Thank you so much for clarifying. Doing this class online and the book only provides some examples. Its helpful to see other examples as well. I am going to play around with program more using your example and see if I amc alculating correctly.

If it fits you, you can do everything in single line.

It is only one way, and what is best depends on your use/experience.

import random
print('{odd} odd out of 100.'.format(odd=sum(random.randint(0,99)& 1 for count in range(100))))

Take care of the fact that random.randint includes values at both endpoints of the range.

Another hint ...

is_even = lambda x: x % 2 == 0
is_odd = lambda x: x & 1 == 1
# test
print(is_even(3))  # False
print(is_even(8))  # True
print(is_odd(12))  # False
print(is_odd(7))   # True
This question has already been answered. Start a new discussion instead.