From 2373cdffe18d65c89d610b2b81752b5cf45b539d Mon Sep 17 00:00:00 2001 From: -Z- Date: Tue, 23 Aug 2022 22:50:01 +0200 Subject: [PATCH 1/4] 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() --- main.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 90bbbd2..57501b9 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,38 @@ import random import operator -def random_problem(): - operators = { +operators = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv, } +def operate(operation): num_1 = random.randint(1, 10) num_2 = random.randint(1, 10) + answer = operators.get(operation)(num_1, num_2) + return answer,num_1,num_2 + +def random_problem(): + + operation = random.choice(list(operators.keys())) - answer = operators.get(operation)(num_1, num_2) + + answer,num_1,num_2 = operate(operation) + print(f'What is {num_1} {operation} {num_2}') return answer def ask_question(): answer = int(random_problem()) - guess = float(input('Enter you answer: ')) + try: + guess = int(input('Enter you answer: ')) + except: + print('It is not an integer!') + return False + return guess == answer def game(): From 8b03c27cfd74fec7c299b7f112943623698fbf16 Mon Sep 17 00:00:00 2001 From: -Z- Date: Tue, 23 Aug 2022 23:52:33 +0200 Subject: [PATCH 2/4] truediv returns an answer which is int() not float() --- main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 57501b9..a90620a 100644 --- a/main.py +++ b/main.py @@ -12,15 +12,17 @@ def operate(operation): num_1 = random.randint(1, 10) num_2 = random.randint(1, 10) answer = operators.get(operation)(num_1, num_2) + return answer,num_1,num_2 def random_problem(): - - - + operation = random.choice(list(operators.keys())) - answer,num_1,num_2 = operate(operation) + + while (operation=='/' and num_1 Date: Wed, 24 Aug 2022 00:13:33 +0200 Subject: [PATCH 3/4] I got rid off code redundancy in a method operate() --- main.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index a90620a..a4128c9 100644 --- a/main.py +++ b/main.py @@ -8,10 +8,21 @@ '/': operator.truediv, } -def operate(operation): +def chooseNum(): num_1 = random.randint(1, 10) num_2 = random.randint(1, 10) - answer = operators.get(operation)(num_1, num_2) + return num_1,num_2 + +def operate(operation): + + pureInt=True + while pureInt: + num_1,num_2=chooseNum() + answer = operators.get(operation)(num_1, num_2) + if (operation=='/' and num_1 Date: Sat, 27 Aug 2022 11:40:18 +0200 Subject: [PATCH 4/4] This version implements an Issue 24.8.22 improvements --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index a4128c9..e3ba26d 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,6 @@ +#This version implements an Issue 24.8.22 improvements + + import random import operator @@ -13,8 +16,9 @@ def chooseNum(): num_2 = random.randint(1, 10) return num_1,num_2 + def operate(operation): - + #The operation operator.truediv must compute only integers pureInt=True while pureInt: num_1,num_2=chooseNum() @@ -35,6 +39,7 @@ def random_problem(): print(f'What is {num_1} {operation} {num_2}') return answer +# The answer and quess are both int() type def ask_question(): answer = int(random_problem()) try: