Important Lesson - Logic Gates
Important lessons to understand
Notes
- Binary is a number system using 0’s and 1’s
- it is the language of the computers.
Popcorn Hack 1
- This is binary
- This is not binary because it doesn’t include 1’s and 0’s
- 25 and the number is binary
Popcorn Hack 2
- 101+110 = 1011
- 1101 - 1011 = 010
- 111 + 1001 = 1110
Popcorn Hack 1
True becasue AND has higher priority than the OR
Popcorn Hack 2
False because the NOT operator is evaluated first and then the AND operator.
Popcorn Hack 3
True because the AND and NOT operators are evaluated first due to their high priority
Homework Hacks
Homework Hack 1: Binary Converter
Instructions:
- Create a function to convert a decimal number to binary.
- Create a function to convert a binary string back to decimal.
- Test your functions with positive and negative numbers.
Code:
# Function to convert decimal to binary
def decimal_to_binary(n):
if n >= 0:
return bin(n)[2:] # Remove the '0b' prefix
else:
return '-' + bin(n)[3:] # Handle negative numbers
# Function to convert binary to decimal
def binary_to_decimal(b):
if b.startswith('-'):
return -int(b[1:], 2)
else:
return int(b, 2)
# Test Cases
print(decimal_to_binary(10)) # Output: 1010
print(binary_to_decimal("1010")) # Output: 10
print(decimal_to_binary(-10)) # Output: -1010
print(binary_to_decimal("-1010")) # Output: -10
```python
while difficulty != "easy" or difficulty != "medium" or difficulty != "hard":
import time
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
while difficulty != "easy" and difficulty != "medium" and difficulty != "hard":
print("Please enter a valid difficulty level.")
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
time.sleep(0.5)
print("Difficulty set to:", difficulty)
grades = []
def add_grade(name: str, score: int):
grades.append((name, score))
def calculate_average() -> float:
total = 0
for entry in grades:
total += entry[1]
return total / len(grades)
def main():
while True:
action = input("Type 'add' to add a grade, 'avg' to see average, or 'exit' to quit: ")
if action == "add":
name = input("Enter student name: ")
score = int(input("Enter grade: "))
add_grade(name, score)
print(f"{name}'s grade added.")
elif action == "avg":
if grades:
avg = calculate_average()
print(f"Class average: {avg:.2f}")
else:
print("No grades yet.")
elif action == "exit":
print("Goodbye!")
break
else:
print("Invalid input. Try again.")
main()
darsh's grade added.
robert's grade added.
avg's grade added.
Class average: 84.00
Goodbye!