-

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

Different types of taxes in India In India, the taxation system is intricate and includes various types of taxes that cater to different sectors of the economy. Here are 20 different types of taxes in India.
The Olympics: A Journey Through History and the Road to Paris 2024 The Olympic Games, often referred to simply as the Olympics, are the world's foremost sports competition, featuring summer and winter games in which thousands of athletes from around the world participate in a variety of competitions. The Games are considered the world's premier sports competition and include more than 200 nations participating.
Understanding the Indian Tax Structure The Indian tax structure is a comprehensive system that funds the government's expenditures and supports the nation's economic development. In this blog, we will explore the various taxes levied in India, their rates, and important aspects to consider. Please note that tax rates and policies are subject to change, and it is essential to consult with a tax professional or refer to the latest government notifications for the most accurate and up-to-date information.
AI in Indian Elections: Unveiling the Challenges and Strategic Solutions Artificial Intelligence (AI) is revolutionizing numerous sectors globally, and elections are no exception. In India, with its vast and diverse electorate, the integration of AI into the electoral process presents both significant opportunities and complex challenges. This blog post delves into the role of AI in Indian elections, highlighting the challenges and proposing strategic solutions to ensure a fair and transparent democratic process.
Cultivating Change: A Comprehensive Study of India's Agricultural Issues and Remedies Agriculture is a critical sector in India, employing nearly half of the country's workforce and contributing significantly to the GDP. Despite its importance, the sector faces numerous challenges that hinder its growth and productivity. This analysis outlines the key problems in Indian agriculture and proposes potential solutions to address them.
The Future of SEO if ChatGPT Kills Search Engines Search Engine Optimization (SEO) has been a cornerstone of digital marketing for decades. Companies have invested heavily in optimizing their websites to rank higher on search engine results pages (SERPs) of giants like Google, Bing, and Yahoo. However, the advent of AI technologies, particularly language models like ChatGPT, is poised to revolutionize the landscape. What happens to SEO if ChatGPT replaces traditional search engines? This blog will explore the potential future of SEO in a world where ChatGPT dominates information retrieval.