Reflection:

In the lesson, I learned about software libraries, the use for existing code segments, APIs, and the importance of documentation for these APIs and software libraries. First off, I learned that it is very useful to use existing procedures in code, as this can make the creation of the program more efficient. Furthermore, it is important to accompany these procedures with the proper documentation, which explains what the procedure is doing and what the program is attempting to accomplish. Furthermore, I also learned more about the "random" module that is often used in Python. It can serve as a random number generator and a number shuffler as well, which is very cool.

Multiple Choice:

  1. What does random (a,b) function generate?

A. A random integer from a to b exclusive

B. A random integer from a to b inclusive

C. A random word from variable a to variable b exclusive

D. A random word from variable a to variable b inclusive

Option B is the correct answer, since the random function is used to generate a random number from a range a to b, and the random number can be the bounds of that range as well, therefore rendering it INCLUSIVE.

  1. What is x, y, and z in random.randrange(x, y, z)?

A. x = start, y = stop, z = step

B. x = start, y = step, z = stop

C. x = stop, y = start, z = step

D. x = step, y = start, z = stop

  1. Which of the following is NOT part of the random library?

A. random.item

B. random.random

C. random.shuffle

D. random.randint

Short Answer Questions:

  1. Libraries allow us to use already tested and functional code in our own procedures. This creates a more efficient programming process.

  2. This procedure goes through a series of steps. First of all, the random module is imported to be used in this procedure. Then, an input is provided for the user to input everyone's names. The "names" variable is then defined as everyone's names split with a comma in between. Then, there is a variable defined as "num_items", which measures the number of names inputted.

Then, the random module comes into place by selecting a random number in between 0 and the number of names - 1, inclusive. Then, in order to figure out the person who will pay the bill, the numbers corresponding to the names are randomized, and the name of the person that the code lands on is printed at the end.

Coding Challenges

import random 

namesList = ["Safin","Aarav","Boomin","G","Navan","Shaurya","Justin","Theo","James","Mihir","Aryan","Karthik","Rohan","Rohit","Zach"]

sample_namesList = random.sample(namesList, k=5) # here, k = number of items to select
print(f"you got these 5 random names: {sample_namesList}")
you got these 5 random names: ['Aarav', 'Shaurya', 'G', 'James', 'Rohan']
import random 

player1= random.randrange(1,6)
player1t2 = random.randrange(1,6)
print("Player 1 landed on a", player1, "and", player1t2, "adding up to:", player1+player1t2)

player2 = random.randrange(1,6)
player2t2 = random.randrange(1,6)
print("Player 2 landed on a", player2, "and", player2t2, "adding up to:", player2+player2t2)

player1score = player1 + player1t2
player2score = player2 + player2t2

if player1score > player2score:
    print("Player 1 is the winner.")
elif player1score == player2score:
    print("It's a tie.")
else: 
    print("Player 2 is the winner.")

Extra Credit Try

import random

directions = ["up", "down", "left", "right"]

start_move = random.choice(directions)
start_point = random.randint(1,25)
end_point = random.randint(1,25)

print("You are starting in this direction: " + start_move)
print("You will start at this block number: " + str(start_point))
print("Your mission if you choose to accept it is to make it to this block: " + str(end_point))
print("The obstacles are in the following areas: ")
i = 1
while i <= 6:
    obstacle = random.randint(1,25)
    print(str(obstacle))
    i += 1
    
You are starting in this direction: up
You will start at this block number: 7
Your mission if you choose to accept it is to make it to this block: 20
The obstacles are in the following areas: 
11
12
4
5
24
17

I wasn't really sure where to go from here, so this was my attempt.