Homework Assignment for 3.14 and 3.15
Completing the Hacks given to us for Unit 3, Sections 14 and 15
- This Group: Emma, Sanika, Ishi, and Leonard
- Notes
- (1) Reflection
- (2) Multiple Choice
- (3) Short Answer Questions
- (4) Coding Challenges!
- EXTRA-CREDIT ATTEMPT
- EXTRA X 2
Notes
- 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
- Documentation for APIs/Libraries are necessary to gain proper understanding on how to use them
- We can use the code random.shuffle to help us to shuffle a list and randomize it and a library
- In CollegeBoard pseudo-code RANDOM(a, b) is used to generate a random integer value in the range from a to b
- In Python the random library can be imported and the min and max ranges can be set to any integer
- A general way to to write this is randint(start, stop) where start is the minimum value, stop is the maximum value
- This can help make a coin-flip, dice roll, and anything else which needs a range
Vocabulary
- Documentation: Text that explains the what, how, or why of your code
- Libraries: A collection of pre-written 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 each other
(1) Reflection
Thorough notes(shown above) or summary that reflects understanding of today's lesson.
Today's notes were all about Libraries and Variables. When talking about Libraries, API's were mentioned, which we have had some experience with already, and they are very useful in the Computer Science Field. They can help you a lot when you are trying to code something. API's are very useful for using, finding, and collecting pre-determined data. Another part of the lesson that was mentioned was about Documentation which is basically text that explains the logistics behind your code. It's like justifying your work in Math, you basically use Documentation to ensure you and other programmers know what your code is for, and if you need help on it, people can easily understand your intentions. There is nothing wrong with adding a lot of information with your code, it might not be necessary, but not adding enough documentation can cause problems for other programmers and yourself. If you look back on a segment of code you made a while ago, you might read the documentation and think the code has a different purpose than it originally had. Randomization was also mentioned, and this is useful especially in lists and algorithms that rely on things of chance like a coin flip or rolling a dice, these are examples of random occurrences. I have learned that randomization is used a lot in Python because it is so important in lists, and many games and websites actually use it.
Did both notes and a summary
(2) 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.
C. A random word from variable a to variable b exclusive.
D. A random word from variable a to variable b inclusive.
My Answer: B
Explanation: This is because that you want to make sure that you include a and b within the randomized list, you can not exclude it because it is a range that includes it. This means that it can't be exclusive. Next, we know that a and b are integers and that they are the index of a list that are connected to a list, so since it is a range, it will only work for integers in a list, also since we are using random, that is why a random integer from a to b is being chosen.
- 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
C. x = stop, y = start, z = step
D. x = step, y = start, z = stop
My Answer: A
Explanation: In this function, the parameters are start, stop, and step. First, off I know that x has to be start, and this is because it is a range, and you want to start off with the beginning of the list, and then y has to be stop because it has to know when to stop. This tells the range to stop, and the list will stop there, and this means that z has to be the step, which tells the code what the increment of the values should be. Step should go last because the code needs to know the range of the list before the step.
- Which of the following is NOT part of the random library?
A. random.item
B. random.random
C. random.shuffle
D. random.randint
My Answer: A
Explanation: I have seen random.randint be used before especially in pseudocode and also in the examples and practice problems that we did for this lesson, it just generates a random integer. Additionally, I have also seen random.shuffle used during this lesson to simulate random chances like a number generator or flipping a coin. Then, random.random is a thing, and all it does is generate a float between 0 and 1. Item() is it's own parameter and does not need to be randomized and it can just act as it's own. This means that random.item is not an actual thing, so my answer is Option A.
(3) Short Answer Questions
-
What is the advantage of using libraries? Libraries are so beneficial, and in fact I have been told this by many people. One of my good friends, Raunak, who likes to program a lot says that he uses API's, which are libraries with a lot of information, and he uses them frequently because it is so easy. I have heard from so many different people that using API's is a very easy method for making sure that we can use it and display code very easily. Another example of this is when Kris, a panelist, came to our school and talked about how Twitter used REST API until about 2 years ago. API's are convenient as well because they provide procedures and pre-written code to maximize the efficiency of programmers.
-
Write a thorough documentation of the following code.
Shown in Documentation with the Code Cell with Comments Some Comments may not be able to be seen
import random # this just pulls the random library and helps the code and lists to become randomized when using the .random parameter
names_string = input("Give me everybody's names, seperated by a comma.") # This will ask the user to input a name, and then this is later stored as a string
names = names_string.split(",") # This defines the names variable and splits the string based on the input the user had entered in the previous code
num_items = len(names) # This defines a new variable as the length of many names were entered
random_choice = random.randint(0, num_items - 1) # This will use the random library to choose a random number from 0 to the number of items-1
person_who_will_pay = names[random_choice] # This takes one of the items or indexes in the user inputted list and then assigns that to this variable
print(f"{person_who_will_pay} is going to buy the meal today!") # This prints the last sentence saying who is going to pay the bill by using the previous randomly selected variable
(4) Coding Challenges!
REQUIRED: Create programs in python to complete the two task</p>
No need to create a graphical user interface (output the course in graphical form); that would be too demanding. You can use store your randomizations in variables to be printed, after which you can sketch one instance of your output using any means (hand drawn, google drawings, etc.) to attach to your submission as an image. Your illustration can differ graphically from CollegeBoard's illustrations as long as the robot, its direction, the wall squares, and the goal square are fairly self-evident (ie you can make the goal a present or food instead of a gray square) You must randomize the following: The latter three must be mutually exclusive (on different squares). This prevents you from generating a course where the robot is on the goal, on a wall square, or the goal is in a wall square
</div>
</div>
</div>
import random # imports the random library
names = ["Emma", "Sanika", "Ishi", "Leonard", "Lionel", "Francis", "Aron", "Mili", "Edwin", "Cristiano", "Lia", "Melvin", "Ansel", "Evlin", "Misha"] # List of 15 Names
names_number = 1 # Variable set to one to tell the code when to stop the while loop
print("5 Random Names:") # Print this before the code randomly selects names so it will look nice
while names_number <= 5: # A While Loop so that the code can randomly choose 5 Names
print(random.choice(names)) # Takes random items from the list and prints them
names_number +=1 # This adds to the variable names_numbers, so once it reaches 5, the while loop will stop
import random # imports the random library
player1 = input("Who is Player 1?")
player2 = input("Who is Player 2?")
player1_score = 0 # Variable for the score of the first player
player2_score = 0 # Variable for the score of the second player
def roll_game(): # This is a function that is defined with more code inside of it and will be run later
player1_score = random.randint(1,6) + random.randint(1,6) # The score of Player 1 is being determined by choosing 2 random numbers between 1 and 6
player2_score = random.randint(1,6) + random.randint(1,6) # The score of Player 2 is being determined by choosing 2 random numbers between 1 and 6
if player1_score > player2_score: # This will happen if the score of Player 1 is greater than Player 2
print("The Winner is " + player1 + " with a score of " + str(player1_score))
if player1_score < player2_score: # This will happen if the score of Player 2 is greater than Player 1
print("The Winner is " + player2 + " with a score of " + str(player2_score))
if player1_score == player2_score: # This will happen if the score of both players are the same
print("This game resulted in a tie, " + player1 + " got " + str(player1_score) + " and " + player2 + " got " + str(player2_score) + ". Play again to see who will win!")
roll_game()
EXTRA-CREDIT ATTEMPT
import random
direction = ["north", "south", "west", "east"]
itemDirection = random.choices(direction)
print(f"Initial direction of the robot: {itemDirection}")
goalPosition = []
for i in range(1):
r=random.randrange(1,10)
goalPosition.append(r)