DEV Community

Aaditya Parag Patani
Aaditya Parag Patani

Posted on

Building My First Python Calculator as a 1st-Year Computer Engineering Student

Hello DEV Community! 👋

I am Aaditya, a 1st-year computer engineering student. As I start my journey into programming and building a path toward software engineering, I wanted to share one of the first scripts I wrote in Python: a custom command-line calculator.

It handles basic arithmetic (+, -, *, /), includes division-by-zero error handling, and I even added an exponentiation (power) feature to experiment with Python's syntax!

Here is the code I built:

a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
operator = input("Enter the operator you want (+, -, *, /, power): ")

if operator == "+":
    print("Sum: ", a + b)
elif operator == "-":
    print("Difference: ", a - b)
elif operator == "*":
    print("Product: ", a * b)
elif operator == "power":
    print("Power: ", a ** b)
elif operator == "/":
    if b == 0:
        print("Error: Division by zero is undefined.")
    else:
        print("Quotient: ", a / b)
else:
    print("Invalid operator. Please use one of the following: +, -, *, /, power.")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)