diff --git a/Math_Game/README.md b/Math_Game/README.md
index f27c2847..f42d30ff 100644
--- a/Math_Game/README.md
+++ b/Math_Game/README.md
@@ -7,8 +7,33 @@ It's just a simple math game. Improve your math skills
## How to run the script
`python math_game.py`
+## How to play
+1. Choose a difficulty level: `easy`, `medium`, or `hard`
+2. Choose how many questions you want to answer
+3. Type your answer for each math question
+4. See your final score and percentage at the end
+
+## Difficulty levels
+| Level | Number Range | Operations |
+|--------|--------------|----------------|
+| Easy | 1 – 10 | `+` and `-` |
+| Medium | 1 – 20 | `+` `-` `*` `/`|
+| Hard | 1 – 50 | `+` `-` `*` `/`|
+
+> Division questions always produce whole number answers.
+
+## Changes from original
+- Added difficulty levels (easy, medium, hard)
+- Added question limit — choose how many questions per game
+- Added final score percentage at the end
+- Fixed division to always produce clean whole number answers
+- Simplified input error handling
+
## Screenshot

## *Author Name*
-[`Pargorn Ruasijan (xNewz)`](https://github.com/xNewz)
\ No newline at end of file
+[`Pargorn Ruasijan (xNewz)`](https://github.com/xNewz)
+
+## *Contributor Name*
+[`Megha Chandrashaker`](https://github.com/MChand20656789)
\ No newline at end of file
diff --git a/Math_Game/math_game.py b/Math_Game/math_game.py
index 7e863ecf..914847d9 100644
--- a/Math_Game/math_game.py
+++ b/Math_Game/math_game.py
@@ -1,51 +1,78 @@
import random
import operator
-def errorHandle(problem_answer):
- switch = False
- validated_guess = 0.0
- while switch == False:
- try:
- validated_guess = float(input('Please enter a valid answer: '))
- if type(validated_guess) is float:
- switch = True
- break
- except ValueError:
- pass
- return validated_guess
-
-def random_problem():
- operators = {
+OPERATORS = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
- }
+}
+
+DIFFICULTY_SETTINGS = {
+ 'easy': {'range': (1, 10), 'ops': ['+', '-']},
+ 'medium': {'range': (1, 20), 'ops': ['+', '-', '*', '/']},
+ 'hard': {'range': (1, 50), 'ops': ['+', '-', '*', '/']},
+}
+
+def get_valid_input(prompt):
+ while True:
+ try:
+ return float(input(prompt))
+ except ValueError:
+ print('Invalid input — please enter a number.')
- num_1 = random.randint(1, 10)
- num_2 = random.randint(1, 10)
- operation = random.choice(list(operators.keys()))
- answer = float(round(operators.get(operation)(num_1, num_2),3))
- print(f'What is {num_1} {operation} {num_2}')
+def get_difficulty():
+ while True:
+ choice = input('Choose difficulty (easy / medium / hard): ').strip().lower()
+ if choice in DIFFICULTY_SETTINGS:
+ return DIFFICULTY_SETTINGS[choice]
+ print('Please enter easy, medium, or hard.')
+
+def random_problem(settings):
+ low, high = settings['range']
+ operation = random.choice(settings['ops'])
+ num_1 = random.randint(low, high)
+
+ if operation == '/':
+ # Ensure clean division — no ugly decimals
+ num_2 = random.randint(1, num_1)
+ while num_1 % num_2 != 0:
+ num_2 = random.randint(1, num_1)
+ else:
+ num_2 = random.randint(low, high)
+
+ answer = round(OPERATORS[operation](num_1, num_2), 3)
+ print(f'\nWhat is {num_1} {operation} {num_2}?')
return answer
-def ask_question():
- answer = random_problem()
- try:
- guess = float(input('Enter you answer: '))
- except ValueError:
- guess = errorHandle(answer)
+def ask_question(settings):
+ answer = random_problem(settings)
+ guess = get_valid_input('Your answer: ')
return guess == answer
def game():
+ print('=== Math Game ===')
+ settings = get_difficulty()
+
+ total = int(input('How many questions? '))
score = 0
- while True:
- if ask_question() == True:
+
+ for q in range(1, total + 1):
+ print(f'Question {q} of {total}')
+ if ask_question(settings):
score += 1
- print('Correct !')
+ print('Correct!')
else:
- print('Incorrect')
- break
- print(f'======== Game Over ========\nYou score is {score}\nKeep going!')
+ print('Incorrect.')
+
+ pct = round((score / total) * 100)
+ print(f'\n======== Game Over ========')
+ print(f'Score: {score}/{total} ({pct}%)')
+ if pct == 100:
+ print('Perfect score!')
+ elif pct >= 70:
+ print('Nice work!')
+ else:
+ print('Keep practicing!')
-game()
+game()
\ No newline at end of file