LESSON PLAN
the lesson plan for 3.5-3.7
- Lesson 3.5 Finn/Jake
- Relational Operators
- Logical Operators
- Hacks
- Lesson 3.6 Paaras/Shruthi
- Lesson 3.7 James
- Rubric for hacks:
What is a Boolean
- The defention of a Boolean is a denoting a system of algebraic notation used to represent logical propositions, especially in computing and electronics.
- A boolean expresions are either true or false.
- Testing if two numbers or variables are equal is a common example.
- For example: The sky is blue = True
- What do we use to represent them? (Hint think of how domain/range of graph is answered) <- variables
- How is binary related to this? <- 0=false and 1=true
- How could this be used when asked a question?
Examples (Not part of homework)
Type the code down below for these two example
- Try to make a statement that determines if a variable "score" is equal to the number 3
- Try to make this score variable have an output if it equals 3
# The code for ex:1
score = 3
i = score
if i == 3:
print("True")
else:
print("False")
# The code for ex:2
Relational Operators
- The mathmatical relationship between two variables
- Determines an output on whether or not the statement is true
- a=b, a>b, etc.
Examples (Not part of homework)
Type the code down below for these two example
- You have to be the age of 16 or older to drive
- Each rollercoaster cart holds 4 for people per cart
# Put the code for ex:1
i = "age"
if i >= str(16):
print("can drive")
else:
print("cannot drive")
# Put the code for ex:2
i = "number of people"
numcarts = i/4
cannotride = 16 % i
isRaining = False
result = not(isRaining)
print(result)
grade = 95
if grade > 70 and grade <= 100:
print("You passed the quiz")
lives = 1
score = 21
if lives <= 0 or score > 20:
print("end game")
Key Terms
- Selection: The specific block of code that will execute depending on the algorithm condition returning true or false.
- Algorithm: "A finite set of instructions that accomplish a specific task."
- Conditional Statement / If-Statement: A statement that affects the sequence of control by executing certain statements depending on the value of a boolean.
function isEven(parameter) {
if (parameter % 2 == 0) {
console.log("The number is even.");
}
else if (parameter % 2 != 0) {
console.log("The number is odd.")
}
}
isEven(4)
A computer science student such as yourself will see conditional statements in JavaScript a lot. Below is an example of one in action:
if (30 == 7) {
console.log("The condition is true")
}
That is one conditional statement, but this algorithm is too simple to have anything done with it. Below is an algorithm building on the previous algorithm:
if (30 == 7) {
console.log("The condition is true")
}
else if (30 != 7) {
console.log("The condition is false")
}
Conditional statements can be used for many a purpose. The algorithm above was quite simple, but conditionals can be found in more complex algorithms.
Essential Knowledge
Conditional statements ("if" statements) affect the sequential flow of control by executing different statements based on the value of a Boolean expression. The exam reference sheet provides:
In which the code in block of statements is executed if the Boolean expression condition evaluates to true; no action is taken if condition evaluates to false
# also needs to iterate through to check every character (what if there are multiple of each vowel?)
word=input("Input a word to check how many vowels it has. ")
i == 0 #number of vowels
if word.__contains__("a"):
i =+ 1
if word.__contains__("e"):
i =+ 1
if word.__contains__("i"):
i =+ 1
if word.__contains__("o"):
i =+ 1
if word.__contains__("u"):
i =+ 1
if i == 0:
print("this word has no vowels")
else:
print(i)
input_string = input("Input a string to check how many vowels it has. ")
vowelcount = 0
for character in input_string: #iterates through the string character by character
if character == "a":
vowelcount += 1
elif character == "e":
vowelcount += 1
elif character == "i":
vowelcount += 1
elif character == "o":
vowelcount += 1
elif character == "u":
vowelcount += 1
print(vowelcount)
numberstring = input("Input a number to find the sum of all the multiples of 3 or 5 below it. ")
numbertocheck = int(numberstring)
totalmultiples = 0
if numbertocheck >= 0: # if the number is negative it will display the sum as 0
for n in range(numbertocheck+1):
if n/3 == int(n/3): #checks for multiples of 3
print(str(n) + " is a multiple of 3")
totalmultiples += n #elif prevents duplicates of numbers with both multiples of 3 and 5
elif n/5 == int(n/5): # checks for multiples of 5
print(str(n) + " is a multiple of 5")
totalmultiples += n
print(totalmultiples)
def average(lst):
return sum(lst)/len(lst)
gradelist = [92, 83, 90, 74, 86, 97]
averagegrade = average(gradelist)
print(str(averagegrade) + " is the student's grade average")
if averagegrade >= 75:
print("student is eligible for credit")
else:
print("student needs to retake tests during break")
num1 = input("What is your first number?")
num2 = input("What is your second number?")
sumofnumbers = int(num1) + int(num2)
if sumofnumbers >= 100:
print(str(sumofnumbers) + " is greater than or equal to 100")
else:
print(str(sumofnumbers) + " is less than than 100")
# to test if a show is included in Netflix, and if it is recommended
showdictionary = {
'Wednesday':5, # key=title name and value=rating out of 5 stars
'You':3,
'Knight Before Christmas':4,
'Red Notice':5,
'Truth or Dare':1
}
show = input("What show are you looking for?")
if show in showdictionary.keys():
print(show + " is available on Netflix")
if showdictionary[show] >= 3:
print("show/movie is reccomended")
else:
print("show is not recommended")
else:
print(show + " is not available on Netflix")
STEM = input("Are you intrested in STEM? Y or N")
if STEM == "Y":
subject = input("Do you like math or science better?")
if subject == "math":
print("Advanced Function Analysis, AP Calculus AB, AP Calculus BC, AP Statistics")
if subject == "science":
print("Class recommendations: Chemistry, Physics, AP Enviornmental Science, AP Physics, AP Chemistry")
else:
language = input("Are you intrested in language? Y or N")
if language == "Y":
lansubject = input("Do you prefer a foreign language or English?")
if lansubject == "foreign language":
print("Class recommendations: Spanish, Mandarin, AP Spanish, AP Mandarin")
if lansubject == "English":
print("Class recommendations: American Literature, AP English Literature")
if language == "N":
print("See class list for more options!")
Hacks
- For the first hack, pretend you are a school's test grader. Create an array with integers, each integer representing one score from a student's taken tests. If the average of the student's test scores are at least 75 percent, then display that the student is elligible for credit, and if not, display that the student must retake the tests over break.
- The second hack is more number-oriented. Create an algorithm that calculates the sum of two numbers, then determines whether the sum is greater than or less than 100.
- The hacks above was heavily derived from CollegeBoard. As a computer science student, you should be able to create an algorithm utilizing conditionals. Try something number-oriented if you get stuck. Creativity gets points.
Nested Conditionals
- Nested conditional statements consist of conditional statements within conditional statements
- they're nested one inside the other
- An else if inside of another else if
- Can be used for a varying amount of "else if statements." The first if statement can represent if two coditions are true. The first else if can represent if only one or the other is true. The last else if represents if neither of the conditions are true.
Take aways
- Learn how to determine the result of nested condtional statements
- Nested conditional statements consist of conditional statements within conditional statements
- One condition leads to check in a second condition
Writing Nested Conditional Statements
- Can be planned and writen out first
- Flow chart is a possibility. Ask a question. If the statement is false end the flowchart with one result. If it is true then repeat the process once more.
- If (condition 1)
- {
- first block of statements
- }
- else
- {
- IF (condition 2)
- {
- second block of statements
- }
- }
this statement is false make a new result. Finally if the statement is true make a final result
x = 2
y = 3
if x == y:
print("x and y are equal")
else:
if x > y:
print("x is bigger than y")
elif x < y:
print("x is smaller than y")
height = int(input("Welcom to the rollercoaster! \nWhat is your height in Inch? "))
age = int(input("What is your age?"))
if height < 48 :
print("Can't ride")
elif age < 12 :
photo = input("Adult tickets are $5 \nDo you want a photo taken? Y or N. ")
if photo=="Y":
print("The total bill is $8.")
if photo=="N":
print("The total bill is $5.")
elif age < 18:
photo = input("Adult tickets are $7 \nDo you want a photo taken? Y or N. ")
if photo=="Y":
print("The total bill is $10.")
if photo=="N":
print("The total bill is $7.")
else :
photo = input("Adult tickets are $12 \nDo you want a photo taken? Y or N. ")
if photo=="Y":
print("The total bill is $15.")
if photo=="N":
print("The total bill is $12.")
import random
global i
game = ["rock", "scissor", "paper"]
winning = ["paper", "rock", "scissor"]
i = 0
def gameStart():
randomNumber = random.randrange(0,2)
randomOne = game[randomNumber]
gamer = str(input("what will you do"))
print(gamer)
print(randomOne)
while True:
if winning[i] == gamer:
break
else:
i += 1
if randomNumber == i:
print("You win")
else:
if randomNumber == (i+1)%3:
print("Lose")
elif randomNumber == (i+2)%3:
print("Draw")
pre = input("Do you want a game?[yes/no]")
if pre == "yes":
gameStart()
randomNumber = random.randrange(0,2)
else:
print("Goodbye")
gameStart()
Plus | Binary | Octal | Hexadecimal | Decimal | Minus |
---|---|---|---|---|---|
00000000 | 0 | 0 | 0 | ||
00000000 | 0 | 0 | 0 | ||
00000000 | 0 | 0 | 0 |
Hacks
- Create 3 differnt flow charts representing nested statements and transfer them into code.
- Create a piece of code that displays four statements instead of three. Try to do more if you can.
- Make piece of code that gives three different recommandations for possible classes to take at a scholl based on two different condtions. These conditions could be if the student likes STEM or not.