Python Beginner Project: Simple Calculator

In this project, you will build a simple calculator that can perform basic arithmetic operations (addition, subtraction, multiplication, and division). Here are the steps you can follow to complete this project:

Simple Calculator Steps

Step 1

Start by creating a new Python file.

Step 2

Define a function called calculator that takes in two numbers and an operator as arguments.

def calculator(num1, num2, operator): pass
Code language: Python (python)

Step 3

Inside the calculator function, use an if statement to check the value of the operator. Depending on the operator, perform the corresponding arithmetic operation and return the result.

def calculator(num1, num2, operator): if operator == "+": return num1 + num2 elif operator == "-": return num1 - num2 elif operator == "*": return num1 * num2 elif operator == "/": return num1 / num2 else: return "Invalid operator"
Code language: Python (python)

Step 4

Test the calculator function by calling it with different numbers and operators.

result = calculator(2, 3, "+") print(result) # Output: 5 result = calculator(5, 2, "-") print(result) # Output: 3 result = calculator(2, 3, "*") print(result) # Output: 6 result = calculator(6, 3, "/") print(result) # Output: 2 result = calculator(2, 3, "&") print(result) # Output: "Invalid operator"
Code language: Python (python)

Step 5

To make the calculator more user-friendly, you can add a prompt that asks the user to input the numbers and the operator they want to use. You can use the input function to get the user’s input and pass it to the calculator function.

num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) operator = input("Enter the operator (+, -, *, /): ") result = calculator(num1, num2, operator) print(result)
Code language: Python (python)

Step 6

You can also add error handling to the calculator to handle cases where the user inputs invalid values or an unsupported operator. For example, you can use a try/except block to catch any errors that may occur and display an error message to the user.

try: num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) operator = input("Enter the operator (+, -, *, /): ") result = calculator(num1, num2, operator) print(result) except ValueError: print("Invalid input. Please enter a number.") except ZeroDivisionError: print("Cannot divide by zero.")
Code language: Python (python)

Final Product

Here is the full code for the calculator:

def calculator(num1, num2, operator): if operator == "+": return num1 + num2 elif operator == "-": return num1 - num2 elif operator == "*": return num1 * num2 elif operator == "/": return num1 / num2 else: return "Invalid operator" try: num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) operator = input("Enter the operator (+, -, *, /): ") result = calculator(num1, num2, operator) print(result) except ValueError: print("Invalid input. Please enter a number.") except ZeroDivisionError: print("Cannot divide by zero.")
Code language: Python (python)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *