-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmath_game.py
More file actions
78 lines (65 loc) · 2.06 KB
/
Copy pathmath_game.py
File metadata and controls
78 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
import operator
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.')
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(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
for q in range(1, total + 1):
print(f'Question {q} of {total}')
if ask_question(settings):
score += 1
print('Correct!')
else:
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()