Vocab

  • Boolean: a data type with two possible values: true or false
  • Logical Operators: these types of operators don't necessarily deal with equivalent/non-equivalent values
  • Conditionals
    • Selection: Uses a condition that evaluates to true or false
    • determines which part of an algorithm are executed based on a condition being true or false
    • Algorithm is a finite set of instructions that accomplish a specific task
  • Nested Conditionals: consist of conditional statements within other conditional statements

Boolean Expression, Conditionals, Nested Conditionals

  • Boolean
    • a data type with two possible values: true or false
    • boolean quantities are restricted to a singular bit (can only be either 1 or 0)
  • Relational Operators
    • The AP exam will provide a ref sheet with these operators: operators
grade1 = 90
grade2 = 65
grade3 = 60
grade4 = 75
grade5 = 95

totalGrade = grade1 + grade2 + grade3 + grade4 + grade5
avgGrade = totalGrade/5
if avgGrade > 80:
    print("The avf grade was " + avgGrade)
else:
    print("the avg grade was not greater than 80 :(. It was " + str(avgGrade))
  • Logical Operators
    • these types of operators don't necessarily deal with equivalent/non-equivalent values.
    • Instead they work on operands to produce a singular boolean result
    1. AND: returns TRUE if the operands around it are TRUE
    2. OR: returns TRUE if at least one operand is TRUE
    3. NOT: returns TRUE if the following boolean is FALSE
print("1 > 2 or 5 < 12:", 1 > 2 or 5 < 12)
# Output TRUE  using OR ^

# Output FALSE using NOT
print("7 > 8:", not 7 > 8)

# Output FALSE using AND
print("10 > 20:", 10 > 20 and False)
  • Conditionals
    • Selection: Uses a condition that evaluates to true or false
    • determines which part of an algorithm are executed based on a condition being true or false
    • Algorithm is a finite set of instructions that accomplish a specific task conditionals
  • Conditional Statements
    • also known as if statements
x = 20
y = 10
if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")
num1 = 100
num2 = 100
sum = num1 + num2
if sum == 200:
    print(sum)
else:
    print(sum)
  • Nested Conditionals
    • consist of conditional statements within other conditional statements
    • uses "if else" statements within "if else" statements nested

HW

  • Write a program that fits these conditions using nested conditionals:
    • If the product is expired, print "this product is no good"
    • If the cost is above 50 dollars, and the product isn't expired, print "this product is too expensive"
    • If the cost is more than 25 dollars but under 50, and the product isn't expired, print "this is a regular product"
    • If the cost is under 25 dollars, print "this is a cheap product"
infoP = []

infoP.append({
    "Product": "Cheese",
    "Expired": True,
    "Cost": 15,
})

infoP.append({
    "Product": "Wine",
    "Expired": False,
    "Cost": 75,
})

def print_info(p_rec):
    if (p_rec["Expired"]): 
            print("\t", "This product is no good")
    else:
        if (p_rec["Cost"] > 50):
            print("\t", "This product is too expensive")
        elif (p_rec["Cost"] > 25 and p_rec["Cost"] <= 50):
            print("\t", "This is a regular product")
        else:
            print("\t", "This is a cheap product")

def print_data(p_rec): #formatting
    print("\t", "Product:", p_rec["Product"])  
    print("\t", "Expired:", p_rec["Expired"]) 
    print("\t", "Cost:", p_rec["Cost"])
    print_info(p_rec)
    print("\n")

def data_entry(): #defining the function that asks for user input
    product = input("What is the product? (one word)")
    expired_string = input("Is it expired? (True or False)")
    cost = input("How much did it cost(dont include $, round to nearest whole number)")

    if (expired_string == "True"):
        expired = True
    else:
        expired = False

    infoP.append({ #appends the user input to the dictionary
        "Product": product,
        "Expired": expired,
        "Cost": int(cost),
   
    })

def main():
    Continue = True #defining continue as true
    while Continue:
        inp = input("Would you like to add a product to the database, type no if you want to exit)?")
        if inp == "no":
            print("Come back again!")
            Continue = False
        elif inp == "add":
            data_entry()
        else:
            print("Invalid input. Please try again")

    length = len(infoP) #defines length as the number of records
    print("Total Number of Records: ", length) 
    for record in infoP:
            print_data(record)
            
 
main()
Come back again!
Total Number of Records:  3
	 Product: Cheese
	 Expired: True
	 Cost: 15
	 This product is no good


	 Product: Wine
	 Expired: False
	 Cost: 75
	 This product is too expensive


	 Product: pie
	 Expired: False
	 Cost: 15
	 This is a cheap product


  • Create a multiple choice quiz that ...
    • uses Boolean expressions
    • uses Logical operators
    • uses Conditional statements
    • prompts quiz-taker with multiple options (only one can be right)
    • has at least 3 questions
import getpass

def question_with_response(prompt):  #defines question_with_response
    print("Question: " + prompt) #prints the question to the user
    msg = input() #takes the user's input
    return msg #returns the user's input as a printed answer

def question_and_answer(prompt): 
    print("Question: " + prompt)  
    msg = input()  
    print("Answer: " + msg) 

questions = 3
correct = 0

print('Hello, ' + getpass.getuser())
print("You will be asked " + str(questions) + " questions.") #str(questions) turns the number 3 that is = to questions into a string that can be printed

rsp = question_with_response("Have humans ever reached the moon? False True") #asks the question in terminal
if (rsp == "True"):
    print(rsp + " is correct!") #prints "is correct" if the rsp was correct
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("How many hearts does an octopus have? a. 8 b. 4 c. 3 d. 1")
if rsp == "c":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Which of the following countries have won the world cup?(Type your answer separated by ',') a. Brazil b. France c. Japan d. China")
# rsp = "a,b"
if ("a" in rsp) and ("b" in rsp) and ("c" not in rsp) and ("d" not in rsp):
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

print("You got " + str(correct) + " out of 3") 
Hello, vivian
You will be asked 3 questions.
Question: Have humans ever reached the moon? False True
True is correct!
Question: How many hearts does an octopus have? a. 8 b. 4 c. 3 d. 1
c is correct!
Question: Which of the following countries have won the world cup?(Type your answer separated by ',') a. Brazil b. France c. Japan d. China
a,b is correct!
You got 3 out of 3