Popcorn Hack #1
def binary_to_decimal(binary_str):
# Convert the binary string to a decimal integer using Python's built-in functionality
decimal = int(binary_str, 2)
return decimal
# Ask the user to input a binary number
binary_input = input("Enter a binary number: ")
# Validate the input to ensure it's a valid binary string
if all(bit in '01' for bit in binary_input):
result = binary_to_decimal(binary_input)
print(f"Binary '{binary_input}' is {result} in decimal.")
else:
print("Invalid input! Please enter a binary number consisting of only 0s and 1s.")
Binary '10101' is 21 in decimal.
Popcorn Hack #2
import random
import time
def binary_addition_battle():
# Generate two random binary numbers (up to 8 bits)
num1 = bin(random.randint(0, 255))[2:]
num2 = bin(random.randint(0, 255))[2:]
# Show the binary numbers
print(f"Add the following binary numbers:")
print(f"Number 1: {num1}")
print(f"Number 2: {num2}")
# Start the timer
start_time = time.time()
# Ask the user for the sum
user_answer = input("Your answer (in binary): ")
# End the timer
end_time = time.time()
# Calculate the correct binary sum
correct_answer = bin(int(num1, 2) + int(num2, 2))[2:]
# Check if the answer is correct
if user_answer == correct_answer:
print(f"Correct! You took {end_time - start_time:.2f} seconds.")
print(f"Your score: +10 points!")
else:
print(f"Oops! The correct answer was {correct_answer}.")
print(f"Your score: -5 points.")
# Run the game
binary_addition_battle()
Add the following binary numbers:
Number 1: 10101111
Number 2: 11111001
Correct! You took 35.59 seconds.
Your score: +10 points!
Popcorn Hack #3
import random
def binary_subtraction(bin1, bin2):
max_len = max(len(bin1), len(bin2))
bin1 = bin1.zfill(max_len)
bin2 = bin2.zfill(max_len)
result = ''
borrow = 0
for i in range(max_len-1, -1, -1):
bit1 = int(bin1[i])
bit2 = int(bin2[i])
sub = bit1 - bit2 - borrow
if sub == 0 or sub == 1:
result = str(sub) + result
borrow = 0
elif sub == -1:
result = '1' + result
borrow = 1
elif sub == -2:
result = '0' + result
borrow = 1
result = result.lstrip('0') or '0'
return result
print("π§ Binary Subtraction Challenge! π§ ")
score = 0
total_questions = 3
for question_num in range(1, total_questions + 1):
num1 = random.randint(8, 63)
num2 = random.randint(0, num1)
bin1 = bin(num1)[2:]
bin2 = bin(num2)[2:]
print(f"\nProblem {question_num}: {bin1} - {bin2}")
user_answer = input("Your answer: ").strip()
correct_answer = binary_subtraction(bin1, bin2)
if user_answer == correct_answer:
print("β
Correct!")
score += 1
else:
print(f"β Incorrect. The correct answer was {correct_answer}.")
print(f"\nπ― You got {score}/{total_questions} correct!")
print("Thanks for practicing!")
π§ Binary Subtraction Challenge! π§
Problem 1: 10000 - 1110
β
Correct!
Problem 2: 101011 - 1010
β
Correct!
Problem 3: 100001 - 100
β
Correct!
π― You got 3/3 correct!
Thanks for practicing!
Homework Hack
import random
import time
def binary_addition(a, b):
return bin(int(a, 2) + int(b, 2))[2:]
def binary_subtraction(a, b):
if int(a, 2) < int(b, 2):
return "Error"
return bin(int(a, 2) - int(b, 2))[2:]
def decimal_to_binary(n):
return bin(n)[2:]
def binary_to_decimal(b):
return int(b, 2)
def binary_battle_royale():
print("πΎ Welcome to Binary Battle Royale! πΎ")
score = 0
total_rounds = 3
for round_num in range(1, total_rounds + 1):
print(f"\nβ‘ Round {round_num} β‘")
mode = random.choice(["addition", "subtraction", "dec_to_bin", "bin_to_dec"])
if mode == "addition":
num1 = bin(random.randint(0, 15))[2:]
num2 = bin(random.randint(0, 15))[2:]
print(f"Add these two binary numbers: {num1} + {num2}")
user_answer = input("Your answer (binary): ").strip()
correct_answer = binary_addition(num1, num2)
if user_answer == correct_answer:
print("β
Correct!")
score += 1
else:
print(f"β Incorrect. The correct answer was {correct_answer}.")
elif mode == "subtraction":
num1_val = random.randint(8, 31)
num2_val = random.randint(0, num1_val)
num1 = bin(num1_val)[2:]
num2 = bin(num2_val)[2:]
print(f"Subtract these two binary numbers: {num1} - {num2}")
user_answer = input("Your answer (binary): ").strip()
correct_answer = binary_subtraction(num1, num2)
if user_answer == correct_answer:
print("β
Correct!")
score += 1
else:
print(f"β Incorrect. The correct answer was {correct_answer}.")
elif mode == "dec_to_bin":
decimal_number = random.randint(0, 31)
print(f"Convert this decimal number to binary: {decimal_number}")
user_answer = input("Your answer (binary): ").strip()
correct_answer = decimal_to_binary(decimal_number)
if user_answer == correct_answer:
print("β
Correct!")
score += 1
else:
print(f"β Incorrect. The correct answer was {correct_answer}.")
elif mode == "bin_to_dec":
binary_number = bin(random.randint(0, 31))[2:]
print(f"Convert this binary number to decimal: {binary_number}")
user_answer = input("Your answer (decimal): ").strip()
correct_answer = str(binary_to_decimal(binary_number))
if user_answer == correct_answer:
print("β
Correct!")
score += 1
else:
print(f"β Incorrect. The correct answer was {correct_answer}.")
print("\nπ Game Over! π")
print(f"Your final score: {score}/{total_rounds}")
if score == total_rounds:
print("π Amazing job! You're a Binary Master!")
elif score >= total_rounds // 2:
print("π Good effort! Keep practicing!")
else:
print("π‘ Don't worry β review the rules and try again!")
# --- Start the game ---
binary_battle_royale()
πΎ Welcome to Binary Battle Royale! πΎ
β‘ Round 1 β‘
Add these two binary numbers: 1100 + 110
β
Correct!
β‘ Round 2 β‘
Convert this decimal number to binary: 14
β
Correct!
β‘ Round 3 β‘
Add these two binary numbers: 1001 + 100
β
Correct!
π Game Over! π
Your final score: 3/3
π Amazing job! You're a Binary Master!
HW FRQ
-
To convert a binary number to decimal, multiply each bit by 2 raised to the power of its position (starting from 0 on the right), then add the results together.
-
For the binary number 11111111, the decimal equivalent is 255.