Reflection / Notes
- libraries contain procedures used in creating novel programs.Existing code segments can be derived from internal or external sources: Libraries, Previously written code. Libraries simplify complex programs.APIs specify how procedures in libraries should behave and be utilized. Documentation for APIs/Libraries are necessary to gain proper understanding into how to use them.
1. What does the random(a,b) function generate?
A. A random integer from a to be exclusive
B. A random integer from a to b inclusive. = Answer
C. A random word from variable a to variable b exclusive.
D. A random word from variable a to variable b inclusive.
- The random(a,b) function generates a random integer from a to b inclusive. This means that the function will return a random whole number that is greater than or equal to a and less than or equal to
2. What is x, y, and z in random.randrange(x, y, z)?
A. x = start, y = stop, z = step = Answer
B. x = start, y = step, z = stop
C. x = stop, y = start, z = step
D. x = step, y = start, z = stop
- In the random.randrange(x, y, z) function, x represents the start of the range, y represents the end of the range, and z represents the step size. This means that the function will return a random integer from x to y, inclusive, with a step size of z.
3. Which of the following is NOT part of the random library?
A. random.item = Answer
B. random.random
C. random.shuffle
D. random.randint
- The random library is a collection of functions and methods for generating random numbers and other random data in Python. Some of the functions and methods included in the random library are random.item, random.random, random.shuffle, and random.randint.
4
- What is the advantage of using libraries?
- Reuse of code: Libraries allow you to reuse existing code, which can save you time and effort when working on a new project. This means you don’t have to reinvent the wheel every time you need to perform a common task, such as generating a random number or sorting a list. Improved organization: Libraries can help you organize your code, making it easier to maintain and modify. By using libraries, you can break your code into smaller, more manageable pieces, which can make it easier to understand and debug. Consistency: Libraries can help ensure that your code is consistent and follows best practices. By using a well-established library, you can be confident that the code you are using is reliable and has been tested and refined by others.
- Write a thorough documentation of the following code.
- the code begins by importing the random module, which provides functions for generating random numbers and data. Next, the input function is used to prompt the user to enter a list of names. The user’s input is stored in a string called names_string The split method is then used to split names_string into a list of individual names, using the comma character as a delimiter. This list is stored in a variable called names. The len function is used to determine the number of items in the names list, and this value is stored in the variable num_items. The random.randint function is then used to generate a random integer between 0 and num_items - 1. This number is stored in the variable random_choice. The selected name is retrieved from the names list using the random_choice index, and stored in the variable person_who_will_pay.
5
- Create a program to pick five random names from a list of at least 15 names
import random
names = [“jerry”, “Bob”, “Charlie”, “David”, “Emily”, “Frank”, “Gina”, “Hannah”, “Ivan”, “Jenny”, “Karen”, “Liam”, “Megan”, “Nina”, “Owen”]
selected_names = random.sample(names, 5)
print(“The selected names are:”) for name in selected_names: print(name)
- The code does work, I just can’t put in notebook because My notebooks are having problems uploading. You can try if you want.
- Create a program to simulate a dice game where each player rolls two fair dice (6 sides); the player with the greater sum wins
import random
player1_dice1 = random.randint(1, 6) player1_dice2 = random.randint(1, 6) player1_sum = player1_dice1 + player1_dice2
player2_dice1 = random.randint(1, 6) player2_dice2 = random.randint(1, 6) player2_sum = player2_dice1 + player2_dice2
if player1_sum > player2_sum: print(“Player 1 wins!”) elif player2_sum > player1_sum: print(“Player 2 wins!”) else: print(“It’s a tie!”)
- The code does work, I just can’t put in notebook because My notebooks are having problems uploading. You can try if you want.