What are the three main types of logic used for truth tables?
And, or, not
How are or, if, & and gates represented?
What is the difference between the = and == operators?
= is to assign a value to a variable
== is used to compare the value of two variables
What does "elif" mean?
"else if" -- used when you need another if statement
Write a program that requests a percentage grade from the user and tells them what their letter grade is. If they input a negative number, tell them that is not a grade.
A = 90-100
B = 80-89
C = 70-79
D = 60-69
F = less than 60
def grade():
percentage_grade = int(input("What is your percentage grade?"))
if percentage_grade >= 90:
print("Congrats! You have an A")
elif percentage_grade >= 80:
print("Nice job! You have a B")
elif percentage_grade >= 70:
print("You have a C")
elif percentage_grade >= 60:
print("You have a D")
elif percentage_grade < 60:
print("You are failing")
elif percentage_grade < 0:
print("How do you have a negative grade? Try again")
grade()
In notation, how are and, or, & not logic represented? (Hint: how would we notate them in a statement)
AND: ∧ and ·
OR: ∨ and +
NOT: ¬
What are the definitions of gates and circuits?
Gate - performs a Boolean logic operation on one or more binary inputs and then outputs a single binary output
Circuit - a model of computation in which input values proceed through a sequence of gates, each of which computes a function.
What is the definition of modulus?
The remainder of a number after division of a certain number.
What does the operator "in" do?
Determines if a value is in a selection of values
Write a program that asks a user to input a number. If it is divisible by 5, return the number. If not, print no output.
def divisible_by_five():
number = int(input("Please enter a number"))
if number%5 == 0:
print("Your number,", number, "is divisible by five")
divisible_by_five()
Fill in the following truth table, with ~ meaning not.
p ~p q ~p∧q
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
Create a circuit from the following:
A ∧ (B ∨ C)
You got it
What is the result of this command? print ("5" + "3")
A) 8
B) "8"
C) "5" + "3"
D) 53
D) 53
What would the output be for the following program?
si_days = ["Monday", "Wednesday", "Sunday"]
days_free = ["Wednesday", "Saturday", "Sunday"]
if "Monday" in si_days and "Monday" in days_free:
print("You should go to SI on Mondays")
elif "Wednesday" in si_days and "Wednesday" in days_free:
print("You should go to SI on Wednesdays")
elif "Sunday" in si_days and "Sunday" in days_free:
print("You should go to SI on Saturdays")
>> You should go to SI on Wednesdays
>> You should go to SI on Sundays
Write a program that checks if "dog" is in the following list:
pets = ['cat', 'hamster', 'dog', 'bird', 'guinea pig']
pets = ['cat', 'hamster', 'dog', 'bird', 'guinea pig']
if 'dog' in pets:
print("Yes, a dog is a pet")
Write a truth table for the following:
(¬A) ∧ (B ∨ C)
A B C ¬A B ∨ C Output
0 0 0 1 0 0
0 0 1 1 1 1
0 1 0 1 1 1
0 1 1 1 1 1
1 0 0 0 0 0
1 0 1 0 1 0
1 1 0 0 1 0
1 1 1 0 1 0
Create a circuit for the following sequence:
(A ∧ B) ∨ (¬B ∧ C)
You got it
How do you limit input to only integers, floats, or strings?
str(input(
float(input(
int(input(
What is the result of the following code, given an input of -1?
number = int(input("Enter an integer number: "))
if number >= 1:
print("Your number is greater than 10")
elif number ==0:
print("Your number is 0")
There would be no output.
Write a program that tells the user when to set their alarm clock. If they input that it is Saturday or Sunday, they need to wake up at 9 AM. If it is Monday, Wednesday, or Friday, the need to wake up at 7 AM. If it is Tuesday or Thursday, they need to wake up at 6 AM. If they enter something besides a day, tell them that is not a valid input. Additionally, make the input not case sensitive.
day = input("What day is it?")
if day == "Saturday" or day=="saturday" or day == "sunday" or day=="Sunday":
print("You need to wake up at 9:00 AM")
elif day=="Monday" or day=="monday" or day=="Wednesday" or day=="wednesday" or day=="friday" or day=="Friday":
print("You need to wake up at 7:00 AM")
elif day=="Tuesday" or day=="tuesday" or day=="Thursday" or day=="thursday":
print("You need to wake up at 6:00 AM")
else:
print("That is not a day of the week")
Write a truth table for the following circuit:
A B ¬A A∨B ¬A∧B (¬A∧B) ∨ (A)
0 0 1 0 0 0
0 1 1 1 0 1
1 0 0 1 0 1
1 1 0 1 0 1
Write a truth table for the circuit below:
See slideshow
What is the result of 127 % 21?
1
Consider the following example:
lst = [1, 2, 3]
print(1 in lst)
What would the output be?
True
Write a program that asks a user for the time, rounded to the nearest hour. Then, calculate and print what time it will be in four hours using a 12-hour clock (e.g, 1 pm instead of 13:00)
time = int(input("What time is it currently, rounded to the nearest hour? "))
in_four_hours = (time+4)%12
print(in_four_hours)