Hello, everyone. I am taking a beginning python class and was wondering if anyone could help me out? We are writing a program that generates 100 random numbers (between 1 and 1000), and keeps a count of how many of those random numbers are even and how many are odd. We are to use a for loop and a Boolean function, is_even, which may not include any calls to input or print. This is what I have so far for my is_even function:

def is_even(number):
    #  Determines if the number is odd or even.         
    if (number % 2) == 0:
         status = True
    else:
         status = False
    # Returns status as True or False
    return status`

This is what I have for may main function:

def main():
    # explain program
    print ('This program will generate 100 random numbers between 1 and 1000.')

    # Executes random numbers in the range 1 to 100
    number = random.randint(100)
    # Calls is_even function
    is_even(number)
    # 
    for count 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)

I am having some issues getting the whole thing to work:

import random

def main():
    # explain program
    print ('This program will generate 100 random numbers between 1 and 1000.')

    # Executes random numbers in the range 1 to 100
    number = random.randint(100)
    # Calls is_even function
    is_even(number)
    # 
    for count 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) 


def is_even(number):
    #  Determines if the number is odd or even.         
    if (number % 2) == 0:
        status = True
    else:
        status = False
    # Returns status as True or False
    return status

main()

I know I probabaly need some variables at this point:
count
number
is_even
is_odd

Any assistance is appreciated.

I recommend putting the def is_even() before def main(). I cant answer at the moment (at school) may fix it later.

your positioning of the randomint was wrong (should be in loop) along with calling is_even function

also, randint should be randint([startvalue], [endValue])
I changed max value to 1000 aswell as requested.

CodingCabbage

`import random
def main():
   # explain program
   print ('This program will generate 100 random numbers between 1 and 1000.')


    for count in range(1, 100):
        number = random.randint(0,1000)
        is_even(number)
        if is_even (number):
            status = True
            print ('The positive number count is:', number)
        else:
            status = False
            print ('The odd number count is: ', number) 
def is_even(number):
#Determines if number is even      
   if (number % 2) == 0:
      status = True
   else:
      status = False
   #Returns status as True or False
   return status
main()`

One of the many ways to do this ...

import random

def is_even(number):
    #  Determines if the number is odd or even.         
    if (number % 2) == 0:
         status = True
    else:
         status = False
    # Returns status as True or False
    return status


even = 0
odd = 0
# Loop through 100 unique random numbers with values from 1 to 1000
for n in random.sample(range(1, 1001), 100):
    if is_even(n):
        # Add 1 to even count
        even += 1
    else:
        odd += 1


# Show result
print("Of 100 random numbers")
print("{} are even".format(even))
print("{} are odd".format(odd))

I do not think the original poster's numbers are supposed to be unique so better use random.randint, but notice that both ends of range are inclusive so one must be odd, another even to give equal chance for even/odd.

This is a compressed version,if you see me post in link you see more how it works in part.

>>> from collections import Counter
>>> import random
>>> Counter(map(lambda x : 'even' if x % 2 == 0 else 'odd', (random.randint(0,1000) for i in range(100))))
Counter({'even': 58, 'odd': 42})

The unique numbers are picked at random. So it doesn't matter as far as randomness is concerned. Python uses the Mersenne Twister as the core pseudo random generator for all methods in module random.

You could even get away with this ...

import random

even = 0
for k in range(100):
    if random.choice(('even', 'odd')) == 'even':
        even += 1

# Show result
print("Of 100 random numbers")
print("{} are even".format(even))
print("{} are odd".format(100 - even))

... and achieve the same randomness.

Fun this odd/even stuff,one with ternary operator.
Did like the clean look of this one.

>>> from collections import Counter
>>> from random import sample
>>> Counter("Odd" if i % 2 else "Even" for i in sample(range(0, 1000), 100))
Counter({'Even': 56, 'Odd': 44})

Add some evaluations ...

from collections import Counter
from random import sample
import numpy as np

# create a list of the number of evens
even_list = []
for k in range(100):
    count = Counter("Odd" if i % 2 else "Even" for i in sample(range(0, 1000), 100))
    even_list.append(count["Even"])

# now evaluate ...
print('Average of even_list = {:.3f}'.format(np.mean(even_list)))
print('Standard Deviation of even_list = {:.3f}'.format(np.std(even_list)))

''' possible result ...
Average of even_list = 50.120
Standard Deviation of even_list = 4.670
'''
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.