Unit 3 Sections 5-7 Hacks
3.5
Practice Questions
- 2; a is assigned 19 and b was assigned 28 and 19 =/= 28
- 1; both score and avaerage are assigned as 99 and 99 ≤ 99
- 2; the type is dog and it has training
Hacks
Binary Practice
- Using psuedocode operators determine if the statements are true or false. The number type will be indicated in parentheses.
1. 90(D) = 1000(B)
- A. True
- B. False Answer: B. False; 1000(B) is the same as 8(D) and 90(D) ≠ 8(D)
2. 10(D) ≠ 0110(B)
- A. True
- B. False Answer: A. True; 10(D) is the same as 1010(B)
3. 56(D) ≥ 111000(B)
- A. True
- B. False Answer: B. False; 111000(B) = 224(D) and 26(D) < 224(D)
4. 99(D) < 1110011(B)
- A. True
- B. False Answer: A. True; 1110011(B) = 115(D) and 99(D) < 115(D)
Binary truth tables
Value 1 | Value 2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Value 1 | Value 2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Not | Value | Result |
---|---|---|
Not | 1 | 0 |
Not | 0 | 1 |
# Testing out relational operators
# Practice with these statements
print(20 > 20) # How can you change the operator to print a value of False?
## Since 20 is not greater than (>) 20 the statement would return as False
x = 30
y = 20
z = 10
print(x == y + z) # How can this return true by only manipulating the operator?
## The operater must be changed from > to == which would make the statement True
# Manipulate the variables x, y, and z to make the below statement return true
print(x > z)
## Since x is assigned 30 and z is assigned 10 to make the statement True == must be changed to >
3.6
Practice Questions
- 1; since rank is assigned "titan" the condition IF (rank = "titan") is met which would display "You like Clash of Clans a lot."
- 2; since class is larger than cookies the else portation is ran
AP Prep
1. What is displayed by this code?
- result <-- 75
- IF result < 80 { DISPLAY("Please schedule a retake.") }
- ELSE { DISPLAY("Nice job!") }
- Nice job!
- Display
- Please schedule a retake.
- 75
Answer: 3. Please schedule a retake; since the ressult is assigned as 75 and 75 < 80 the IF block is executed
2. How is an if statement different from an if-else statement.
- Extra words.
- An if statement will only go through a process if a condition is met. An if-else statement will go through code no matter the conditions.
- They are the exact same.
- An if statement will go through the entire code segment every single time and the if-else statement is always used in an algorithm, no matter the conditions.
Answer: 2
3. What would be most appropriate for this situation? Ben wants to check his bank account. If his car fuel is full, he will go to the bank. Otherwise, he will go home. If he goes to the bank, he will withdraw money only if his balance is above $1000.
- If statement
- If-else statement
Answer: 2; since he will go to the bank IF he has gas ELSE he will go home
4. What would be most appropriate for this situation? Luke wants to play basketball. If it is sunny outside he will go to the park to play basketball.
- If statement
- If-else statement
Answer: 1; There is only a condition for if and not one for else
animals = ["lion", "tiger", "wildebeest", "shark", "jellyfish", "blobfish", "raven"]
for i in animals:
if i == "shark": # What boolean value does this statement cause?
print("Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!")
elif i == "wildebeest":
print("This animal lives in the desert")
else:
print(i)
# Practice
# Using only one more if statement, alter the code to print out a statement saying if an animal lives in the desert, based on booleans
3.7 Hacks
Exercise 1
- Create dictionaries for multiple food items, with the listed specifications
- Chicken Alfredo, Meat: Chicken, Time to Prepare: 60 minutes
- Cheese Quesadilla, Meat: None, Time to Prepare: 10 minutes
- Beef Wellington, Meat: Beef, Time to Prepare: 150 minutes
- Used nested conditionals, determine which meal you can cook, given that a) you have no meat at home, and b) you only have 30 minutes to make the meal
chickenAlfredo = {
"meat": True,
"prepTime": 60,
"name": "Chicken Alfredo"
}
cheeseQuesadilla = {
"meat": False,
"prepTime": 10,
"name": "Cheese Quesadilla"
}
beefWellington = {
"meat": True,
"prepTime": 150,
"name": "Beef Wellington"
}
def cookMeal(dish):
if dish["prepTime"] >= 30 or dish["meat"] == True:
print("You can't cook a " + dish["name"])
else:
print("You can cook a " + dish["name"])
cookMeal(chickenAlfredo)
cookMeal(cheeseQuesadilla)
cookMeal(beefWellington)
Exercise 2
Make a flowchart(here is one we used) and write pseudocode for the following scenario.
- Mr. Yeung would like to grade live reviews.
- He wants to see if each student has at least 2 issues on their project. If they don't they receive a score of 2.0.
- If they have at least 2 issues, check that they have completed at least 5 of their scrumboard tasks.
- If they have completed 5 scrumboard tasks, give the student a 2.7. If they have not completed 5 scrumboard tasks, give them a score of 2.5. If they have completed more than 5 tasks, give them a score of 3.0.
- How much would a student with 3 issues and 1 complete scrumboard task receive?