Lessons 3.14-3.15 hacks
Reflection
Vocab
- Documentation: Text that explains the what, how, or why of your code.
- Libraries: A collection of prewritten code or procedures that coders can use to maximize their efficiency.
- Application Programming Interface: A type of software through several computers are able to communicate information amongst each other.
Notes
- Libraries contain procedures that make programming easier.
- procedures are especially good for reusing segments of code by calling the procedure.
- random procedure: Random(a,b) provides a random number in the range [a, b]
Hacks
Multiple Choice
- B) a random integer between 'a' and 'b' inclusive
- A) x = start, y = stop, z = step
- A) random.item() does not exist
Short Answer
- Libraries are convenient because they provide procedures and prewritten code to maximize the efficiency of programmers
- This procedure takes a list of strings and returns who will buy the meal today
import random
# takes user input, puts it into a list of names
names_string = input("Give me everybody's names, seperated by a comma.")
names = names_string.split(",")
num_items = len(names)
# uses random to choose a random number
random_choice = random.randint(0, num_items - 1)
# associates random number with a name
person_who_will_pay = names[random_choice]
# prints name
print(f"{person_who_will_pay} is going to buy the meal today!")
import random
NAMES = ["Aiden", "Brayden", "Bradley", "Harambe", "Charles", "BigChungus", "Chad", "Brett", "Dorian", "Ryan", "Varalu", "Rohin", "Advay", "SmallChungus"]
rand_list = []
def randomPeople(names, new_names):
count = 1
while count <= 5:
number = random.randint(0, len(names) - 1)
new_names.append(names[number])
count += 1
return new_names
print(randomPeople(NAMES, rand_list))
import random
score1 = 0
score2 = 0
def greatestRoll():
score1 = random.randint(1, 6) + random.randint(1, 6)
score2 = random.randint(1, 6) + random.randint(1, 6)
if score1 > score2:
print("Player 1 won with a score of " + str(score1) + " points!")
if score1 < score2:
print("Player 2 won with a score of " + str(score2) + " points!")
if score1 == score2:
print("Both players tied with " + str(score1) + " points!")
greatestRoll()