Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions recipes/Python/578599_Calculator/README.md
Original file line number Diff line number Diff line change
@@ -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.
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.
58 changes: 58 additions & 0 deletions recipes/Python/578599_Calculator/recipe-578599_v2.py
Original file line number Diff line number Diff line change
@@ -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()