Lesson Three Homework
Lesson Three hacks
Hacks 1
The logical operators:
- NOT: Not is a logical operator that reverses, or takes the opposite, of the data. For example, true would be false, and 1 would be 0.
- AND: decides if both conditions are met- usually an "if" statement and an action or decision will occur if both conditions are met. For example, if an animal is agressive AND hungry, then the animal will attack for food.
- OR: only one of two conditions needs to be met in order for OR to work. If one condition is met then an action or decision will occur. For example, if a person is old OR disabled, they may need a wheelchair.
print("NOT:")
computer_power = True # the computer is on
power_status = not(computer_power) # the opposite of one is off
print(power_status) # the computer is off
# This example below uses both AND and OR so you can see the different situations in which one would use them:
print("AND + OR:")
i = input("input a number between 1 and 10")
if int(i) > 10 or int(i) < 1: # i must satisfy one condition
print("Your number is not in the range")
if int(i) >= 1 and int(i) <= 10: # i must satisfy both conditions
print("Your number is in the range")
userinput = input("Type in a string and see how many vowels are in it!")
vowelcount = 0
for i in userinput: #this will iterate through each character in the string
if i == "a": #if the character has an a, it will add one
vowelcount = vowelcount + 1
if i == "e": # if the character has an e, it will add one to the vowel count
vowelcount = vowelcount + 1
if i == "i": # if the character is an i, adds one
vowelcount = vowelcount + 1
if i == "o": # if the character is an o, adds one
vowelcount = vowelcount + 1
if i == "u": # if the character is a u, adds one
vowelcount = vowelcount + 1
print("There are " + str(vowelcount) + " vowels in this string")
usernumber = input("please input a number")
multiples_sum = 0
if int(usernumber) >= 0: # this will make sure the input is positive
for i in range(int(usernumber)+1): # this will iterate through each number up to the input
if int(i) % 3 == 0: # I used MOD to figure out if the number is a multiple of 3 or 5
multiples_sum += int(i) # this adds the multiple to the count and prints if the number is a multiple
print(str(i) + " is a multiple of 3" )
elif int(i) % 5 == 0: # same as above but for 5
multiples_sum += int(i)
print(str(i) + " is a multiple of 5")
print("The sum of the multiples of 3 and 5 for your input is = " + str(multiples_sum))
Defining the key terms:
-
selection = a segment of code will only run if a specific condition is met example:
-
algorithim = a function/code that completes a task or solves a problem. example:
-
conditional statement = A statement or order of code that will occur if a specific condition is met (true/false) example:
usersinput = int(input("What was your percentage grade on the test?"))
if usersinput >= 75:
print("Congradulations! You passed your test")
if usersinput >= 90:
print("You got an A!")
else:
print("Im sorry, you failed. Try again next time!")
Flow chart one:
people = int(input("How many people are you feeding for dinner?"))
if people >= 4:
adults = int(input("How many adults are eating?"))
children = int(input("How many children are eating?"))
if adults > children:
print("triple cooking porportions")
else:
print("souble cooking porportions")
else:
print("Stick to normal cooking instructions")
Flow chart two:
showering = input("Are you showering in the morning?")
if showering == "yes":
drying_hair = input("Are you blow drying your hair?")
if drying_hair == "yes":
print("wake up at 6:30 AM to get ready")
else:
print("wake up at 6:45 AM to get ready")
else:
print("wake up at 7:25 AM to get ready")
Flow chart three:
coffee = input("Do you like coffee?")
if coffee == "yes":
espresso = int(input("How many shots of espresso do you want?"))
if espresso <=1:
print("Order a machiatto")
else:
print("get a double shot latte")
else:
print("Order a tea")
congestion = input("Do you have a runny nose?")
if congestion == "yes":
fever = input("Do you have a fever?")
if fever == "yes":
print("You have the flu :(")
else:
print("You have a cold:(")
else:
pinkeye = input("Are your eyes red?")
if pinkeye == "yes":
print("You have pinkeye :(")
else:
print("You seem to be healthy!")
STEM = input("Do you like math and science?")
if STEM == "yes": # asks about math vs science and gives reccomendation
math = input("Do you like math?")
if math == "yes":
print("You should take AP Calculus AB")
else:
print("You should take AP Biology")
else: # asks about non-STEM classes and gives reccomendation
ceramics = input("Do you like working with clay?")
if ceramics == "yes":
print("You should take cermaics")
else:
print("You should take drawing and painting or photography")