diff --git a/HW2_Senkovenko/README.md b/HW2_Senkovenko/README.md new file mode 100644 index 0000000..5ac06dc --- /dev/null +++ b/HW2_Senkovenko/README.md @@ -0,0 +1,95 @@ +# Калькулятор / Calculator + +#### _**HomeWork №2.**_ + + +## Description + +The calculator is written in Python and performs basic arithmetic operations. + +Function | Description | Contributor +----------------|------------|---------- +main()| program loop, input/output, error handling | Alexey Senkovenko (lead) +addition(val1, val2) | val1 + val2 | Alisa Senko +subtraction(val1, val2)| val1 - val2 | Aglaya Tsidulko +division(val1, val2) | val1 / val2 | Anastasiia Kolos +multiplication(val1, val2)| val1 * val2 | Elizaveta Menshikova + +README.md - main text by Alisa Senko, en-translation by Alexey Senkovenko. + +## How to run the script +The script is launched from the console. + +1. Clone the repository: +``` +git clone +``` +2. Go to the project folder and run the script: +``` +python3 calculator.py +``` +or +``` +python calculator.py +``` +depending on your OS. + +### _Example_ +``` +> python calculator.py +Hello! +Type exit to finish calculations +Enter your expression: +> 3 * 15 +Result: 45.0 +Enter your expression: +> 3 / 0 +Math Error: Cannot divide by zero +Enter your expression: +> 3 + 15.4 +Result: 18.4 +Enter your expression: +> 3.0 + 3.9 +Result: 6.9 +``` +### _Error handling:_ +* Input-check that exactly 3 elements are given, separated by a space - two numbers and an operator: +``` +Enter your expression: +> 2 + +Value Error: Incorrect number of values, check your input: value1 operator value2 +Enter your expression: +> 2+3 +Value Error: Incorrect number of values, check your input: value1 operator value2 +``` +* Division by zero: +``` +Enter your expression: +> 3 / 0 +Math Error: Cannot divide by zero +``` +* Check for the correct input format: +``` +Enter your expression: +> 3,5 + 3,5 +Value Error: could not convert string to float: '3,5' +Enter your expression: +> 4.0 ++ 5 +Key Error: "Used unknown operator, supported operators are: ('+', '-', '*', '/')" +Enter your expression: +> - - 3 +Value Error: could not convert string to float: '-' +``` +* Unexpected Error + +### _How to end the calculations_ +To finish your work, type `exit` in the input field : +``` +Enter your expression: +> exit +Program closed +``` + +## ✨✨Our team!✨✨ + +![alt text](imgs_Senkovenko/photo_2025-09-18_20-48-29.jpg) diff --git a/HW2_Senkovenko/calculator.py b/HW2_Senkovenko/calculator.py new file mode 100644 index 0000000..b0cbb9a --- /dev/null +++ b/HW2_Senkovenko/calculator.py @@ -0,0 +1,60 @@ +def subtraction(val1, val2): + return val1 - val2 + +def addition(val1, val2): + return val1 + val2 + +def multiplication(val1, val2): + return val1 * val2 + +def division(val1, val2): + if val2 == 0: + raise ZeroDivisionError("Cannot divide by zero") + return val1 / val2 + + +operators = { + '+': addition, + '-': subtraction, + '*': multiplication, + '/': division +} + +def main(): + print("Hello! \nType exit to finish calculations") + + while True: + check = input("Enter your expression: \n") + + if "exit" in check: + print("Program closed") + break + + try: + line = check.split() + + if len(line) != 3: + raise ValueError("Incorrect number of values, check your input: value1 operator value2") + + val1 = float(line[0]) + oper = line[1] + val2 = float(line[2]) + + if oper not in operators: + raise KeyError (f"Used unknown operator, supported operators are: {tuple(operators.keys())}") + + result = operators[oper](val1, val2) + + print(f"Result: {result}") + + except ValueError as valerr: + print(f"Value Error: {valerr}") + except KeyError as keyerr: + print(f"Key Error: {keyerr}") + except ZeroDivisionError as zeroerr: + print(f"Math Error: {zeroerr}") + except Exception as exc: + print(f"Unexpected Error: {exc}") + +if __name__ == "__main__": + main() diff --git a/HW2_Senkovenko/imgs_Senkovenko/photo_2025-09-18_20-48-29.jpg b/HW2_Senkovenko/imgs_Senkovenko/photo_2025-09-18_20-48-29.jpg new file mode 100644 index 0000000..8dac468 Binary files /dev/null and b/HW2_Senkovenko/imgs_Senkovenko/photo_2025-09-18_20-48-29.jpg differ