|
import random |
|
|
|
title = "Addition in Different Bases" |
|
description = "This module helps you practice adding numbers in different bases such as binary, octal, and hexadecimal." |
|
|
|
def generate_question(): |
|
base = random.choice([2, 8, 16]) |
|
num1 = random.randint(1, 15) |
|
num2 = random.randint(1, 15) |
|
|
|
if base == 2: |
|
correct_answer = bin(num1 + num2)[2:] |
|
elif base == 8: |
|
correct_answer = oct(num1 + num2)[2:] |
|
else: |
|
correct_answer = hex(num1 + num2)[2:] |
|
|
|
question = f"Add {num1} and {num2} in base {base}." |
|
options = [correct_answer] |
|
|
|
|
|
while len(options) < 5: |
|
fake_option = bin(random.randint(1, 30))[2:] |
|
if fake_option not in options: |
|
options.append(fake_option) |
|
|
|
random.shuffle(options) |
|
explanation = ( |
|
f"The sum of {num1} and {num2} in base {base} is {correct_answer}." |
|
"\n\n**Step-by-step solution:**\n" |
|
"1. Convert the numbers to decimal if necessary.\n" |
|
"2. Perform the addition in decimal.\n" |
|
"3. Convert the result back to the base if needed." |
|
) |
|
return question, options, correct_answer, explanation |
|
|