Friday, September 14, 2018

How to Make a Simple Calculator in Python | Codeing School

How to make a simple calculator in python

Python Program to Make a Simple Calculator. In this example, you will learn to create a simple calculator that can add, subtract, multiply or divide depending upon the input from the user. Python Functions. Python Function Arguments.

How to Make a Simple Calculator in Python | Codeing School
How to Make a Simple Calculator in Python | Codeing School

Source Code:  Simple Calculator by Making Functions

def calculetor():
    print("\nWellcome to Calc: ")
    operation = input('''
    Please type in the math operation you would like to complete:
    + for addition
    - for subtraction
    * for multiplication
    / for division
    ** for power
    % for modulo
   
    Enter Your Choise:
    ''')

    num1 = int(input("Enter first Number: "))
    num2 = int(input("Enter second Number: "))

    if operation == '+':
        print(f"{num1} + {num2} = {num1 + num2}")
    elif operation == '-':
        print(f"{num1} - {num2} = {num1 - num2}")
    elif operation == '*':
        print(f"{num1} * {num2} = {num1 * num2}")
    elif operation == '/':
        print(f"{num1} / {num2} = {num1 / num2}")
    elif operation == '**':
        print(f"{num1} ** {num2} = {num1 ** num2}")
    elif operation == '%':
        print(f"{num1} % {num2} = {num1 % num2}")
    else:
        print("You Press a Invalid Key")
    again()

def again():
    cal_again = input('''
    Do you want to calculate again?
    Please type y for YES or n for NO.
    ''')

    if cal_again == 'y':
        calculetor()
    elif cal_again == 'n':
        print("See You Later")
    else:
        again()

calculetor()

Output:

Wellcome to Calc:

    Please type in the math operation you would like to complete:
    + for addition
    - for subtraction
    * for multiplication
    / for division
    ** for power
    % for modulo
   
    Enter Your Choise:
    +
Enter first Number: 2
Enter second Number: 3
2 + 3 = 5

    Do you want to calculate again?
    Please type y for YES or n for NO.
    n
See You Later

In this program, we ask the user to choose the desired operation. Options +, -, *, /, **, % are valid. Two numbers are taken and an if...elif...else branching is used to execute a particular section. User-defined functions calculator and again evaluate respective operations.

0 comments:

Post a Comment