Easy Python Learning: Basic calculator script in Python

 Easy Python Learning: Basic calculator script in Python -->
Easy Python Learning: Basic calculator script in Python

To create a calculator in Python, Below is a simple script that can perform addition, subtraction, multiplication, and division:

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        return "Error: Cannot divide by zero"
    return x / y

print("Basic Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

while True:
    choice = input("Enter choice (1/2/3/4) or 'q' to quit: ")

    if choice.lower() == 'q':
        print("Exiting the calculator.")
        break

    if choice not in ['1', '2', '3', '4']:
        print("Invalid input. Please try again.")
        continue

    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))

    if choice == '1':
        print("Result:", add(num1, num2))
    elif choice == '2':
        print("Result:", subtract(num1, num2))
    elif choice == '3':
        print("Result:", multiply(num1, num2))
    elif choice == '4':
        print("Result:", divide(num1, num2))
    else:
        print("Invalid input. Please try again.")

Save this code in a file with a .py extension, run the script, and you'll have a basic calculator in action. It will keep asking for the user's choice until the user decides to quit by entering 'q'. The calculator handles floating-point numbers and provides a message when attempting to divide by zero.

You may interested in

Creating a beautiful kaleidoscope using Python's turtle Creating a beautiful kaleidoscope using Python's turtle graphics can be a fun and mesmerizing experience. Below is a Python script to draw a kaleidoscope-like pattern using the turtle module:
Easy Python Learning: Calculator script in Python with GUI To create a basic calculator with a graphical user interface (GUI) in Python, we can use the tkinter library. Here's a simple Python script that implements a GUI-based calculator:
Easy Python Learning: Basic calculator script in Python To create a calculator in Python, Below is a simple script that can perform addition, subtraction, multiplication, and division:

Trending

Important Milestone of Indian history from 1857 to 2021 Discover the fascinating timeline of India's history from 1857 to 2021, highlighting significant milestones that shaped the nation. Delve into the influential events, political shifts, and remarkable developments that have defined India's journey over the years. Uncover the captivating history of India with this comprehensive timeline of important milestones.
The East India Company (EIC): Rise and Fall The East India Company's history is a complex tale of trade, expansion, and eventual decline, ultimately leading to the establishment of direct British rule in India, which lasted until India gained independence in 1947.
From Turmoil to Transformation: Unraveling the Russian Revolution and Its Global Impact In this captivating blog, we delve into the tumultuous era of the Russian Revolution. Join us as we explore the causes and factors that fueled this historic event, uncovering its profound impact on the world. From key players to pivotal events, we leave no stone unturned in our quest to understand this transformative period. Prepare to be enlightened as we conclude with a reflection on the lasting legacy and invaluable lessons learned from the Russian Revolution. Don't miss out on this insightful journey through history! Read now.
9/11 : Day That Changed the World On September 11, 2001, a day that will forever be etched in our collective memory, the world witnessed a series of coordinated terrorist attacks on the United States. This tragic event, known as 9/11, marked a turning point in history and had far-reaching consequences that continue to shape our world today. In this blog, we will delve into the events of that fateful day, exploring the impact it had on individuals, communities, and nations around the globe.
The French Revolution: Its Causes, Implications, and Influence on Today's World Welcome to our blog on the French Revolution, a pivotal moment in history that shaped the world as we know it today. In this article, we will delve into the causes, events, and lasting impact of this monumental revolution. So, sit back, relax, and prepare to embark on a journey through time as we unveil the fascinating story of the French Revolution.
Indian Freedom struggle from 1857 to 1947 The Indian freedom struggle was a series of events to end British rule in India, which lasted from 1857 to 1947. It began with the Revolt of 1857, also known as the Sepoy Mutiny, which spread across many regions. It continued with various movements, such as the Swadeshi movement, the Home Rule movement, the Khilafat and Non-cooperation movement, the Civil Disobedience movement, the Individual Satyagraha, and the Quit India movement. These movements were led by different leaders, such as Gandhi, Tilak, Besant, Nehru, Patel, Azad, and others.