Sections 14 & 15 Hacks
Question 1 - Notes
Documentation is vital for others or for yourself to understand your code later on. Over documenting is never hurtful but underdocumenting is. Libraries are collections of code and scripts that can provide faster solvents to problems. API's are also useful for using pre-determined data.
Randomization is useful for algorithms that rely on things of chance. "import random" is needed as it provides random scripting. Number ranges are also needed for setting boundaries. A couple things you can do are random shuffles and choices.
Give explanations for each questions
- random(a,b) generates
B) A random integer from a to b inclusive
random(a,b) does this due to the user setting a range from a to b. If you want it to differ, then you have to change the range.
- What is x, y, and z in random.randrange(x,y,z)?
A) x = start, y = stop, z = step
X is the number that starts it. Y is where it ends. Z is what step it is on
- Which of the following is not part of the random library
A) random.item
random.random, random.shuffle, and random.randint are all apart of the library are were in the lesson plan
- Libraries have a distinct advantage of having pre-written code that can provide efficiency at a linear level. This cuts out time that the user has to write repetitive code. Therefore the user can spend more time on the creative and work process then repetitive code writing.
import random
# using import random defines a series of functions and code that is critical for random processes. This is needed for and of the random uses in the python libraries.
names_string = input("Give me everybody's names, seperated by a comma.")
# What this does is ask for user input to define what names are used later on. After the user input is entered, it is stored as a string which then defines the name variable.
names = names_string.split(",")
# Defines the name variable with the user input that was called on earlier
num_items = len(names)
# Defines this variables as the length of how many names were entered.
random_choice = random.randint(0, num_items - 1)
# Defines the random choice variable as a random integer from 0 to the number of items-1.
person_who_will_pay = names[random_choice]
# What this does is take a random choice of one of the names in the stored name data. Then it defines that variable as a singular name.
print(f"{person_who_will_pay} is going to buy the meal today!")
# This is the final print statement that calls on the variable which is defined as one of the randomized names that the user inputted.
import random
names = ["Theo","Drew", "Brady", "Elliot", "Ty", "Tristan", "Bailey", "Avery", "Liam", "Addi", "Kiana", "Keira", "Ethan", "Jess", "Anna",]
i = 0
print("5 random names are")
while i < 5:
print(random.choice(names))
i += 1
import random
def playerOne():
oneFirstRoll = 0
oneFirstRoll = random.randrange(1,6)
oneSecondRoll = 0
oneSecondRoll = random.randrange(1,6)
oneScore = oneFirstRoll + oneSecondRoll
return oneScore
def playerTwo():
twoFirstRoll = 0
twoFirstRoll = random.randrange(1,6)
twoSecondRoll = 0
twoSecondRoll = random.randrange(1,6)
twoScore = twoFirstRoll + twoSecondRoll
return twoScore
playerOne()
print("Player one rolled",playerOne())
playerTwo()
print("Player two rolled",playerTwo())
import random
print("The five randomized mazes are")
x = 0
while x < 5:
grid = [['o' for _ in range(5)] for _ in range(5)]
num_obstacles = 12
for i in range(num_obstacles):
row = random.randint(0, 4)
col = random.randint(0, 4)
grid[row][col] = 'x'
start_row = random.randint(0, 4)
start_column = random.randint(0, 4)
grid[start_row][start_column] = 'S'
finish_row = random.randint(0, 4)
finish_column = random.randint(0, 4)
grid[finish_row][finish_column] = 'F'
for row in grid:
print(' '.join(row))
print("----------------------")
x = x + 1