Reflection/Summary

A library is a collection of pre-written code that can be used by a programmer to help them more easily accomplish a task. This is particularly useful for tasks that are common or repetitive, as the programmer can simply use the code from the library rather than having to write it from scratch each time. Using a library can save time and effort for the programmer, as they do not have to reinvent the wheel each time they need to perform a common task. However, it is important to carefully consider which library to use, as different libraries may have different strengths and weaknesses, and using the wrong library for a task can lead to problems.

One example of a library is the random library, which contains functions for generating random values. These random values can be used in a variety of ways, such as in simulations or games to add an element of chance, or in machine learning algorithms to help with training. Using a library can save time and effort for the programmer, as they do not have to reinvent the wheel each time they need to perform a common task. However, it is important to carefully consider which library to use, as different libraries may have different strengths and weaknesses, and using the wrong library for a task can lead to problems.

MCs

  • 1: B, random is inclusive by definition
  • 2: A, this is the order they would be in
  • 3: A, all the other ones where taught but random:item does not exsist # Short Answer Qs
  • 1: The main advantage of using libraries is that they provide pre-written code that you can use in your own programs. This can save a lot of time and effort, because you don't have to write the code yourself. Additionally, because the code in a library has been used and tested by many other people, it is generally more reliable and efficient than code that you might write yourself. Using libraries can also make it easier to develop programs that are more complex and sophisticated, because the code in a library can provide a foundation that you can build upon.
  • 2: The code is a simple program that takes a list of names from the user and randomly selects one of the names to be the person who will pay for a meal.

First, the code imports the random module, which provides functions for generating random numbers.

Next, the code prompts the user for a list of names, which are entered as a string with the names separated by commas. The input string is then split using the split() method, which creates a list of the individual names.

The code then finds the number of names in the list using the len() function, and uses this value to generate a random integer using the random.randint() function. This random integer is used as an index to select one of the names from the list.

Finally, the selected name is printed to the screen.

Code

  • 1:
import random

# List of names
names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Heidi", "Isabel", "Jack", "Karen", "Liam", "Mike", "Nancy", "Olivia", "Pat", "Queenie", "Richard", "Samantha", "Trevor", "Ursula", "Victoria", "Wanda", "Xander", "Yolanda", "Zach"]

# Pick five random names from the list
random_names = random.sample(names, 5)

# Print the selected names
print("The five random names are:")
for name in random_names:
    print(name)
The five random names are:
Charlie
Nancy
Wanda
Liam
Heidi
  • 2:
import random

num_sides = 6

def roll_die():
  return random.randint(1, num_sides)

def roll_dice():
  roll1 = roll_die()
  roll2 = roll_die()
  return roll1 + roll2

def play_game(player1, player2):
  sum1 = roll_dice()
  sum2 = roll_dice()

  if sum1 > sum2:
    return player1
  elif sum2 > sum1:
    return player2
  else:
    return "Tie"

player1_wins = 0
player2_wins = 0
ties = 0

for i in range(100):
  result = play_game("Player 1", "Player 2")
  if result == "Player 1":
    player1_wins += 1
  elif result == "Player 2":
    player2_wins += 1
  else:
    ties += 1

print(f"Player 1 wins: {player1_wins}")
print(f"Player 2 wins: {player2_wins}")
print(f"Ties: {ties}")
Player 1 wins: 46
Player 2 wins: 48
Ties: 6

Ec

import random

# initial direction of the robot
directions = ["up", "down", "left", "right"]
initial_direction = random.choice(directions)

# initial position of the robot
initial_row = random.randint(0, 4)
initial_col = random.randint(0, 4)

# goal position
goal_row = random.randint(0, 4)
goal_col = random.randint(0, 4)

# positions of wall/obstacle squares
# we generate at least 12 such positions, but we need to make sure they are different
# from the initial position and the goal position
wall_positions = []
while len(wall_positions) < 12:
    row = random.randint(0, 4)
    col = random.randint(0, 4)
    if (row, col) != (initial_row, initial_col) and (row, col) != (goal_row, goal_col):
        wall_positions.append((row, col))

course = [[" " for _ in range(5)] for _ in range(5)]

# set the initial position of the robot
course[initial_row][initial_col] = "R"

# set the goal position
course[goal_row][goal_col] = "G"

# set the positions of the wall/obstacle squares
for row, col in wall_positions:
    course[row][col] = "X"

# print out the course
for row in course:
    print(" ".join(row))
X   X   X
      X  
  X X X  
X     X  
R G     X