Skip to content

Commit 07f1917

Browse files
committed
I got rid off code redundancy in a method operate()
1 parent 8b03c27 commit 07f1917

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

main.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@
88
'/': operator.truediv,
99
}
1010

11-
def operate(operation):
11+
def chooseNum():
1212
num_1 = random.randint(1, 10)
1313
num_2 = random.randint(1, 10)
14-
answer = operators.get(operation)(num_1, num_2)
14+
return num_1,num_2
15+
16+
def operate(operation):
17+
18+
pureInt=True
19+
while pureInt:
20+
num_1,num_2=chooseNum()
21+
answer = operators.get(operation)(num_1, num_2)
22+
if (operation=='/' and num_1<num_2) or (operation=='/' and answer!=num_1//num_2):
23+
pureInt=True
24+
else:
25+
pureInt=False
1526

1627
return answer,num_1,num_2
1728

@@ -20,10 +31,7 @@ def random_problem():
2031
operation = random.choice(list(operators.keys()))
2132
answer,num_1,num_2 = operate(operation)
2233

23-
while (operation=='/' and num_1<num_2) or (operation=='/' and answer!=num_1//num_2):
24-
print(str(answer))
25-
answer,num_1,num_2 = operate(operation)
26-
34+
2735
print(f'What is {num_1} {operation} {num_2}')
2836
return answer
2937

0 commit comments

Comments
 (0)