For hacks, make a copy of this notebook and answer the questions or complete the code, as described in comments. Additionally, blog about any missed questions, or what you learned from this lesson.

3.5 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)

1000 Binary = 0 + 0 + 0 + 8 = 8 Decimal

Answer: B. False

2. 10(D) ≠ 0110(B)

0110 Binary = 0 + 2 + 4 + 0 = 6 Decimal ≠ 10 Decimal

Answer: A. True

3. 56(D) ≥ 111000(B)

111000 Binary = 0 + 0 + 0 + 8 + 16 + 32 = 56 Decimal ≥ 56 Decimal

Answer: A. True

3. 99(D) < 1110011(B)

1110011 Binary = 1 + 2 + 0 + 0 + 16 + 32 + 64 = 115 Decimal

99 Decimal < 115 Decimal

Decimal: A. True

Now, complete the binary truth tables

AND Operator
Value 1 Value 2 Result
1 1 1
1 0 0
0 1 0
0 0 0
OR Operator
Value 1 Value 2 Result
1 1 1
1 0 1
0 1 1
0 0 0
Not operator
Not Value Result
Not 1 0
Not 0 1

Python Practice

# Practice with these statements

print(20 == 20) # How can you change the operator to print a value of False?

# Here's a simple way to change the value to false:

print(20 > 20)

x = 30
y = 20
z = 10
print(x > y + z) # How can this return true by only manipulating the operator?

# The following change makes the statement true:
print(x > y - z)

# Manipulate the variables x, y, and z to make the below statement return true
x = x - z
z = y

print(x == z)
True
False
False
True
True

3.6 Hacks

AP Prep

1. What is displayed by this code?

  • result <-- 75
  • IF result < 80 { DISPLAY("Please schedule a retake.") }
  • ELSE { DISPLAY("Nice job!") }
  1. Nice job!
  2. Display
  3. Please schedule a retake.
  4. 75

Answer: 3. Please Schedule a Retake

Result was set to 75. Since that value is less than 80, the if condition is satisfied. Therefore, its associated command, which prints "Please schedule a retake" to output, is executed.

2. How is an if statement different from an if-else statement.

  1. Extra words.
  2. 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.
  3. They are the exact same.
  4. 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.

If statements and if-else statements are distinct, and their differences are not merely in what words are used.

Both if statements and if-else statements will execute commands if a condition is fulfilled.

In case of the condition not being met, the if statement does nothing, while the if-else statement executes commands associated with the else.

Therefore, the answer is 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.

Note that if the first condition (having a full car) is not fulfilled, a procedure is still executed (namely Ben goes home). Therefore, an if-else statement is procedurally appropriate.

Answer: 2. If-else statement

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.

Note that if the condition (it being sunny outside) is not fulfilled, the procedural response is not specified. This is similar to an if statement, which does nothing if the condition is not met.

Answer: 1. If statement

Using Python

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!")
    else:
        print(i)

print("This is information about the habitats of these animals:")
for j in animals:
    if (j == "lion") or (j == "raven"):
        print("The " + j + " is a desert animal")
    else:
        print("The " + j + " is not a desert animal")


# 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
lion
tiger
wildebeest
Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!
jellyfish
blobfish
raven
This is information about the habitats of these animals:
The lion is a desert animal
The tiger is not a desert animal
The wildebeest is not a desert animal
The shark is not a desert animal
The jellyfish is not a desert animal
The blobfish is not a desert animal
The raven is a desert animal

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
Menu = [
    {"Name": "Chicken Alfredo",
    "Meat": "Chicken",
    "PrepTime": 60},
    {"Name": "Cheese Quesadilla",
    "Meat": "None",
    "PrepTime": 10},
    {"Name": "Beef Wellington",
    "Meat": "Beef",
    "PrepTime": 150},
]

for entry in Menu:
    if entry["Meat"] == "None":
        if entry["PrepTime"] < 30:
            print("You have sufficient resources to make " + entry["Name"])
    else:
        print("You do not have sufficient resources to make " + entry["Name"])
You do not have sufficient resources to make Chicken Alfredo
You have sufficient resources to make Cheese Quesadilla
You do not have sufficient resources to make Beef Wellington
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb Cell 11 in <cell line: 29>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=26'>27</a> lst.pop()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=27'>28</a> lst.append('x')
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=28'>29</a> lst.insert(lst[0])
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=30'>31</a> print(lst.pop())
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=31'>32</a> print(lst)

TypeError: insert expected 2 arguments, got 1

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?

Anyways, my flowchart is on the issue. Apparently my website hates me so I'll have to paste an image

Anyways, the student with 3 issues fulfills the first if condition (has at least two issues), so the number of scrum tasks completed is checked. Since 1 is less than the minimum of 5 scrum tasks, this student gets a 2.5.

List = [['lizards', 'snakes', 'salamanders'], ['sharks', 'whales', 'fish'], ['lions', 'tigers', 'pumas']]

for i in List:
    for j in i:
        print(j)
lizards
snakes
salamanders
sharks
whales
fish
lions
tigers
pumas
List = [1, 2, 3, 4, 5]

def hrar(i):
    if i == 5:
        i = 0
    if i < len(List):
        temp = List[i]
        print(temp)
    hi = input("Input quit to stop the code. This is case-sensitive")
    if (hi == "quit"):
            return
    hrar(i + 1)

hrar(0)
    
1
2
3
List = [1, 2, 3, 4, 5]

hi = "hi"
i = 0
while hi != "quit":
    if i < len(List):
        temp = List[i]
        print(temp)
        i = i + 1
    if i == 5:
        i = 0
    hi = input("Input quit to stop the code. This is case-sensitive")
1
2
3
4
5
1
2
3
List = [1, 2, 3, 4, 5]

hi = "hi"

for i in range(999999999999999):
    mod = i % 5
    i = mod
    if i < len(List):
        temp = List[i]
        print(temp)
    hi = input("Input quit to stop the code. This is case-sensitive")
    if (hi == "quit"):
            break
    
1
2
111
4
5
1
2
111
4
5
1
2
111
4
5
1
2
111
list = [1, 2, 3, 4, 5]

#Defines while loop
i = len(list) - 1 
while i > -1: #Ensures the first entry (where i = 0) is printed
        record = list[i]
        print(record)
        i -= -1 #Goes to previous entry (rather than next entry)
5
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb Cell 18 in <cell line: 5>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X23sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> i = len(list) - 1 
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X23sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> while i > -1: #Ensures the first entry (where i = 0) is printed
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X23sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a>         record = list[i]
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X23sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a>         print(record)
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X23sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a>         i -= -1

IndexError: list index out of range
print("What did you roll on the dice?")
diceRoll = int(input())
if diceRoll >= 4:
    print("Nice roll!")
else:
    if diceRoll >= 2:
        print("Meh... You can do better")
    else:
        print("That was not a great roll!")
What did you roll on the dice?
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb Cell 19 in <cell line: 2>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X24sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> print("What did you roll on the dice?")
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X24sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a> diceRoll = int(input())
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X24sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> if diceRoll >= 4:
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/anonymous/computer-project/FastPage/_notebooks/2022-12-05-3.5-3.7hack.ipynb#X24sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a>     print("Nice roll!")

ValueError: invalid literal for int() with base 10: '2.3'