200k views
5 votes
Write a program that imports the Python random module to generate random integers in the range of 1 through 1000 using the following assignment: currentNumber = random.randint (1,1000) The program should include this statement in a loop to select up 100 of these random numbers. As part of this program, you also need to write and use an isEven function to help you count how many of those numbers were even and how many were odd. The isEven function should be defined to accept a single integer number and return True if the number is even, False if odd. Make sure the user has the option of repeated execution by including an outer loop. This program should produce output similar to the following: >>> \%Run Test1_Program.py Out of 100 random numbers, 51 were odd, and 49 were even. Would you like to run the program again (Y/N):y Out of 100 random numbers, 50 were odd, and 50 were even. Would you like to run the program again (Y/N):y Out of 100 random numbers, 52 were odd, and 48 were even. Would you like to run the program again (Y/N):y Out of 100 random numbers, 53 were odd, and 47 were even. Would you like to run the program again (Y/N):n

User Cdpautsch
by
8.6k points

1 Answer

2 votes

Final answer:

A Python program can use the random.randint function to generate 100 random numbers and an isEven function to determine their evenness or oddness. The user can then decide to repeat this process based on their input.

Step-by-step explanation:

The task is to write a Python program that uses the random module to generate up to 100 random integers in the range of 1 through 1000. To accomplish this, we can define an isEven function that will determine the parity of the numbers. Further, we'll include loops to allow the program to run multiple times based on the user's input. Below is an example of how such a Python program could look:

import random

def isEven(number):
return number % 2 == 0

while True:
even_count = 0
odd_count = 0
for _ in range(100):
currentNumber = random.randint(1, 1000)
if isEven(currentNumber):
even_count += 1
else:
odd_count += 1
print(f'Out of 100 random numbers, {odd_count} were odd, and {even_count} were even.')
run_again = input('Would you like to run the program again (Y/N):')
if run_again.lower() != 'y':
break

The program defines the isEven function, then enters an outer loop that will iterate until the user decides not to run the program again. Inside the loop, it keeps count of even and odd numbers out of 100 randomly generated ones, using a for loop and the random.randint function. After printing out the counts, it prompts the user for input to either continue or exit the loop.

User Black Frog
by
8.3k points