commit 8b3f69ca74c0eefd8e4a3c2c006452d25b74589b Author: Casper Date: Mon Dec 9 18:57:08 2024 +0100 first commit 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/mathematics.iml b/.idea/mathematics.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/mathematics.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..39277c7 --- /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..d684693 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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..6304cf1 --- /dev/null +++ b/main.py @@ -0,0 +1,72 @@ +import random +from colorama import Fore, Style + + +def generate_arithmetic_question(max_digits=2, num_terms=2, operation=None): + """ + Generates an arithmetic question based on the configured digits, number of terms, and operation. + Ensures all results are positive. + """ + if operation is None: + operation = random.choice(["add", "sub"]) # Randomly choose addition or subtraction + + terms = [random.randint(10 ** (max_digits - 1), 10 ** max_digits - 1) for _ in range(num_terms)] + + if operation == "add": + question = " + ".join(map(str, terms)) + correct_answer = sum(terms) + elif operation == "sub": + terms.sort(reverse=True) # Ensure subtraction produces a positive result + question = " - ".join(map(str, terms)) + correct_answer = terms[0] + for term in terms[1:]: + correct_answer -= term + + return f"{question} =", correct_answer + + +def quiz(max_digits=2, num_terms=2, operation=None): + """ + Arithmetic practice quiz. + Configurables: + - max_digits: Maximum digits for each term in the problem. + - num_terms: Number of terms in the arithmetic problem. + - operation: 'add', 'sub', or None for random. + """ + print(f"Welcome to the Arithmetic Practice Quiz!") + total_questions = int(input("How many questions would you like to solve? ")) + print(f"Operation mode: {operation.upper() if operation else 'RANDOM'}") + correct_count = 0 + + for question_num in range(1, total_questions + 1): + question, correct_answer = generate_arithmetic_question(max_digits, num_terms, operation) + print(f"Question {question_num}/{total_questions}: {question}") + + attempt = 0 + while attempt < 2: + try: + user_answer = int(input("Your answer: ")) + if user_answer == correct_answer: + 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 + "Please enter a valid number." + Style.RESET_ALL) + + print(f"\nYou got {correct_count}/{total_questions} correct. Thanks for playing!") + + +# Run the quiz +if __name__ == "__main__": + # Adjust default parameters if needed + quiz( + max_digits=2, # Max digits per term + num_terms=2, # Number of terms in the problem + operation=None # Operation: 'add', 'sub', or None for random + )