Skip to content

Commit 5f61a58

Browse files
author
vdz
committed
update brain_calc
1 parent 2906557 commit 5f61a58

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

brain_games/games/brain_calc.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@
88
def calc():
99
print('What is the result of the expression?')
1010
count = 0
11+
def add(num1, num2):
12+
return num1 + num2
13+
14+
def sub(num1, num2):
15+
return num1 - num2
16+
17+
def mul(num1, num2):
18+
return num1 * num2
19+
1120
operators = {
12-
operator.add: '+',
13-
operator.sub: '-',
14-
operator.mul: '*',
15-
}
21+
'+': add,
22+
'-': sub,
23+
'*': mul,
24+
}
1625

1726
while count < 3:
18-
rand_num1, rand_num2 = random.randint(1, 100), random.randint(1, 100)
27+
num1, num2 = random.randint(1, 100), random.randint(1, 100)
1928
rand_operator = random.choice(list(operators))
2029
symbol = operators[rand_operator]
21-
print(f'Question: {rand_num1} {symbol} {rand_num2}')
22-
correct_answer = rand_operator(rand_num1, rand_num2)
30+
print(f'Question: {num1} {rand_operator} {num2}')
31+
correct_answer = symbol(num1, num2)
2332
user_answer = prompt.string('Your answer: ')
2433

2534
if int(user_answer) == correct_answer:

0 commit comments

Comments
 (0)