commit 552488d4c653f4f2f7801b50eeffebbc8504ebe2 Author: Casper Date: Thu Dec 5 13:36:59 2024 +0100 first diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..a16a385 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bc394fd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e15ec35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pythonProject.iml b/.idea/pythonProject.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/pythonProject.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0fb800a --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +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() \ No newline at end of file