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

  1. 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.
  1. 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!")
 Aiden 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>

  1. Create a program to pick five random names from a list of at least 15 names
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)
The selected names are:
Paulo
James
Ashkon
Max
Cam
  • You can with making a list of 15 names and then you create a new list(selected names) that contains 5 random names. Then it prints each name in the selected names list.
  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

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")
James,
You rolled 12 in total!
Ben,
You rolled 7 in total!
James wins
  • Starts by defining what a dice roll is like the possible outcomes. Then you input player 1 and 2 names and then you run the same section of code for each player that rolls 2 dice and adds the 2 numbers together. Then it compares player 1 to player 2 total score.

Blog/Final Thoughts

  • Learned what a library was and documentation
  • Furthered my knowledge on API's
  • Learned the different randoms and rads like random.choice, random.shuffle, radint, and radrange
  • Learned new ways to randomize lists
  • I feel I understood documentation pretty and the different randomizes
  • Creating the dice roll problem was a little hard
</div>