(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
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.
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
Which of the following is NOT part of the random library?
A. random.item
B. random.random
C. random.shuffle
D. random.randint
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!")
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>
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>
</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)])
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!")