Program 4: “Modeling uncertainty with the random module” The objective of this problem is two-fold: 1) to learn how to use the web to find python-specific information about functions and methods, and 2) to use the random module to model uncertainty. There are many functions and methods available in the python distribution that are not included in the standard library. One example is the “sqrt()” function included in the math module which returns the square root of its argument. To access functions of a module not included in the standard library, that module must first be imported using an import statement. This statement should be the first statement in the file, above all of the function definitions. In this program you will import and use the random module which enables drawing a random value from a range, container, or a distribution. Go to this page and read about how to use the randrange() and choice() methods of the random module. The links for these methods will take you to pages that describe their usage, syntax including parameters, and give examples of using the method that you can run and experiment with. After you are familiar with these, then use those methods in developing the below program. The program will have two parts, each using a loop and one each for using the randrange() and choice() methods and a loop.
Certainly! Below is a Python program that fulfills the requirements you've outlined. The program uses the random
module to generate random numbers and letters based on user input.
import random
def program_header():
print("Welcome to the Random Number and Letter Generator!")
print("This program will help you generate random numbers from a specified range and random letters from a given string.")
def main():
# Call the program header
program_header()
# Part 1: Using randrange()
print("\n--- Random Number Generator ---")
lower_limit = int(input("Enter the lower limit of the range: "))
upper_limit = int(input("Enter the upper limit of the range: "))
while True:
random_number = random.randrange(lower_limit, upper_limit)
print(f"Your random number {random_number} is pulled from the range {lower_limit} to {upper_limit}.")
# Check for sentinel value to stop
continue_prompt = input("Do you want to generate another random number? (yes/no): ").strip().lower()
if continue_prompt != 'yes':
break
# Part 2: Using choice()
print("\n--- Random Letter Generator ---")
user_string = input("Enter a string to pull random letters from: ")
while True:
random_letter = random.choice(user_string)
print(f"Your random letter '{random_letter}' is pulled from the string '{user_string}'.")
# Check for sentinel value to stop
continue_prompt = input("Do you want to generate another random letter? (yes/no): ").strip().lower()
if continue_prompt != 'yes':
break
if __name__ == "__main__":
main()
Importing the Random Module: The program starts by importing the random
module, which provides functions to generate random numbers and select random elements.
Program Header: The program_header
function prints a welcome message and explains the purpose of the program.
Main Function: The main
function contains two parts:
random.randrange()
and prints it along with the range.random.choice()
and prints it.Execution: The program is executed by calling the main()
function when the script is run.