diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c2d52b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/* diff --git a/HW2_nekrasova/README.md b/HW2_nekrasova/README.md new file mode 100644 index 0000000..3e5bacd --- /dev/null +++ b/HW2_nekrasova/README.md @@ -0,0 +1,21 @@ +# Basic calculator +*This repo is for the 2nd homework of Python 2023 course* + +### Calculator description +This branch (HW2_nekrasova) contains calculator.py script designed to operate with two numbers via one of the following basic mathematical functions: addition, subtraction, multiplication, division. +Right after start script waits for the user to enter a mathematical expression like '2 * 2'. Expression is then splitted by spaces and calculated corresponding to function according to the specified operator. +The script handles both int and float types of numbers. It also handles devision by 0. + +### Installation +Calculator is a straight forward script that does not need any prerequisites apart from installed Python 3. To get started use your CLI and execute the following commandlet: `python3 calculator.py`. +After this the script is ready to recieve your mathematical expression. + +### Development +This project is leaded by nekrasovadasha22 with contributions from the following people: +- grishchenkoira +- anisssum +- NSapozhnikov +- Komarov I. + +Feel free to report any bugs encountered to nekrasovadasha22. +![photo_2023-09-13_19-43-30](https://github.com/nekrasovadasha22/HW2_Git_and_python/assets/144455860/d11f2906-21e0-4eda-9849-b58d64b312c8) diff --git a/HW2_nekrasova/calculator.py b/HW2_nekrasova/calculator.py new file mode 100644 index 0000000..cf4115f --- /dev/null +++ b/HW2_nekrasova/calculator.py @@ -0,0 +1,62 @@ +def main(expression: str): + expression_array = expression.split(' ') + array_of_operands = [] + + try: + left_int = int(expression_array[0]) + array_of_operands.append(left_int) + except: + left_float = float(expression_array[0]) + array_of_operands.append(left_float) + + + try: + right_int = int(expression_array[2]) + array_of_operands.append(right_int) + except: + right_float = float(expression_array[2]) + array_of_operands.append(right_float) + + answer = None + + if expression_array[1] == '+': + answer = add(array_of_operands) + + elif expression_array[1] == '-': + answer = substruct(array_of_operands) + + elif expression_array[1] == '*': + answer = multiple(array_of_operands) + elif expression_array[1] == '/': + answer = divide(array_of_operands) + + print(answer) + + +def add(arr_of_operands: list): + return arr_of_operands[0]+arr_of_operands[1] + +def substruct(arr_of_operands: list): + # substruct + return arr_of_operands[0]-arr_of_operands[1] + + +def multiple(arr_of_operands: list): + answer = arr_of_operands[0] * arr_of_operands[1] + return answer + + +def divide(arr_of_operands: list): + if arr_of_operands[1] != 0: + answer = arr_of_operands[0]/arr_of_operands[1] + return answer + else: + answer = 'Деление на 0!' + return answer + + + + +current_expression = input() +main(current_expression) +