Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How can make Calculator

Simple calculator codes 

Here's an example of a simple calculator program in Python:

while True:

    print("Options:")

    print("Enter 'add' to add two numbers")

    print("Enter 'subtract' to subtract two numbers")

    print("Enter 'multiply' to multiply two numbers")

    print("Enter 'divide' to divide two numbers")

    print("Enter 'quit' to end the program")

    user_input = input(": ")

    if user_input == "quit":

        break

    elif user_input == "add":

        num1 = float(input("Enter a number: "))

        num2 = float(input("Enter another number: "))

        result = num1 + num2

        print("The answer is " + str(result))

    elif user_input == "subtract":

        num1 = float(input("Enter a number: "))

        num2 = float(input("Enter another number: "))

        result = num1 - num2

        print("The answer is " + str(result))

    elif user_input == "multiply":

        num1 = float(input("Enter a number: "))

        num2 = float(input("Enter another number: "))

        result = num1 * num2

        print("The answer is " + str(result))

    elif user_input == "divide":

        num1 = float(input("Enter a number: "))

        num2 = float(input("Enter another number: "))

        result = num1 / num2

        print("The answer is " + str(result))

    else:

        print("Unknown input")

This calculator program uses a while loop to keep the program running until the user enters "quit". The program then prompts the user to enter one of four options: "add", "subtract", "multiply", or "divide". Depending on the user's input, the program will perform the corresponding calculation and print the result to the screen. If the user enters an input that is not one of the four options or "quit", the program will print "Unknown input".

Advanced calculator codes :

here's a more feature-rich calculator example that includes a menu with options for basic arithmetic operations as well as advanced functions such as square root, power, and modulus. The program also includes error handling for invalid input.

import math

def calculator():

    while True:

        Print("Calculator Menu")

        Print("1. Addition")

        Print("2. Subtraction")

        Print("3. Multiplication")

        Print("4. Division")

        Print("5. Power")

        Print("6. Square Root")

        Print("7. Modulus")

        Print("8. Quit")

        

        try:

            choice = int(input("Enter your choice (1-8): "))

            if choice 8:

                raise Value Error

        except Value Error:

            print("Invalid input. Please enter a number between 1 and 8.")

            continue

        

        if choice == 8:

            break

        

        num1 = float(input("Enter first number: "))

        num2 = float(input("Enter second number: "))

        

        if choice == 1:

            Print("Result: ", num1 + num2)

        Elif choice == 2:

            print("Result: ", num1 - num2)

        Elif choice == 3:

            print("Result: ", num1 * num2)

        Elif choice == 4:

            if num2 == 0:

                print("Error: Division by zero")

            else:

                print("Result: ", num1 / num2)

        Elif choice == 5:

            print("Result: ", math.pow(num1, num2))

        Elif choice == 6:

            if num1

                print("Error: Negative number cannot have a real square root")

            else:

                print("Result: ", math.sqrt(num1))

        Elif choice == 7:

            print("Result: ", num1 % num2)

    print("Thank you for using the calculator!")

calculator()

This program uses a while loop to keep the program running until the user enters 8 to quit. The program uses a try-except block to handle invalid input and check if the input is an integer between 1 and 8. Depending on the user's input, the program will perform the corresponding calculation and print the result to the screen. If the user enters an input that is not one of the eight options or 8 for quit, the program will print "Invalid input. Please enter a number between 1 and 8."



This post first appeared on How Do Astronauts Survive In Space | Space Science?, please read the originial post: here

Share the post

How can make Calculator

×

Subscribe to How Do Astronauts Survive In Space | Space Science?

Get updates delivered right to your inbox!

Thank you for your subscription

×