From c26cd29624f1a24cf790c0f38dbe7d86e7154ce0 Mon Sep 17 00:00:00 2001 From: Mihir008 <91221854+Mihir008@users.noreply.github.com> Date: Wed, 2 Oct 2024 20:44:18 +0530 Subject: [PATCH] calculator in python made an easy calculator in python where we can add,substract,divide and multiply --- calculator.ipynb | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 calculator.ipynb diff --git a/calculator.ipynb b/calculator.ipynb new file mode 100644 index 0000000..0c25005 --- /dev/null +++ b/calculator.ipynb @@ -0,0 +1,102 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Select operation.\n", + "1.Add\n", + "2.Subtract\n", + "3.Multiply\n", + "4.Divide\n" + ] + } + ], + "source": [ + "def add(x, y):\n", + " return x + y\n", + "\n", + "# This function subtracts two numbers\n", + "def subtract(x, y):\n", + " return x - y\n", + "\n", + "# This function multiplies two numbers\n", + "def multiply(x, y):\n", + " return x * y\n", + "\n", + "# This function divides two numbers\n", + "def divide(x, y):\n", + " return x / y\n", + "\n", + "\n", + "print(\"Select operation.\")\n", + "print(\"1.Add\")\n", + "print(\"2.Subtract\")\n", + "print(\"3.Multiply\")\n", + "print(\"4.Divide\")\n", + "\n", + "while True:\n", + " # take input from the user\n", + " choice = input(\"Enter choice(1/2/3/4): \")\n", + "\n", + " # check if choice is one of the four options\n", + " if choice in ('1', '2', '3', '4'):\n", + " num1 = float(input(\"Enter first number: \"))\n", + " num2 = float(input(\"Enter second number: \"))\n", + "\n", + " if choice == '1':\n", + " print(num1, \"+\", num2, \"=\", add(num1, num2))\n", + "\n", + " elif choice == '2':\n", + " print(num1, \"-\", num2, \"=\", subtract(num1, num2))\n", + "\n", + " elif choice == '3':\n", + " print(num1, \"*\", num2, \"=\", multiply(num1, num2))\n", + "\n", + " elif choice == '4':\n", + " print(num1, \"/\", num2, \"=\", divide(num1, num2))\n", + " \n", + " # check if user wants another calculation\n", + " # break the while loop if answer is no\n", + " next_calculation = input(\"Let's do next calculation? (yes/no): \")\n", + " if next_calculation == \"no\":\n", + " break\n", + " \n", + " else:\n", + " print(\"Invalid Input\")\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.7 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "be195e74ed175410d6540ba025da5c4406bdff32be56f2bbb5032c9081165736" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}