Skip to content

Commit f6e77d9

Browse files
committed
Create Math Game Using Python
Create Math Game Using Python
1 parent 3701bbc commit f6e77d9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

main.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import random
2+
import operator
3+
4+
def random_problem():
5+
operators = {
6+
'+': operator.add,
7+
'-': operator.sub,
8+
'*': operator.mul,
9+
'/': operator.truediv,
10+
}
11+
12+
num_1 = random.randint(1, 10)
13+
num_2 = random.randint(1, 10)
14+
15+
operation = random.choice(list(operators.keys()))
16+
answer = operators.get(operation)(num_1, num_2)
17+
print(f'What is {num_1} {operation} {num_2}')
18+
return answer
19+
20+
def ask_question():
21+
answer = int(random_problem())
22+
guess = float(input('Enter you answer: '))
23+
return guess == answer
24+
25+
def game():
26+
score = 0
27+
while True:
28+
if ask_question() == True:
29+
score += 1
30+
print('Correct !')
31+
else:
32+
print('Incorrect')
33+
break
34+
print(f'======== Game Over ========\nYou score is {score}\nKepp going!')
35+
36+
game()

0 commit comments

Comments
 (0)