diff --git a/Math game/README.md b/Math game/README.md new file mode 100644 index 00000000..6388bb24 --- /dev/null +++ b/Math game/README.md @@ -0,0 +1,41 @@ +# Math Quiz Game + +This is a simple Math Quiz Game built with Python, using the `easygui` library for a graphical user interface. The game generates random math problems for the user to solve, and it keeps track of the score and time taken to complete the problems. + +## Features + +- Randomly generated math problems using basic operators: addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and modulo (`%`). +- Five problems per game, with random numbers between 1 and 10. +- User-friendly interface with `easygui` dialogs for input and results. +- Tracks the user's score and total time taken to complete the quiz. +- Handles special cases like division by zero and invalid input. + +## Prerequisites + +- Python 3.x +- `easygui` library + +## Installation + +1. **Clone the repository**: +    git clone https://github.com/yourusername/math-quiz-game.git +2. **Navigate to the project directory**: +    cd math-quiz-game +3. **Install the required dependencies: You need to have the easygui library installed. If it's not already installed, you can install it using**: +    pip install easygui + +## How to Run + +1. **Run the scrip**: +    python math_quiz_game.py +2. Follow the prompts in the easygui dialogs to answer the math problems. +3. At the end of the game, your score and the time taken will be displayed. + +## Game Rules + +1. The game consists of 5 math problems. +2. Problems are generated randomly with basic math operators. +3. You need to enter the correct numeric answer to score a point. +4. The final score is displayed at the end, along with the total time taken. + +## GOOD LUCK! diff --git a/Math game/game.py b/Math game/game.py new file mode 100644 index 00000000..cd438d4e --- /dev/null +++ b/Math game/game.py @@ -0,0 +1,69 @@ +import random +import time +import easygui + +# Initialize the score and start time +score = 0 +start_time = time.time() + +# List of operators to be used in the problems +operators = ['+', '-', '/', '%', '*'] + +# Loop to generate 5 problems for the user +for i in range(1, 6): + # Generate two random numbers for the math problem + first_number = random.randint(1, 10) + second_number = random.randint(1, 10) + + # Randomly select an operator + selected_operator = random.choice(operators) + + # Create the problem message for the user + message = f"Problem {i} -> {first_number} {selected_operator} {second_number}" + + # Get the user's answer through an input dialog + user_answer = easygui.enterbox(message, "Enter your answer:") + + # If the user cancels the dialog, exit the loop early + if user_answer is None: + easygui.msgbox("Game cancelled.", "Exit") + break + + # Calculate the correct result based on the selected operator + if selected_operator == '+': + result = first_number + second_number + elif selected_operator == '-': + result = first_number - second_number + elif selected_operator == '/': + # Avoid division by zero, if second_number is zero, skip this iteration + if second_number == 0: + easygui.msgbox("Skipping division by zero problem.", "Warning") + continue + result = round(first_number / second_number, 1) + elif selected_operator == '%': + # Avoid modulo by zero, if second_number is zero, skip this iteration + if second_number == 0: + easygui.msgbox("Skipping modulo by zero problem.", "Warning") + continue + result = first_number % second_number + elif selected_operator == '*': + result = first_number * second_number + + # Check if the user's answer matches the correct result + try: + # Use float comparison for cases with division results + if float(user_answer) == result: + score += 1 + else: + easygui.msgbox(f"Wrong answer! The correct answer is {result}.", "Incorrect") + except ValueError: + # If the user's input isn't a number, notify the user + easygui.msgbox("Invalid input! Please enter a numeric answer.", "Error") + +# Calculate the total time taken to complete the problems +end_time = time.time() +final_time = round(end_time - start_time, 2) + +# Display the final score and time taken to the user +result_message = f"You scored {score} points in {final_time} seconds." +easygui.msgbox(result_message, "Result")