From cac0f876cd1df8434e91f6f8b6c7c2573983d31d Mon Sep 17 00:00:00 2001 From: Alex Kamano Date: Mon, 18 Nov 2024 12:04:37 +0100 Subject: [PATCH 1/2] refactor --- .idea/misc.xml | 2 +- .idea/setupCheck.iml | 2 +- src/main.py | 28 +++++++++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 36e7663..232b71f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/setupCheck.iml b/.idea/setupCheck.iml index 7584dbd..b6731d8 100644 --- a/.idea/setupCheck.iml +++ b/.idea/setupCheck.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/src/main.py b/src/main.py index 47ce342..f201ff8 100644 --- a/src/main.py +++ b/src/main.py @@ -1,13 +1,9 @@ +operand1 = None +operator = None +operand2 = None def main(): - # Get first operand from the user - operand1 = float(input("Enter the first operand: ")) - - # Get the operator from the user - operator = input("Enter an operator (+, -, *, /): ") - - # Get second operand from the user - operand2 = float(input("Enter the second operand: ")) + ask_user_input() # Perform the operation based on the operator if operator == '+': result = operand1 + operand2 @@ -27,6 +23,20 @@ def main(): # Print the result print("Result:", result) +def ask_user_input(): + global operand1 + # Get first operand from the user + operand1 = float(input("Enter the first operand: ")) + + global operator + # Get the operator from the user + operator = input("Enter an operator (+, -, *, /): ") + + global operand2 + # Get second operand from the user + operand2 = float(input("Enter the second operand: ")) # Call the main function to run the program -main() \ No newline at end of file +main() + + From 00399de84331e8559c4141abff920f8d1ab6ab95 Mon Sep 17 00:00:00 2001 From: Alex Kamano Date: Fri, 22 Nov 2024 12:16:05 +0100 Subject: [PATCH 2/2] refractor : etape 2 sur git book --- src/function.py | 48 ++++++++++++++++++++++++++++++++++++++++ src/main.py | 58 ++++++++++++++++++++++++++++++------------------- 2 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 src/function.py diff --git a/src/function.py b/src/function.py new file mode 100644 index 0000000..d31a5a0 --- /dev/null +++ b/src/function.py @@ -0,0 +1,48 @@ +from main import main + +def ask_user_input(): + + global operand1 + # Get first operand from the user + operand1 = float(input("Enter the first operand: ")) + + global operator + # Get the operator from the user + operator = input("Enter an operator (+, -, *, /): ") + + global operand2 + # Get second operand from the user + operand2 = float(input("Enter the second operand: ")) + + return operand1, operator, operand2 + +def calculate(ope1, oper, ope2): + # Perform the operation based on the operator + res = None + match operator: + case '+': + res = (operand1 + operand2) + + case '-': + res = (operand1 - operand2) + + case '*': + res = (operand1 * operand2) + + case '/': + if ope2 == 0: + print("Error: Division by zero is undefined.") + return + res = (ope1 / ope2) + + case _: + print("Error: Operator '{}' not recognized.".format(operator)) + return + + return res + +def display_result(ope1, oper, ope2, res): + print(f"{ope1} {oper} {ope2}={res}") + + + diff --git a/src/main.py b/src/main.py index f201ff8..eb8dc60 100644 --- a/src/main.py +++ b/src/main.py @@ -1,29 +1,13 @@ -operand1 = None -operator = None -operand2 = None -def main(): - ask_user_input() - # Perform the operation based on the operator - if operator == '+': - result = operand1 + operand2 - elif operator == '-': - result = operand1 - operand2 - elif operator == '*': - result = operand1 * operand2 - elif operator == '/': - if operand2 == 0: - print("Error: Division by zero is undefined.") - return - result = operand1 / operand2 - else: - print("Invalid operator.") - return - # Print the result - print("Result:", result) +def main(): + ask_user_input() + global result + result = calculate(operand1, operator, operand2) + display_result(operand1, operator, operand2, result) def ask_user_input(): + global operand1 # Get first operand from the user operand1 = float(input("Enter the first operand: ")) @@ -36,6 +20,36 @@ def ask_user_input(): # Get second operand from the user operand2 = float(input("Enter the second operand: ")) + return operand1, operator, operand2 + +def calculate(ope1, oper, ope2): + # Perform the operation based on the operator + res = None + match operator: + case '+': + res = (operand1 + operand2) + + case '-': + res = (operand1 - operand2) + + case '*': + res = (operand1 * operand2) + + case '/': + if ope2 == 0: + print("Error: Division by zero is undefined.") + return + res = (ope1 / ope2) + + case _: + print("Error: Operator '{}' not recognized.".format(operator)) + return + + return res + +def display_result(ope1, oper, ope2, res): + print(f"{ope1}' '{oper}' '{ope2}'='{res}") + # Call the main function to run the program main()