diff --git a/README_POPOV.md b/README_POPOV.md new file mode 100644 index 0000000..061688b --- /dev/null +++ b/README_POPOV.md @@ -0,0 +1,42 @@ +# Calculator + +This readme describes mini-program Calculator you can find here. + +Calculator can calculate easy arythmetical functions: +* Addition +* Subtraction +* Multiplication +* Division + +## How it works + +1. Clone this repo using SSH or HTTPS: + +```bash +git clone git@github.com:YuryPopov/HW2_Git_and_python.git +``` +**or** +```bash +git clone https://github.com/YuryPopov/HW2_Git_and_python.git +``` +2. Launch application (I hope, that you are inside app folder): +```bash +python3 ./calculator.py +``` +3. After prompt (*Enter expression:*) enter your expression (numbers and arythmetical operator divided with spaces) and press *"Enter"* button. +***Pay attention***, programm works with integrer and float numbers and only with +, -, *, / operators. +**Enter expression like +``` +1 + 3 +``` +4. Enjoy the results. + + +## Your developers: +* Yury Popov +* Kristina Zhur +* Maria Uzun +* Anastasia Shtompel +* Alina Potyseva + +![Our team](/images/picture.jpg "Our team") \ No newline at end of file diff --git a/calculator.py b/calculator.py new file mode 100644 index 0000000..c8a5be7 --- /dev/null +++ b/calculator.py @@ -0,0 +1,64 @@ +def substract(a, b): + return a - b + + +def divide(a, b): + if b == 0: + return "Error: division by zero" + return a / b + + +def multiplication(a, b): + answer = a * b + return answer + + +def summing(a, b): + return a + b + + +list_of_operators = ['/', '*', '-', '+'] + + +def main(keyboard_input: str) -> float: + ''' + Function checks operators, transform numbers to float, + call math function described above (substract, divide, + multiplication, summing) and return result. If operators + are not in list or numbers cannot transform into floats, + func returns error message. + ''' + math_sign, operator_position = False, False + + for char in range(len(keyboard_input)): + if keyboard_input[char] in list_of_operators: + math_sign = keyboard_input[char] + operator_position = char + + if math_sign == False: + return 'Enter valid expression' + + num1 = keyboard_input[:operator_position].strip() + num2 = keyboard_input[operator_position + 1:].strip() + + try: + num1 = float(num1) + num2 = float(num2) + if math_sign == '/': + return divide(num1, num2) + + elif math_sign == '-': + return substract(num1, num2) + + elif math_sign == '+': + return summing(num1, num2) + + elif math_sign == '*': + return multiplication(num1, num2) + + except ValueError: + return 'Enter valid expression' + + +if __name__ == '__main__': + print(main(input('Enter expression:'))) diff --git a/images/picture.jpg b/images/picture.jpg new file mode 100644 index 0000000..4970e37 Binary files /dev/null and b/images/picture.jpg differ diff --git a/images/picture.jpg:Zone.Identifier b/images/picture.jpg:Zone.Identifier new file mode 100644 index 0000000..289271c --- /dev/null +++ b/images/picture.jpg:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://web.telegram.org/