unit-conversion/main.py
2024-12-05 13:36:59 +01:00

97 lines
3.7 KiB
Python

import random
from colorama import Fore, Style
# Unit hierarchy and conversion factors
unit_hierarchy = ["km", "hm", "dam", "m", "dm", "cm", "mm"]
conversion_factors = [10, 10, 10, 10, 10, 10] # Stepwise conversion factor
# Generate a random conversion question
def generate_question(max_jump=2):
from_index = random.randint(0, len(unit_hierarchy) - 2)
to_index = random.randint(max(0, from_index - max_jump),
min(len(unit_hierarchy) - 1, from_index + max_jump))
# if from or to index is 2, get another one
if from_index == 2:
from_index += 1
if to_index == 2:
to_index += 1
if from_index == to_index:
# add one to to_index if it is not the last index and new index not 2
if to_index < len(unit_hierarchy) - 1 and to_index != 1:
to_index += 1
else:
to_index -= 1
# Ensure meaningful values for the question
if from_index < to_index: # Larger to smaller unit
number = random.randint(1, 100)
else: # Smaller to larger unit
number = random.randint(10, 1000)
from_unit = unit_hierarchy[from_index]
to_unit = unit_hierarchy[to_index]
correct_answer = calculate_conversion(number, from_unit, to_unit)
return f"{number} {from_unit} equals x {to_unit}", correct_answer
# Calculate conversion between two units
def calculate_conversion(number, from_unit, to_unit):
from_index = unit_hierarchy.index(from_unit)
to_index = unit_hierarchy.index(to_unit)
factor = 1
if from_index < to_index: # Larger to smaller
for i in range(from_index, to_index):
factor *= conversion_factors[i]
else: # Smaller to larger
for i in range(to_index, from_index):
factor *= conversion_factors[i]
factor = 1 / factor # Reciprocal for smaller to larger
return round(number * factor, 3)
def main():
print("Welcome to the Unit Conversion Quiz!")
try:
num_questions = int(input("How many questions would you like? "))
except ValueError:
print(Fore.RED + "Please enter a valid number." + Style.RESET_ALL)
return
correct_count = 0
for i in range(num_questions):
question, correct_answer = generate_question()
print(f"\nQuestion {i + 1}/{num_questions}: {question}")
attempt = 0
while attempt < 2: # Allow up to two attempts
try:
user_answer = float(input("Your answer: "))
if abs(user_answer - correct_answer) < 1e-5: # Handle floating-point precision
print(Fore.GREEN + "Correct!" + Style.RESET_ALL)
correct_count += 1
break
else:
attempt += 1
if attempt < 2:
print(Fore.YELLOW + "Wrong! Try again." + Style.RESET_ALL)
else:
print(Fore.RED + f"Wrong! The correct answer is {correct_answer}" + Style.RESET_ALL)
except ValueError:
print(Fore.RED + "Invalid input. Please enter a number." + Style.RESET_ALL)
attempt += 1
if attempt >= 2:
print(Fore.RED + f"The correct answer was {correct_answer}" + Style.RESET_ALL)
print(f"\nQuiz completed! You answered {correct_count}/{num_questions} correctly.")
if correct_count == num_questions:
print(Fore.GREEN + "Perfect score! Great job!" + Style.RESET_ALL)
elif correct_count > num_questions // 2:
print(Fore.YELLOW + "Good effort!" + Style.RESET_ALL)
else:
print(Fore.RED + "Better luck next time!" + Style.RESET_ALL)
if __name__ == "__main__":
main()