Important Vocab/Notes
- Documentation Text is text explains the what, how, or why of your code.
- Libraries is a collection of prewritten code or procedures that coders can use to maximize their efficiency
- Application Programming Interface (API) is a type of software through several computers are able to communicate information amongst eachother
- Software libraries contain procedures used in creating novel programs.
- 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
(1) Reflection
Thorough notes or summary that reflects understanding of today's lesson.
(2) Multiple Choice
1. What does the random(a,b) function generate?
1
A. A random integer from a to be exclusive
B. A random integer from a to b inclusive.
- includes both a and b and any values in between
C. A random word from variable a to variable b exclusive.
D. A random word from variable a to variable b inclusive.
2. What is x, y, and z in random.randrange(x, y, z)?
A. x = start, y = stop, z = step
- the order is start, stop, step
B. x = start, y = step, z = stop
C. x = stop, y = start, z = step
D. x = step, y = start, z = stop
3. Which of the following is NOT part of the random library?
A. random.item
- process of elimantion leaves this as the answer
B. random.random
C. random.shuffle
D. random.randint
(3) Short Answer Questions
- What is the advantage of using libraries?
- An advantage of using libraries is it allows you to reuse code that has already been created, which allows you to save time. A person can simply call that library in order to get the information they need.
- 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!")
- The code starts by having the user put in a set of name and then the code you len(names) to find the amount of people in the list. After the code picks a random value in the list and uses the corresponding name. They then have the information they gathered displayed by saying who is going to pay for the next meal.
(4) Coding Challenges!
REQUIRED: Create programs in python to complete the two task</p>
</div>
</div>
</div>
import random
names = ["James", "Ethan", "Charlie", "Kai", "Max", "Aiden", "Yasha", "Brayden", "Paulo", "Cam", "Ashkon", "Gabe", "Will", "Nick", "Will"]
selected_names = random.sample(names, 5)
print("The selected names are:")
for name in selected_names:
print(name)
import random
def roll_dice():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
return die1 + die2
num_players = 2
player1 = ["James"]
player2 = ["Ben"]
for player in player1:
print(f"{player1[0]},")
jamesscore = roll_dice()
print(f"You rolled {jamesscore} in total!")
for player in player2:
print(f"{player2[0]},")
benscore = roll_dice()
print(f"You rolled {benscore} in total!")
if jamesscore > benscore:
print("James wins")
else:
print("Ben wins")