diff --git a/recipes/Python/578599_Calculator/README.md b/recipes/Python/578599_Calculator/README.md index 0905846d4..75cc6a08e 100644 --- a/recipes/Python/578599_Calculator/README.md +++ b/recipes/Python/578599_Calculator/README.md @@ -1,6 +1,29 @@ -## Calculator -Originally published: 2013-07-10 17:28:17 -Last updated: 2013-07-10 17:28:17 +## Calculator +Version 1 +Originally published: 2013-07-10 17:28:17 Author: superducktoxic -Put any numbers in and the program will give you the answer. \ No newline at end of file +Put any numbers in and the program will give you the answer. + +Version2 +Latest published: 2025-11-30 +Author: manasa + +Project Title: Simple Calculator + +Filename: recipe-578599_v2.py + +Description: This Python script provides a command-line interface for performing basic arithmetic operations. + +Features: +Supports addition (+), subtraction (-), multiplication (*), division (/), remainder (%), and power (**) operations. + +Allows continuous calculations using the previous result as the first number. +Includes input validation for operators. + +Usage: +Run the script from the terminal: python3 recipe-578599_v2.py +Follow the prompts to enter numbers and operators. +Enter 'c' to continue with the previous result or 'q' to exit the application. + +Requirements: Python 3 interpreter. diff --git a/recipes/Python/578599_Calculator/recipe-578599_v2.py b/recipes/Python/578599_Calculator/recipe-578599_v2.py new file mode 100644 index 000000000..81efba01b --- /dev/null +++ b/recipes/Python/578599_Calculator/recipe-578599_v2.py @@ -0,0 +1,58 @@ +#CALCULATOR PROGRAM + +print("\nCALCULATOR\n") +print("List of operators:", "Add:+ ", "Substract:- ", "Multiplication:* ", "Division:/ ", "Remainder:% ", "Power:** \n") +num1 = int(input("Enter your first number: ")) +output1 = num1 +operator = input("Enter the operator: ") +num2 = int(input("Enter your second number: ")) + +def calculator (num1, num2): + if (operator != '+' and operator != '-' and operator != '*' and operator != '/' and operator != '%' and operator != '**'): + print("Enter a valid operator\n") + num1 = output1 + return num1 + if (operator == '+'): + print(f"{num1} + {num2} = {num1 + num2}\n") + return num1 + num2 + if (operator == '-'): + print(f"{num1} - {num2} = {num1 - num2}\n") + return num1 - num2 + if (operator == '*'): + print(f"{num1} x {num2} = {num1 * num2}\n") + return num1 * num2 + if (operator == '/'): + if (num2 == 0): + print(f"{num1} cannot be divided with {num2}.\n") + num1 = output1 + return num1 + print(f"{num1} / {num2} = {num1 / num2}\n") + return num1 / num2 + if (operator == '%'): + if (num2 == 0): + print(f"{num1} cannot be divided with {num2}.\n") + num1 = output1 + return num1 + print(f"Remainder: {num1} % {num2} = {num1 % num2}\n") + return num1 % num2 + if (operator == '**'): + print(f"{num1} power {num2} = {num1 ** num2}\n") + return num1 ** num2 + +output1 = calculator (num1, num2) + +def result_func (): + new_result = input("To exit enter 'q'. To continue enter 'c'. your response: ") + return new_result + +result = result_func() + +while (result == 'c'): + if (result == 'q' and result != 'c'): + break + print(f"\nThe first number is the previous result: {output1}") + num1 = output1 + operator = input("Enter the operator: ") + num2 = int(input("Enter your second number: ")) + output1 = calculator (num1, num2) + result = result_func()