File size: 1,188 Bytes
d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 32f2e18 d924141 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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]
# Generate incorrect options
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
|