(1) Reflection

Thorough notes or summary that reflects understanding of today's lesson.

In today's lesson I learned about libraries, documentation, and API's. Libraries allow a user to reference pre written code. Documentation is a process in which an individual explains code. API's (application program interfaces) specify how procedures in libraries should behave and be utilized. I was introduced to the random function and I learned of its application. For example we can use randrange, randint, and random to specify the random value being provided.

(2) Multiple Choice

  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.

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

    D. A random word from variable a to variable b 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

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

    A. random.item

    B. random.random

    C. random.shuffle

    D. random.randint

(3) Short Answer Questions

  1. What is the advantage of using libraries?

    • Libraries allow a user to simplify his or her code by referencing pre written code. This allows for greater efficiency and reliability.
  2. Write a thorough documentation of the following code.

import random 

names_string = input("Give me everybody's names, seperated by a comma.")
names = names_string.split(",")

num_items = len(names)

random_choice = random.randint(0, num_items - 1)

person_who_will_pay = names[random_choice]

print(f"{person_who_will_pay} is going to buy the meal today!")
 Kumesh is going to buy the meal today!

Documentation: The function takes an input of names and assigns each name an integer. Then the randint function randomly selects a number that is assigned to a specific name. Finally the name is printed along with the sentence.

(4) Coding Challenges!

REQUIRED: Create programs in python to complete the two task</p>

  1. Create a program to pick five random names from a list of at least 15 names
  2. Create a program to simulate a dice game where each player rolls two fair dice (6 sides); the player with the greater sum wins
</div> </div> </div>
import random

names = ["Dontavious", "Rohin", "Quandale", "Putin", "Shamly", "Babbar", "Jaria","Brogden","Tyrone","Miheer", "Obama", "Joe", "Kumesh", "Shindi","Prao" ]

for i in range(5):
    print(names[random.randint(0,14)])
Rohin
Joe
Babbar
Obama
Tyrone
import random 

p1dice1 = random.randrange(1,6)
p1dice2 = random.randrange(1,6)
print("Player 1 rolled a", p1dice1, "and", p1dice2, "for a sum of:", p1dice1+p1dice2)

p2dice1 = random.randrange(1,6)
p2dice2 = random.randrange(1,6)
print("Player 2 rolled a", p2dice1, "and", p2dice2, "for a sum of:", p2dice1+p2dice2)

p1sum = p1dice1 + p1dice2
p2sum = p2dice1 + p2dice2

if p1sum > p2sum:
    print("Player 1 wins!")
elif p1sum == p2sum:
    print("Roll again! You both rolled the same sum.")
else: 
    print("Yay! Player 2 wins!")
Player 1 rolled a 2 and 5 for a sum of: 7
Player 2 rolled a 1 and 2 for a sum of: 3
Player 1 wins!

I utilized a random library to simulate the rolling of dice. I set the variables to a random number between 1-6. Finally each variable is named according to the user and the output is summed together in order to compare the numbers.

</div>