A program that generates questions to train your number base conversion skills. This program is made to ask questions about octet, binary and hexadecimal. No other bases. It first asks a series of questions to the user such as "amount of questions", "question type" and "base". Then, it starts generating questions based on the user's input. The user can give an answer and it will tell them if they're correct. Code is perfectly functional.
I'm asking for a review because I sense a bad smell in my code. It feels overly long, repetitive, and kind of confusing. There surely must be a better way. Maybe an OOP approach? Something to cut down on the complexity and "if-else" repetition. Though, I feel it is this way partly because I have limited it to only 3 bases. I'm a late beginner but I wouldn't say that I am intermediate just yet.
My code uses only one external library, that is baseconvert
. Can be installed with pip using pip install baseconvert
import random
import baseconvert
# The Start
# ---------
# Step 1: Ask whether the user wants 'Decimal > Base X' or 'Base X > Decimal' or 'Random'
# Step 2: Ask how many questions the user wants. Type the answer. Type -1 for infinite.
# Step 3: If infinite, prompt to type 'f' to stop.
# Step 4: Ask what base you want based on index. 1. Octet // 2. Hexadecimal // 3. Binary // 4. Random
def question_type_ask():
print('Which question would you like to be asked?')
print('1. Decimal > Base X\n2. Base X > Decimal\n3. Random')
ans = input('Input: ')
if ans != '1' and ans != '2' and ans != '3':
print('Invalid input')
question_type_ask()
return ans
def amount_ask():
print('How many questions do you want to be asked? Type -1 for infinite.')
print('Type \'f\' as answer to stop')
try:
ans = int(input('Input: '))
except ValueError:
print('Input numbers only, please.')
return amount_ask()
if ans >= -1:
return ans
else:
return amount_ask()
def base_ask():
print('Which base do you want to be asked?')
print('1. Octet\n2. Hexadecimal\n3. Binary\n4. Random')
ans = input('Input: ')
if ans != '1' and ans != '2' and ans != '3' and ans != '4':
print('Invalid input')
base_ask()
return ans
question_type = question_type_ask()
amount = amount_ask()
base = base_ask()
is_random_question = False
is_random_base = False
if question_type == '3':
is_random_question = True
if base == '4':
is_random_base = True
def question_generator():
global question_type
global amount
global base
base_list = ['1', '2', '3']
type_list = ['1', '2']
while True:
if is_random_base:
base = random.choice(base_list)
if is_random_question:
question_type = random.choice(type_list)
if question_type == '1': # Decimal > Base X
base1 = 'Base 10'
num = random.randrange(100, 100000)
if base == '1': # Octet
base2 = 'Base 8'
correct_ans = baseconvert.base(num, 10, 8, string=True)
break
elif base == '2': # Hexa
base2 = 'Base 16'
correct_ans = baseconvert.base(num, 10, 16, string=True)
break
elif base == '3': # Binary
base2 = 'Base 2'
correct_ans = baseconvert.base(num, 10, 2, string=True)
break
elif base == '4': # Random
base2 = random.choice(base_list)
if base2 == base_list[0]:
base = '1'
elif base2 == base_list[1]:
base = '2'
elif base2 == base_list[2]:
base = '3'
elif question_type == '2': # Base X > Decimal
base2 = 'Base 10'
correct_ans = str(random.randrange(100, 100000))
if base == '1': # Octet
base1 = 'Base 8'
num = baseconvert.base(correct_ans, 10, 8, string=True)
break
elif base == '2': # Hexa
base1 = 'Base 16'
num = baseconvert.base(correct_ans, 10, 16, string=True)
break
elif base == '3': # Binary
base1 = 'Base 2'
num = baseconvert.base(correct_ans, 10, 2, string=True)
break
elif base == '4': # Random
base1 = random.choice(base_list)
if base1 == base_list[0]:
base = '1'
elif base1 == base_list[1]:
base = '2'
elif base1 == base_list[2]:
base = '3'
print(f'[{base1}] {num} to [{base2}]')
ans = input('Answer: ')
if ans == correct_ans:
print('You are correct!')
elif ans.lower() == 'f':
question_generator()
else:
print(f'Wrong! The answer is {correct_ans}')
counter = 0
if amount == -1:
amount = float('inf')
while counter < amount:
counter += 1
question_generator()
while True:
loop insidequestion_generator
function really necessary ? \$\endgroup\$ – impopularGuy May 14 '20 at 11:53