Skip to content

Commit 2373cdf

Browse files
committed
The following problems were fixed:
1) guess is the same type as answer int() 2) error handlig of a wrong input. The programm accepts only integers 3) optimalization of the function random_problem()
1 parent 823e15b commit 2373cdf

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

main.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
import random
22
import operator
33

4-
def random_problem():
5-
operators = {
4+
operators = {
65
'+': operator.add,
76
'-': operator.sub,
87
'*': operator.mul,
98
'/': operator.truediv,
109
}
1110

11+
def operate(operation):
1212
num_1 = random.randint(1, 10)
1313
num_2 = random.randint(1, 10)
14+
answer = operators.get(operation)(num_1, num_2)
15+
return answer,num_1,num_2
16+
17+
def random_problem():
18+
1419

20+
1521
operation = random.choice(list(operators.keys()))
16-
answer = operators.get(operation)(num_1, num_2)
22+
23+
answer,num_1,num_2 = operate(operation)
24+
1725
print(f'What is {num_1} {operation} {num_2}')
1826
return answer
1927

2028
def ask_question():
2129
answer = int(random_problem())
22-
guess = float(input('Enter you answer: '))
30+
try:
31+
guess = int(input('Enter you answer: '))
32+
except:
33+
print('It is not an integer!')
34+
return False
35+
2336
return guess == answer
2437

2538
def game():

0 commit comments

Comments
 (0)