Notes

3.14

  • Software libraries contain procedures used in creating novel programs.
  • Existing code segments can be derived from internal or external sources: Libraries, Previously written code.
  • Libraries simplify complex programs.
  • APIs (application program interfaces) specify how procedures in libraries should behave and be utilized.
  • Documentation for APIs/Libraries are necessary to gain proper understanding into how to use them.
Vocabulary
Term Definition
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 eachother

3.15

Syntax:

In collegeboard psudo-code RANDOM(a, b) is used to generate a random integer value in the range from a to b. For example if RANDOM(1, 5) is written 1, 2, 3, 4, and 5 have the same chance to show up. In Python the random library can be imported and the min and max ranges can be set to any integer.

Randint:

A general way to to write this is randint(start, stop) where start is the minimum value, stop is the maximum value. And this allows you to generate a random integer from a set range and this can help you create code for things like a coin-flip, dice roll, and anything else which needs an inclusive range.

Randrange:

In general randrange 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. If start = 0 and step = 5 all the values that can by outputed are 0 and multiples of 5. And if start = 2 and step = 3 the output would be 2 and a multiple of 3 plus 2.

Reflection

I learned about the random function and what documentation, libraries, and application programming interfaces are. The random function has three other functions that are syntax, randint, randrange and they all have different purposes and are way more efficient than coding it from the beginning with just python. This lesson was very good and I learned a lot!

Hacks

Multiple Choice

  1. B because the random functions chooses between the range of numbers and it also includes the numbers.
  2. A because x is the number that starts, y is the number that stops, z is the step this is because that is how they are formatted.
  3. A because random:random, random:shuffle, and random:randit exist and are functions in the library, but random:item doesn't exist.

Short Answer

  1. It is more efficient and speeds up the process of being able to look through the library rather than creating code that has be made for a word that already exists. For example, the randint function is where start is the minimum value and stop is the maximum value so I do not have to make a new piece of code to use that function.
import random 

names_string = input("Give me everybody's names, seperated by a comma.") #The function asks for names to input and is the piece of code that makes a list. 
names = names_string.split(",") #This functions helps seperate the names one by one

num_items = len(names) #makes the names into a list

random_choice = random.randint(0, num_items - 1) #randint is the the function where start is the minimum which is 0 and the stop is maximum which is num_items - 1. 

person_who_will_pay = names[random_choice] #This chooses the random name

print(f"{person_who_will_pay} is going to buy the meal today!") #This is the output.

Coding Challenges

  1. Create a program to pick five random names from a list of at least 15 names
import random 
names = ["John", "Jake", "Jack", "Alex", "Ryan", "Jaiden", "Jayden", "Jaden", "Ethan", "Emily", "Ella", "Ellie", "Hailey", "Kaylee", "Bob"]

i = 0
print("5 random names are")
while i < 5:
    print(random.choice(names))
    i += 1
5 random names are
Jayden
Hailey
John
Ellie
Alex
  1. Create a program to simulate a dice game where each player rolls two fair dice (6 sides); the player with the greater sum wins
import random

oneFirstRoll = random.randrange(1,6)
oneSecondRoll = random.randrange(1,6)
oneScore = oneFirstRoll + oneSecondRoll

twoFirstRoll = random.randrange(1,6)
twoSecondRoll = random.randrange(1,6)
twoScore = twoFirstRoll + twoSecondRoll

if oneScore > twoScore:
    print ("Player one won with a score of",playerOne())
    print ("Player two lost with a score of",playerTwo())

else:
    print ("Player two won with a score of",playerTwo())
    print ("Player one lost with a score of",playerOne())
Player one won with a score of 6
Player two lost with a score of 3

Extra Credit

import random 

direction = ["up", "down", "left", "right"]
itemDirection = random.choices(direction)
print(f"Initial direction of the robot: {itemDirection}") 

startNumber = random.randrange(1,25)
print(f"Where the robot started: {startNumber}")

goalPosition = []
for i in range(1):
   r=random.randrange(1,25)
   if r != startNumber:
      goalPosition.append(r)
print(f"The goal position is: {goalPosition}")

obstacles = []
for i in range(12):
   r=random.randint(1,25)
   if r != startNumber:
      obstacles.append(r)
   if r != goalPosition:
      obstacles.append(r)
   if r not in obstacles:
      obstacles.append(r)
print(f"The obstacles are at: {obstacles}")

## I tried to make it so the program wouldn't repeat the same number twice and it worked but it doubles the number.
Initial direction of the robot: ['down']
Where the robot started: 11
The goal position is: [16]
The goal position is: [21, 21, 7, 7, 13, 13, 5, 5, 8, 8, 11, 15, 15, 10, 10, 17, 17, 16, 16, 3, 3, 19, 19]