Lesson 3.5 Finn/Jake

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
True

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

Logical Operators

NOT

  • NOT, it displays the opposite of whatever the data is. Mainly used for true/false, and does not effect the variable.
isRaining = False
result = not(isRaining)
print(result)
True

AND

  • AND, used to evaulte two conditions together and determine if both condintions are met
grade = 95
if grade > 70 and grade <= 100:
    print("You passed the quiz")
You passed the quiz

OR

  • OR, when using or the function only looks to see if one of the conditions is met then will
lives = 1
score = 21
if lives <= 0 or score > 20:
    print("end game")
end game

Hacks

  • Explain in your own words what each logical operator does
  • Code your own scenario that makes sense for each logical operator

Lesson 3.6 Paaras/Shruthi

Learning Objectives (Some Things You Might Want to Keep Note Of)

  • Conditionals allow for the expression of algorithms that utilize selection without a programming language.
  • Writing conditional statements is key to computer science.
  • Determine the result of conditional statements

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")
}
  Input In [6]
    if (30 == 7) {
                 ^
SyntaxError: invalid syntax

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)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb Cell 24 in <cell line: 3>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X32sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> # my first attempt at the Level 1 Challenge
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X32sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a> word=input("Input a word to check how many vowels it has. ")
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X32sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> i == 0 #number of vowels
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X32sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> if word.__contains__("a"):
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X32sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>     i =+ 1

NameError: name 'i' is not defined
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)
4
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)
0 is a multiple of 3
3 is a multiple of 3
5 is a multiple of 5
6 is a multiple of 3
9 is a multiple of 3
10 is a multiple of 5
12 is a multiple of 3
15 is a multiple of 3
60
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")
87.0 is the student's grade average
student is eligible for credit
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")
202 is greater than or equal to 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")
You is available on Netflix
show/movie is reccomended
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!")
Advanced Function Analysis, AP Calculus AB, AP Calculus BC, AP Statistics

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.

Lesson 3.7 James

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

Question 1

Look at the following code

  • what happens when x is 5 and y becomes 4? Is the output same or change?
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")

Question 2

  • How much it will be cost when the height will be 60, age is 17, and photo taken?
  • when this person came after 1 year how much it will be cost?
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.")
The total bill is $10.

Let's look at Examples

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()
rock
scissor
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb Cell 33 in <cell line: 30>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=27'>28</a>     else:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=28'>29</a>         print("Goodbye")
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=29'>30</a> gameStart()

/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb Cell 33 in gameStart()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> print(randomOne)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11'>12</a> while True:
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a>     if winning[i] == gamer:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>         break
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/alexac54767/vscode/Alexa-Fastpage/_notebooks/2022-11-30-LESSONPLAN.ipynb#X44sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a>     else:

UnboundLocalError: local variable 'i' referenced before assignment

Binary Math with Conversions
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.

Kahoot quiz

kahoot

  • After finishing this quiz, please take a screenshot of how much you got correct

Rubric for hacks:

  • Each section is worth .33 and you will get + 0.01 if all are completed to have full points.

How to get a .33

  • All hacks are done for the section, fully completed.

How to get a .30

  • Not all hacks are done in the section

Below a .30

  • Sections are missing/incomplete