-
Notifications
You must be signed in to change notification settings - Fork 2
/
MATH----GAME ---USING---PYTHON.py
80 lines (67 loc) · 2.54 KB
/
MATH----GAME ---USING---PYTHON.py
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
79
80
import random
def calculate(operator, num1, num2):
if operator == '+':
return float(num1 + num2)
elif operator == '-':
return float(num1 - num2)
elif operator == '*':
return float(num1 * num2)
elif operator == '/':
return float(num1 / num2)
else:
return 'Invalid operator'
score = 0
while True:
# Generate two random numbers for the calculation
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
if score < 10:
# Only use + and - until score 10
operator = random.choice(['+', '-'])
elif score < 20:
# Use +, -, *, and / after score 10
operator = random.choice(['+', '-', '*', '/'])
elif score < 30:
# Combine two operators after score 20
operator1 = random.choice(['+', '-', '*', '/'])
operator2 = random.choice(['+', '-', '*', '/'])
operator = operator1 + operator2
elif score < 50:
# Combine three operators after score 30
operator1 = random.choice(['+', '-', '*', '/'])
operator2 = random.choice(['+', '-', '*', '/'])
operator3 = random.choice(['+', '-', '*', '/'])
operator = operator1 + operator2 + operator3
else:
# Use all four operators after score 50
operator1 = random.choice(['+', '-', '*', '/'])
operator2 = random.choice(['+', '-', '*', '/'])
operator3 = random.choice(['+', '-', '*', '/'])
operator4 = random.choice(['+', '-', '*', '/'])
operator = operator1 + operator2 + operator3 + operator4
result = calculate(operator, num1, num2)
# Check if the result is a whole number
if result.is_integer():
result = int(result)
else:
result = int(result) + 1
print("What is the calculation to get", result, "using the operator", operator, "?")
# Get the user's calculation and check if it's correct
calculation = input()
if "+0" in calculation or "-0" in calculation:
print("Please input a different calculation.")
continue
if "/1" in calculation and operator == '/':
print("Please input a different calculation. Try again.")
continue
if "*1" in calculation and operator == '*':
print("Please input a different calculation. Try again.")
continue
if eval(calculation) == result:
score += 1
print("""Correct
Your score =""", score)
else:
print("Game over")
print("Your final score is", score)
break