Reflection

This lesson was has taught me new ways I can improve my coding skills. I can use libraries to make coding a lot more simpl and use stuff that has already been developed by others. Documentation makes my code readable. Programs use documentation to outline usage for users. The random library can be used in python which can be very handy in programs. Most programs such as videogames use randomization.


Notes

  • Libraries and prewritten code can make writing algorithms easier

  • Software libraries have procedures that are used in creating novel programs

  • APIs(application program interfaces) explain how procedures in libraries should behave and be utilized

  • Documentation makes it much easier to learn how to use libraries/APIs

  • Existing code segments can be derived from existing sources

  • random.randint(a, b): generates a random number


Multiple Choice

What does the random(a,b) function generate?

A. A random integer from a to be exclusive

B. A random integer from a to b inclusive. </p>

C. A random word from variable a to variable b exclusive.

D. A random word from variable a to variable b inclusive.

Explanation: B has the integers listed


What is x, y, and z in random.randrange(x, y, z)?

A. x = start, y = stop, z = step

B. x = start, y = step, z = stop </p>

C. x = stop, y = start, z = step

D. x = step, y = start, z = stop


Which of the following is NOT part of the random library?

A. random.item </p>

B. random.random

C. random.shuffle

D. random.randint

Explanation: The other options are specific functions


Short Answer Questions

What is the advantage of using libraries? We can access code that as already been developed in order to make coding something such as an algorithm less difficult.


Write a thorough documentation of the following code.

</div> </div> </div>
import random 

# implements list of names
names_string = input("Give me everybody's names, seperated by a comma.")
names = names_string.split(",")

num_items = len(names)

# Chooses random number
random_choice = random.randint(0, num_items - 1)

# puts together name and number
person_who_will_pay = names[random_choice]

# prints name
print(f"{person_who_will_pay} is going to buy the meal today!")
 Jamili is going to buy the meal today!
import random
score1 = 0
score2 = 0

def RandomNumberGame():
    score1 = random.randint(1, 10)
    score2 = random.randint(1, 10) 
    if score1 > score2:
        print("Player 1 won with a score of " + str(score1) + " bananas")
    if score1 < score2:
        print("Player 2 won with a score of " + str(score2) + " bananas")
    if score1 == score2:
        print("Player 1 and Player 2 have tied. Both have " + str (score1) + " bananas")

RandomNumberGame()
RandomNumberGame()
Player 2 won with a score of 5 bananas
Player 2 won with a score of 10 bananas
import random
names = ["Qais", "Jamili", "Jamilee", "Bob", "Georgy", "Michael" ,] 
i = 0

print("5 random names:")
while i < 5:
    print(random.choice(names))
    i += 1
5 random names:
Bob
Georgy
Jamilee
Qais
Michael

EXTRA CREDIT

Tried my best but I don't think I did it right.

startEnd()
import random


width = 5
height = 5
obstacles = 10


maze = [[0 for i in range(width)] for j in range(height)]


for i in range(obstacles):
    x = random.randint(0, width - 1)
    y = random.randint(0, height - 1)
    maze[x][y] = 'x' 


startEnd()

for row in maze:
    print(' '.join(str(cell) for cell in row))
0 0 0 x x
x x 0 0 0
0 x 0 0 0
0 x 0 0 0
0 0 0 x x
</div>