As a part of learning, I started coding in python. My kids are just curious about what am I learning and wanted to test my code, so I started with basic calculator.
Core Concepts :
- Arithmetic Operators
- Logical Operators
- Conditional Statements
- Functions
Variables :
Three variables used here are for accepting inputs from the user.
They are, mode_of_operation , first_number and second_number.
mode_of_operation = input("""Enter the Operation you want to perform : "
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
% for Modulo
** for Exponent : """)
first_number = input("Enter first number : ")
second_number = input("Enter second number : ")
Functions :
Functions defined are,
def is_blank():
def is_digit():
def basic_calculator(n):
Function is_blank() is to check whether input field is maintained or left blank.
If any of the input is blank, this function returns False otherwise True.
def is_blank():
if (first_number != "") and (second_number != ""):
is_blank = True
else:
is_blank = False
return is_blank
In is_digit() function variable_name.is_digit() method is used, that returns True if all the characters are digits, otherwise False.
def is_digit():
if (first_number.isdigit()) and (second_number.isdigit()):
is_digit = True
else:
is_digit = False
return is_digit
In def basic_calculator(n),mode_of_operation is passed as an argument and depending on selection, conditional loop will execute. If valid operator is not entered, final else block statement is executed and prints: “Please enter valid operation”
def basic_calculator(n):
if n == "+":
if is_blank() and is_digit():
result = int(first_number) + int(second_number)
else:
result = "Please enter valid inputs"
elif n == "-":
if is_blank() and is_digit():
result = int(first_number) - int(second_number)
else:
result = "Please enter valid inputs "
elif n == "*":
if is_blank() and is_digit():
result = int(first_number) * int(second_number)
else:
result = "Please enter valid inputs"
elif n == "/":
if is_blank() and is_digit():
result = int(first_number) / int(second_number)
else:
result = "Please enter valid inputs"
elif n == "%":
if is_blank() and is_digit():
result = int(first_number) % int(second_number)
else:
result = "Please enter valid inputs"
elif n == "**":
if is_blank() and is_digit():
result = int(first_number) ** int(second_number)
else:
result = "Please enter valid inputs"
else:
result = "Please select valid operation "
print(result)
Finally , call the function with mode_of_operation as argument.
basic_calculator(mode_of_operation)
Output :
Scenario 1 -
mode_of_operation : +
first_number : 50
second_number : 50
Enter the Operation you want to perform : "
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
% for Modulo
** for Exponent : +
Enter first number : 50
Enter second number : 50
100
Scenario 2 -
mode_of_operation : 5
first_number : 50
second_number : 50
Enter the Operation you want to perform : "
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
% for Modulo
** for Exponent : 5
Enter first number : 50
Enter second number : 50 Please select valid operation
Scenario 3 -
mode_of_operation : *
first_number :
second_number : 50
Enter the Operation you want to perform : "
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
% for Modulo
** for Exponent : *
Enter first number :
Enter second number : 50
Please enter valid inputs
Scenario 4 -
mode_of_operation : *
first_number : kg
second_number : 50
Enter the Operation you want to perform : "
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
% for Modulo
** for Exponent : *
Enter first number : kg
Enter second number : 50
Please enter valid inputs
Thanks for reading..!!