Hacks Section 14-15
Summary of what I learned
- In this lesson I learned a lot about libraries which are ways of code that are previously written so users can reuse them. Also I learned about different random functions such as radrange and rad int. Finally I learned about documentation and how it can level up your code through the through explanation it gives.
Vocab
- Documentation: The what, how, or why of the code
- Libraries: Prewritten code or procedures that can be used to maximize efficiency
- API: Data stored in a database that can be accessed via code
Notes
- Documentation is understanding the purpose of a procedure when it gets too long -random.choice picks a random item from a list
- random.shuffle shuffles a the items in a list
- radint: this allows you to generate a random integer from a set range
- radrange:can be written like randrange(start, stop, step) where start is the minimum value, stop is the maximum value (like the randint function), and step is the incriment the values can be and its default value is 1
Hacks
(1) Reflection
Thorough notes or summary that reflects understanding of today's lesson.
(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. Explanation : This is becuase the random integers from a to be pick something within these parameters leaving it to be answer B
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 EXPLANATION : This is the answer as random rad range gives you a number from one of these sets so in this case 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 This isnt not a known function in random librabries
B. random.random
C. random.shuffle
D. random.randint
(3) Short Answer Questions
What is the advantage of using libraries? ANSWER: The advantage of using libraries is its a faster and more easier way to get code rather than having to write it and test it.
import random # importants random library
names_string = input("Give me everybody's names, seperated by a comma.")
# gets the names of "everybody" and then splits them into a list using the .split() method
names = names_string.split(",")
num_items = len(names)
# Finds the number of items through the length of the names list
random_choice = random.randint(0, num_items - 1)
# uses a random number from getting randint function
person_who_will_pay = names[random_choice]
# assigns whoever to pay with the random choice
print(f"{person_who_will_pay} is going to buy the meal today!")
# prints whats going to be displayed.
import random
lst = ["chris", "Dan," "Madi","mulchy", "Merua", "Juan", "Bran","Kimmy", "krishiv", 'qais', "caleb", "jhon", "bob", "tod", "sherly"]
for i in range(3): # tells to pick the 3 name
print(random.choice(lst)) # print these names from a list
import random
def roll_dice():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
return die1 + die2
num_players = 2
player1 = ["Krishiv"]
player2 = ["Jermey"]
for player in player1:
print(f"{player1[0]},")
krishivscore = roll_dice()
print(f"You rolled {krishivscore} in total!")
for player in player2:
print(f"{player2[0]},")
jermeyscore = roll_dice()
print(f"You rolled {jermeyscore} in total!")
if krishivscore > jermeyscore:
print("Krishiv Wins")
else:
print("Jermey wins")
# Explanation: First I took my two player which is krishiv and jermey, then I made sure to define them with the special rad int function to simulate rolling the dice. Then I made a loop displaying how the dice would be rolled as it prints a number the person rolled