Skip to the content.

BI 3 Binary Base 2 Math

# Popcorn Hack 1

def binary_to_decimal(binary_str):
    decimal = 0
    for i in range(len(binary_str)):
        decimal += int(binary_str[-(i + 1)]) * (2 ** i)
    return decimal

#Get user input
binary_input = input("Enter a binary number: ")
decimal_output = binary_to_decimal(binary_input)
print(f"The decimal representation of {binary_input} is {decimal_output}.")
The decimal representation of 100001 is 33.
# Popcorn Hack 2

import random
import time

def binary_addition_challenge():
    num1 = bin(random.randint(0, 255))[2:]
    num2 = bin(random.randint(0, 255))[2:]
    
    print("๐Ÿ”ข Binary Addition Challenge!")
    print(f"Number 1: {num1}")
    print(f"Number 2: {num2}")
    
    start_time = time.time()
    user_input = input("Enter the sum (in binary): ").strip()
    end_time = time.time()
    
    correct = bin(int(num1, 2) + int(num2, 2))[2:]
    
    if user_input == correct:
        print(f"โœ… Correct! Time: {end_time - start_time:.2f} sec | +10 points")
        return 10
    else:
        print(f"โŒ Incorrect. Correct answer: {correct} | -5 points")
        return -5

def decimal_to_binary_challenge():
    num = random.randint(0, 255)
    print("๐Ÿ” Decimal โžก๏ธ Binary Challenge!")
    start_time = time.time()
    user_input = input(f"Convert decimal {num} to binary: ").strip()
    end_time = time.time()
    
    correct = bin(num)[2:]
    
    if user_input == correct:
        print(f"โœ… Correct! Time: {end_time - start_time:.2f} sec | +5 points")
        return 5
    else:
        print(f"โŒ Incorrect. Correct answer: {correct} | -2 points")
        return -2

def binary_to_decimal_challenge():
    num = random.randint(0, 255)
    binary = bin(num)[2:]
    print("๐Ÿ” Binary โžก๏ธ Decimal Challenge!")
    start_time = time.time()
    user_input = input(f"Convert binary {binary} to decimal: ").strip()
    end_time = time.time()
    
    if user_input.isdigit() and int(user_input) == num:
        print(f"โœ… Correct! Time: {end_time - start_time:.2f} sec | +5 points")
        return 5
    else:
        print(f"โŒ Incorrect. Correct answer: {num} | -2 points")
        return -2

def play_game():
    print("๐ŸŽฎ Welcome to Popcorn Hack 2: Binary Battle!\n")
    score = 0

    for round_num in range(1, 4):
        print(f"\n--- Round {round_num} ---")
        challenge = random.choice([
            binary_addition_challenge, 
            decimal_to_binary_challenge, 
            binary_to_decimal_challenge
        ])
        score += challenge()

    print(f"\n๐Ÿ Game Over! Final Score: {score} points")

# Run the game
play_game()
๐ŸŽฎ Welcome to Popcorn Hack 2: Binary Battle!


--- Round 1 ---
๐Ÿ” Binary โžก๏ธ Decimal Challenge!
โœ… Correct! Time: 22.17 sec | +5 points

--- Round 2 ---
๐Ÿ” Binary โžก๏ธ Decimal Challenge!
โœ… Correct! Time: 17.12 sec | +5 points

--- Round 3 ---
๐Ÿ” Decimal โžก๏ธ Binary Challenge!
โœ… Correct! Time: 11.81 sec | +5 points

๐Ÿ Game Over! Final Score: 15 points
# 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 โšก
Subtract these two binary numbers: 11010 - 10011
โœ… Correct!

โšก Round 2 โšก
Convert this decimal number to binary: 22
โœ… Correct!

โšก Round 3 โšก
Convert this binary number to decimal: 111
โœ… Correct!

๐Ÿ† Game Over! ๐Ÿ†
Your final score: 3/3
๐ŸŒŸ Amazing job! You're a Binary Master!

To convert a binary number to a decimal number, multiply each binary digit by 2 raised to its position power (starting from right at 0), then add the results together.

For the binary number 11111111:
1ร—2โท + 1ร—2โถ + 1ร—2โต + 1ร—2โด + 1ร—2ยณ + 1ร—2ยฒ + 1ร—2ยน + 1ร—2โฐ

= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= 255
Answer: 255 (decimal)